Currently timingwheel only supports one-time tasks (via AfterFunc), it's meaningful to provide a general mechanism for supporting repetitive tasks (a.k.a. Cron jobs).
Inspired by cron.Schedule, the API is designed to be as follows:
// Scheduler determines the execution plan of a task.
type Scheduler interface {
// Next returns the next execution time after the given (previous) time.
// It will return a zero time if no next time is scheduled.
Next(time.Time) time.Time
}
// ScheduleFunc calls f (in its own goroutine) according to the execution
// plan scheduled by s.
// It returns a Timer that can be used to cancel the call using its Stop method.
//
// Internally, ScheduleFunc will ask the first execution time (by calling
// s.Next()) initially, and create a timer if the execution time is non-zero.
// Afterwards, it will ask the next execution time each time f is about to
// be executed, and f will be called at the next execution time if the time
// is non-zero.
func (tw *TimingWheel) ScheduleFunc(s Scheduler, f func()) *Timer {
...
}
Usage example
Based on the Scheduler interface,it's possible for us to implement all kinds of repetitive tasks.
Motivation
Currently
timingwheel
only supports one-time tasks (via AfterFunc), it's meaningful to provide a general mechanism for supporting repetitive tasks (a.k.a. Cron jobs).(For Chinese users, also see https://github.com/RussellLuo/timingwheel/issues/8.)
The API
Inspired by cron.Schedule, the API is designed to be as follows:
Usage example
Based on the
Scheduler
interface,it's possible for us to implement all kinds of repetitive tasks.EveryFunc shortcut
As an example (as well as a comparison of https://github.com/RussellLuo/timingwheel/pull/9), the new implementation will be as follows:
Cron expression
By leveraging cronexpr or cron.Parser to parse Cron expression, it's possible for us to support Cron jobs in a more conventional way.