New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/dontsellfish/schedule-worker

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/dontsellfish/schedule-worker

  • v0.0.0-20220908130305-800c58294ec4
  • Source
  • Go
  • Socket score

Version published
Created
Source

Schedule Worker

Introduction

I needed something both simple and more functional than a familiar go func() { time.Sleep(...); foo(); }

So this package was created. All code review/additions would be very appreciated.

Functions Overview

workerFoo := NewScheduleWorker(foo) // without error handler
workerBar := NewScheduleWorker(bar, OnErrorFunc) // with error handler
Main 3 functions of a worker: For, Until, Add

  • workerFoo.For(time.Minute) -- schedule for a minute from now
  • workerBar.Until(time.Parse("2006-01-02", "2077-01-01")) -- schedule until 2077
  • workerBar.For(time.Minute).Add(time.Minute * 5) -- worker.Add allows to increase/decrease datetime scheduled

DoImmediately, Cancel, GetTime, IsDone, ExtNewScheduleWorker -- could be useful depend on your use case

Example

package main

import (
	"log"
	"os"
	"time"

	_ "github.com/dontsellfish/Schedule-Worker"
)

func DeleteExampleFile() error {
	return os.Remove("example.jpg")
}

func OnError(err error) {
	log.Println(err.Error())
}

func main() {
	unneededFileDeletionWorker := NewScheduleWorker(DeleteExampleFile, OnError).For(time.Minute)
	// some initialization
	for {
		select {
		case _ = <-FileAccessRequested:
			unneededFileDeletionWorker.For(time.Minute)
			// some work with the file
		case _ = <-DeleteRightNow:
			unneededFileDeletionWorker.Immediately()
		case _ = <-NeverDeleteTheFile:
			unneededFileDeletionWorker.Cancel()
		}
	}
}

FAQs

Package last updated on 08 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc