// Job encapsulates logic for a scheduled job to be run according
// to a set Timing, executing the job with a set panic handler, and
// holding onto a next execution time safely in a concurrent environment.
typeJobstruct{
iduint64
nextatomics.Time
timingTiming
callfunc(time.Time)
panicfunc(interface{})
}
// NewJob returns a new Job to run given function.
funcNewJob(fnfunc(nowtime.Time))*Job{
iffn==nil{
// Ensure a function
panic("nil func")
}
j:=&Job{// set defaults
timing:emptytiming,// i.e. fire immediately
call:fn,
panic:func(iinterface{}){panic(i)},
}
// Init next time ptr
j.next.Store(zerotime)
returnj
}
// At sets this Job to execute at time, by passing (*sched.Once)(&at) to .With(). See .With() for details.
func(job*Job)At(attime.Time)*Job{
returnjob.With((*Once)(&at))
}
// Every sets this Job to execute every period, by passing sched.Period(period) to .With(). See .With() for details.
func(job*Job)Every(periodtime.Duration)*Job{
returnjob.With(Periodic(period))
}
// EveryAt sets this Job to execute every period starting at time, by passing &PeriodicAt{once: Once(at), period: Periodic(period)} to .With(). See .With() for details.