README
scheduler
Job scheduling made easy.
Scheduler allows you to schedule recurrent jobs with an easy-to-read syntax.
Inspired by the article Rethinking Cron and the schedule python module.
How to use?
package main
import (
"fmt"
"runtime"
"time"
"github.com/carlescere/scheduler"
)
func main() {
job := func() {
t := time.Now()
fmt.Println("Time's up! @", t.UTC())
}
// Run every 2 seconds but not now.
scheduler.Every(2).Seconds().NotImmediately().Run(job)
// Run now and every X.
scheduler.Every(5).Minutes().Run(job)
scheduler.Every().Day().Run(job)
scheduler.Every().Monday().At("08:30").Run(job)
// Keep the program from not exiting.
runtime.Goexit()
}
How it works?
By specifying the chain of calls, a Job
struct is instantiated and a goroutine is starts observing the Job
.
The goroutine will be on pause until:
- The next run scheduled is due. This will cause to execute the job.
- The
SkipWait
channel is activated. This will cause to execute the job. - The
Quit
channel is activated. This will cause to finish the job.
Not immediate recurrent jobs
By default the behaviour of the recurrent jobs (Every(N) seconds, minutes, hours) is to start executing the job right away and then wait the required amount of time. By calling specifically .NotImmediately()
you can override that behaviour and not execute it directly when the function Run()
is called.
scheduler.Every(5).Minutes().NotImmediately().Run(job)
License
Distributed under MIT license. See LICENSE
for more information.
Documentation
Overview ¶
Package scheduler is a cron replacement based on:
http://adam.herokuapp.com/past/2010/4/13/rethinking_cron/
and
https://github.com/dbader/schedule
Uses include:
func main() { job := func() { fmt.Println("Time's up!") } scheduler.Every(5).Seconds().Run(function) scheduler.Every().Day().Run(function) scheduler.Every().Sunday().At("08:30").Run(function) }
Index ¶
- type Job
- func (j *Job) At(hourTime string) *Job
- func (j *Job) Day() *Job
- func (j *Job) Friday() *Job
- func (j *Job) Hours() *Job
- func (j *Job) IsRunning() bool
- func (j *Job) Minutes() *Job
- func (j *Job) Monday() *Job
- func (j *Job) NotImmediately() *Job
- func (j *Job) Run(f func()) (*Job, error)
- func (j *Job) Saturday() *Job
- func (j *Job) Seconds() *Job
- func (j *Job) Sunday() *Job
- func (j *Job) Thursday() *Job
- func (j *Job) Tuesday() *Job
- func (j *Job) Wednesday() *Job
Constants ¶
Variables ¶
Functions ¶
Types ¶
type Job ¶
type Job struct { Quit chan bool SkipWait chan bool sync.RWMutex // contains filtered or unexported fields }
Job defines a running job and allows to stop a scheduled job or run it.
func Every ¶
Every defines when to run a job. For a recurrent jobs (n seconds/minutes/hours) you should specify the unit and then call to the correspondent period method.
func (*Job) At ¶
At lets you define a specific time when the job would be run. Does not work with recurrent jobs. Time should be defined as a string separated by a colon. Could be used as "08:35:30", "08:35" or "8" for only the hours.
func (*Job) Hours ¶
Hours sets the job to run every n Hours where n was defined in the Every function.
func (*Job) Minutes ¶
Minutes sets the job to run every n Minutes where n was defined in the Every function.
func (*Job) NotImmediately ¶
NotImmediately allows recurrent jobs not to be executed immediatelly after definition. If a job is declared hourly won't start executing until the first hour passed.
func (*Job) Run ¶
Run sets the job to the schedule and returns the pointer to the job so it may be stopped or executed without waiting or an error.