Documentation ¶
Index ¶
- Constants
- type Cron
- type Crontab
- func (o *Crontab) Add(worker *Worker) error
- func (o *Crontab) After(_ context.Context) (err error)
- func (o *Crontab) Del(worker *Worker)
- func (o *Crontab) DelByClass(class string)
- func (o *Crontab) GetName() string
- func (o *Crontab) GetWorker(class string) *Worker
- func (o *Crontab) GetWorkers() map[string]*Worker
- func (o *Crontab) Process() process.Process
- func (o *Crontab) Run(ctx context.Context) (err error)
- func (o *Crontab) Start(ctx context.Context) (err error)
- func (o *Crontab) Started() bool
- func (o *Crontab) Stop()
- func (o *Crontab) Stopped() bool
- type Error
- type Job
- type JobBefore
- type JobFailed
- type JobFinish
- type JobSucceed
- type Strategy
- type Worker
Examples ¶
Constants ¶
const ( // DefaultGloballyUnique // default to disable globally unique. DefaultGloballyUnique = false // DefaultNodeUnique // default to enabled node unique. DefaultNodeUnique = true // DefaultRunOnStartup // default to disable run for crontab startup. DefaultRunOnStartup = false )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cron ¶
type Cron interface { // Add // a worker into crontab. Add(worker *Worker) error GetWorker(class string) *Worker GetWorkers() map[string]*Worker Process() process.Process Start(ctx context.Context) error Started() bool Stop() Stopped() bool }
Cron is an interface of Crontab.
type Crontab ¶
type Crontab struct {
// contains filtered or unexported fields
}
Crontab is a scheduler for crontab. It's worked as a provider for process.
func NewCrontab ¶
NewCrontab creates a new crontab instance.
Example ¶
crontab := NewCrontab("example") println("crontab:", crontab.GetName())
Output: crontab: example
func (*Crontab) After ¶
After wait all running scheduler done before stopped. It's called by process, do not call it directly.
func (*Crontab) DelByClass ¶
DelByClass deletes a worker from crontab by class name.
func (*Crontab) GetWorkers ¶
GetWorkers returns the added worker mapping.
func (*Crontab) Start ¶
Start starts the crontab service by call process.
Example ¶
var ( crontab *Crontab ctx = context.Background() err error worker *Worker ) go func() { time.Sleep(time.Second * 5) if err == nil { crontab.Stop() } }() worker = NewWorker(EveryMinute, &ej{}) crontab = NewCrontab("example") if err = crontab.Add(worker); err == nil { err = crontab.Start(ctx) }
Output:
type Job ¶
Job is a required handler of a job, it's called by worker. If an error is returned then call JobFailed.OnFailed() otherwise JobSucceed.OnSucceed() called.
Called sequence:
- OnBefore
- Do
- OnFailed / OnSucceed
- OnFinish
Code:
func (o *YourJob) OnRun(ctx context.Context) (err error){ // ... return }
type JobBefore ¶
JobBefore is an optional handler of a job, it's called by worker. If an error returned then quit other handlers otherwise Job.OnRun() called.
func (o *YourJob) OnBefore(ctx context.Context) (err error){ // ... return }
type JobFailed ¶
JobFailed is an optional handler of a job, it's called by worker.
func (o *YourJob) OnFailed(ctx context.Context){ // ... }
type JobFinish ¶
JobFinish is an optional handler of a job, it's called by worker.
func (o *YourJob) OnFinish(ctx context.Context){ // ... }
type JobSucceed ¶
JobSucceed is an optional handler of a job, it's called by worker.
func (o *YourJob) OnSucceed(ctx context.Context){ // ... }
type Strategy ¶
type Strategy string
Strategy is a type name used to define timed execution time for crontab like follows.
- "*/1 * * * * * *" (every second)
- "0 1/* * * * * *" (every minute)
- "0 0 1/* * * * *" (every hour)
const ( // EveryMinute // schedule once every 0 seconds per minute like follows. // // 1. 09:00:00 // 2. 09:01:00 EveryMinute Strategy = "0 */1 * * * * *" // EveryHour // schedule once every 0 seconds and 0 minutes per hour like follows. // // 1. 09:00:00 // 2. 10:00:00 EveryHour Strategy = "0 0 */1 * * * *" )
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker is a job manager that schedule in crontab. It's call job handlers in a goroutine.
func NewWorker ¶
NewWorker creates a worker with job.
Example ¶
worker := NewWorker(EveryMinute, &ej{}) println("class:", worker.class) println("strategy:", worker.strategy)
Output: class: gitee.com/go-libs/crontab/src.ej strategy: 0 */1 * * * * *
func (*Worker) GetStrategy ¶
GetStrategy gets worker strategy.
func (*Worker) SetGloballyUnique ¶
SetGloballyUnique skip if previous scheduling in any node is not completed and value of v is true.
func (*Worker) SetNodeUnique ¶
SetNodeUnique skip if previous scheduling is not completed and value of v is true.
func (*Worker) SetRunOnStartup ¶
SetRunOnStartup schedule the job once immediately when crontab starts if enabled.