The cron library is a cron job library for Go.
It is a fork of robfig/cron with some improvements.
Thanks to robfig/cron for the original work, and thanks to all the contributors.
[!IMPORTANT]
v4.x
may introduce situations that are not backward compatible.The reason for this is that we are using
v4.x
as a transitional version. In this version, we will try to improve the functionality of the components as much as possible until the release ofv5.x
.When releasing a new version, backward compatibility is the default behavior. If there are any incompatibilities, they will be indicated in the release notes.
go get github.com/flc1125/go-cron/v4
package main
import (
"context"
"github.com/flc1125/go-cron/v4"
"github.com/flc1125/go-cron/v4/middleware/nooverlapping"
"github.com/flc1125/go-cron/v4/middleware/recovery"
)
func main() {
c := cron.New(
cron.WithSeconds(), // if you want to use seconds, you can use this option
cron.WithMiddleware(
recovery.New(), // recover panic
nooverlapping.New(), // prevent job overlapping
),
cron.WithContext(context.Background()), // use custom context
// ... other options
)
// use middleware
c.Use(recovery.New(), nooverlapping.New()) // use middleware
// add job
entryID, _ := c.AddJob("* * * * * *", cron.JobFunc(func(ctx context.Context) error {
// do something
return nil
}))
_ = entryID
// add func
_, _ = c.AddFunc("* * * * * *", func(ctx context.Context) error {
// do something
return nil
}, recovery.New(), nooverlapping.New()) // use middleware for this job
// start cron
c.Start()
// stop cron
c.Stop()
}