package cronimport ()// JobWrapper decorates the given Job with some behavior.typeJobWrapperfunc(Job) Job// Chain is a sequence of JobWrappers that decorates submitted jobs with// cross-cutting behaviors like logging or synchronization.typeChainstruct { wrappers []JobWrapper}// NewChain returns a Chain consisting of the given JobWrappers.func ( ...JobWrapper) Chain {returnChain{}}// Then decorates the given job with all JobWrappers in the chain.//// This:// NewChain(m1, m2, m3).Then(job)// is equivalent to:// m1(m2(m3(job)))func ( Chain) ( Job) Job {for := range .wrappers { = .wrappers[len(.wrappers)--1]() }return}// Recover panics in wrapped jobs and log them with the provided logger.func ( Logger) JobWrapper {returnfunc( Job) Job {returnFuncJob(func() {deferfunc() {if := recover(); != nil {const = 64 << 10 := make([]byte, ) = [:runtime.Stack(, false)] , := .(error)if ! { = fmt.Errorf("%v", ) } .Error(, "panic", "stack", "...\n"+string()) } }() .Run() }) }}// DelayIfStillRunning serializes jobs, delaying subsequent runs until the// previous one is complete. Jobs running after a delay of more than a minute// have the delay logged at Info.func ( Logger) JobWrapper {returnfunc( Job) Job {varsync.MutexreturnFuncJob(func() { := time.Now() .Lock()defer .Unlock()if := time.Since(); > time.Minute { .Info("delay", "duration", ) } .Run() }) }}// SkipIfStillRunning skips an invocation of the Job if a previous invocation is// still running. It logs skips to the given logger at Info level.func ( Logger) JobWrapper {returnfunc( Job) Job {var = make(chanstruct{}, 1) <- struct{}{}returnFuncJob(func() {select {case := <-: .Run() <- default: .Info("skip") } }) }}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.