Documentation ¶
Overview ¶
jobkicker is A Golang in-process cron task scheduler that kicks (executes) them once in specified time or periodically.
Index ¶
- type ITimer
- type Job
- type JobKicker
- func (jobKicker *JobKicker) CancelJob(jobId string) error
- func (jobKicker *JobKicker) KickOnceAfter(delay time.Time, fn interface{}, args ...interface{}) (jobID string)
- func (jobKicker *JobKicker) KickOnceAt(runAt time.Time, fn interface{}, args ...interface{}) (jobID string)
- func (jobKicker *JobKicker) KickPeriodicallyEvery(delay time.Time, fn interface{}, args ...interface{}) (jobID string)
- type JobQueue
- type JobType
- type KickerTicker
- type KickerTimer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct { JobType JobType Fn interface{} Args []interface{} Timer ITimer // contains filtered or unexported fields }
Job is the representation of the job to run
type JobKicker ¶
type JobKicker struct {
// contains filtered or unexported fields
}
func NewScheduler ¶
NewScheduler generates new JobKicker (scheduler) and enables to configure the logger.
by passing `loggerOutput *io.Writer` like a file or any thing implements io.Writer to write the logs to. if nil passed it's write to `os.Stderr`
by passing `loggerFormatter *log.Formatter` it allows to format the logs before logging it by passing any struct implements the interface:
type Formatter interface { Format(*Entry) ([]byte, error) }
https://github.com/sirupsen/logrus used for logging so you can try to pass `&log.JSONFormatter{}` for example to format the logs as json. and if nil is passed it will use `logrus.TextFormatter` by default.
func (*JobKicker) CancelJob ¶
CancelJob cancels a job with it's id by using a cancel context that the job struct holds
Example ¶
package main import ( "fmt" "time" "github.com/MohabMohamed/jobkicker" ) func main() { task := func() { println("jobkicker is awesome") } jk := jobkicker.NewScheduler(nil, nil) // time.Date(year ,month ,day ,hour ,min ,sec,nsec,loc) // every field equals zeo expect seconds equals 3 // as KickPeriodicallyEvery takes the delay between every execution delay := time.Date(0, 0, 0, 0, 0, 3, 0, time.UTC) jobId := jk.KickPeriodicallyEvery(delay, task) // sleep for 4 seconds to let it run once before cancelling time.Sleep(4 * time.Second) err := jk.CancelJob(jobId) if err != nil { fmt.Printf("error while cancelling a job: %s", err.Error()) } time.Sleep(10 * time.Second) }
Output: jobkicker is awesome INFO[0003] job with id [e7e0d6ab-6aa5-4b52-9f60-3c74e5585b15] executed in 2022-02-09 12:49:14.102550679 +0200 EET m=+3.001345112 INFO[0016] job with id [e7e0d6ab-6aa5-4b52-9f60-3c74e5585b15] cancelled successfully in 2022-02-09 12:49:15.101968589 +0200 EET m=+4.000763046
func (*JobKicker) KickOnceAfter ¶
func (jobKicker *JobKicker) KickOnceAfter(delay time.Time, fn interface{}, args ...interface{}) (jobID string)
Example ¶
package main import ( "time" "github.com/MohabMohamed/jobkicker" ) func main() { task := func() { println("jobkicker is awesome") } jk := jobkicker.NewScheduler(nil, nil) // time.Date(year ,month ,day ,hour ,min ,sec,nsec,loc) // every field equals zeo expect seconds equals 3 delay := time.Date(0, 0, 0, 0, 0, 3, 0, time.UTC) jk.KickOnceAfter(delay, task) time.Sleep(4 * time.Second) }
Output: jobkicker is awesome INFO[0003] job with id [5e1b8baa-5133-483b-9188-8179ecc8aea4] executed in 2022-02-08 22:43:08.701094256 +0200 EET m=+3.002681837
func (*JobKicker) KickOnceAt ¶
func (jobKicker *JobKicker) KickOnceAt(runAt time.Time, fn interface{}, args ...interface{}) (jobID string)
KickOnceAt excutes a function once at a certain time and returns the job id
Example ¶
package main import ( "fmt" "time" "github.com/MohabMohamed/jobkicker" ) func main() { task := func() { println("jobkicker is awesome") } jk := jobkicker.NewScheduler(nil, nil) // time.Date(year ,month ,day ,hour ,min ,sec,nsec,loc) // KickOnceAt takes the date to run the task in // in this example I run it with date equals now + 3 seconds fmt.Println(time.Now()) runAt := time.Now().Add(3 * time.Second) jk.KickOnceAt(runAt, task) time.Sleep(4 * time.Second) }
Output: 2022-02-09 12:39:26.645499827 +0200 EET m=+0.000422605 jobkicker is awesome INFO[0003] job with id [0477e575-22a1-48a2-851f-017b5aeb9ea4] executed in 2022-02-09 12:39:29.646510482 +0200 EET m=+3.001433319
func (*JobKicker) KickPeriodicallyEvery ¶
func (jobKicker *JobKicker) KickPeriodicallyEvery(delay time.Time, fn interface{}, args ...interface{}) (jobID string)
KickPeriodicallyEvery excutes a function Periodically with the passed interval between every execution and returns the job id
Example ¶
package main import ( "time" "github.com/MohabMohamed/jobkicker" ) func main() { task := func() { println("jobkicker is awesome") } jk := jobkicker.NewScheduler(nil, nil) // time.Date(year ,month ,day ,hour ,min ,sec,nsec,loc) // every field equals zeo expect seconds equals 3 // as KickPeriodicallyEvery takes the delay between every execution delay := time.Date(0, 0, 0, 0, 0, 3, 0, time.UTC) jk.KickPeriodicallyEvery(delay, task) time.Sleep(10 * time.Second) }
Output: jobkicker is awesome INFO[0003] job with id [d8ae6a99-1d2b-434f-8a9c-db77a4e3e844] executed in 2022-02-09 12:43:54.758287996 +0200 EET m=+3.001531441 jobkicker is awesome INFO[0009] job with id [d8ae6a99-1d2b-434f-8a9c-db77a4e3e844] executed in 2022-02-09 12:43:57.760335343 +0200 EET m=+6.003578840 jobkicker is awesome INFO[0011] job with id [d8ae6a99-1d2b-434f-8a9c-db77a4e3e844] executed in 2022-02-09 12:44:00.758331522 +0200 EET m=+9.001575009
type JobQueue ¶
type JobQueue struct { sync.RWMutex PendingJobs map[string]*Job DoneJobs map[string]time.Time //Done jobs with it's last execution time }
the collections that hold the the pending jobs to be executed and the executed jobs jobs with the last time it ran.
Note: periodic jobs stays in PendingJobs map after execution unless got canceled and Done jobs in this case holds the last execution time.
type KickerTicker ¶
KickerTimer is a wrapper to time.Ticker
func InitiateNewKickerTicker ¶
func InitiateNewKickerTicker(d time.Duration) *KickerTicker
InitiateNewKickerTicker returns new KickerTicker set to a duration
func (*KickerTicker) GetWaiter ¶
func (kickerTicker *KickerTicker) GetWaiter() <-chan time.Time
GetWaiter returns the chan of the Ticker
type KickerTimer ¶
KickerTimer is a wrapper to time.Timer
func InitiateNewKickerTimer ¶
func InitiateNewKickerTimer(d time.Duration) *KickerTimer
InitiateNewKickerTimer returns new KickerTimer set to a duration
func (*KickerTimer) GetWaiter ¶
func (kickerTimer *KickerTimer) GetWaiter() <-chan time.Time
GetWaiter returns the chan of the Timer