scheduler

中文文档
Introduction
scheduler is a job scheduling package for Go. It provides a simple, humans-friendly way to schedule the execution of the go function and includes delay and periodic.
Inspired by Linux cron and Python schedule.
Features
- Delay execution, accurate to a second
- Periodic execution, accurate to a second, like the cron style but more flexible
- Cancel job
- Failure retry
Installation
go get github.com/prprprus/scheduler
Example
job function
func task1(name string, age int) {
fmt.Printf("run task1, with arguments: %s, %d\n", name, age)
}
func task2() {
fmt.Println("run task2, without arguments")
}
Delay
Delayed supports four modes: seconds, minutes, hours, and days.
package main
import (
"fmt"
"github.com/prprprus/scheduler"
)
func main() {
s, err := scheduler.NewScheduler(1000)
if err != nil {
panic(err)
}
jobID := s.Delay().Second(1).Do(task1, "prprprus", 23)
jobID = s.Delay().Minute(1).Do(task2)
jobID = s.Delay().Hour(1).Do(task2)
jobID = s.Delay().Do(task2)
jobID = s.Delay().Day(1).Do(task2)
err = s.CancelJob(jobID)
if err != nil {
panic(err)
} else {
fmt.Println("cancel delay job success")
}
}
Every
Like the cron style, it also includes seconds, minutes, hours, days, weekday, and months, but the order and number are not fixed. You can freely arrange and combine them according to your own preferences. For example, the effects of Second(3).Minute(35).Day(6)
and Minute(35).Day(6).Second(3)
are the same. No need to remember the format! 🎉👏
But for the readability, recommend the chronological order from small to large (or large to small).
Note: Day()
and Weekday()
avoid simultaneous occurrences unless you know that the day is the day of the week.
package main
import (
"fmt"
"github.com/prprprus/scheduler"
)
func main() {
s, err := scheduler.NewScheduler(1000)
if err != nil {
panic(err)
}
jobID = s.Every().Second(45).Minute(20).Hour(13).Day(23).Weekday(3).Month(6).Do(task1, "prprprus", 23)
jobID = s.Every().Second(15).Minute(40).Hour(16).Weekday(4).Do(task2)
jobID = s.Every().Second(1).Do(task1, "prprprus", 23)
jobID = s.Every().Do(task2)
jobID = s.Every().Second(1).Minute(1).Hour(1).Do(task2)
err = s.CancelJob(jobID)
if err != nil {
panic(err)
} else {
fmt.Println("cancel periodically job success")
}
}
Documentation
Full documentation
Contribution
Thank you for your interest in contribution of scheduler, your help and contribution is very valuable.
You can submit issue and pull requests and fork, please submit an issue before submitting pull requests.
License
See LICENSE for more information.
have fun✨👻✨