Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultManager = NewManager(time.Local)
DefaultManager is the default task manage of this package. Users of the DefaultManager must make sure that no unwanted tasks are registered automatically by the init() functions of other packages. For safety, users are adviced to use NewManager on their own and register all required tasks manually. Creators of tasks are adivced to export a RegisterOn(mng *Manager) method that can be used to register a task on a certain manager.
Functions ¶
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages and schedules tasks register on it. The manager must be started for any tasks to get scheduled. Once started tasks are scheduled as long as the manager is not stopped.
func NewManager ¶
NewManager returns a new task manager that is configured to interpret cron schedule as at the timezone loc. If loc is nil then time.UTC will be used.
func (*Manager) Register ¶
Register registers task at mng. An error can only be returned when the Schedule field of task cannot be parsed. If StartNow is set to true on task it will be executed as soon as the manager is started. If mng has already been started and StartNow is true the task is executed immediately in a dedicated go-routine.
func (*Manager) Start ¶
Start starts the manager and the cron scheduler. Tasks that are marked as StartNow are executed immediately in a dedicated goroutine.
func (*Manager) Stop ¶
func (mng *Manager) Stop()
Stop stops the manager and waits for all running tasks to complete. Running tasks are NOT cancelled! If the user wants to cancel all tasks the context passed to mng.Start() should be cancelled instead. Afterwards a call to Stop() will make sure that no more tasks get scheduled and will wait for all running tasks to finish.
type Task ¶
type Task struct { // Name is the name of the task. Name string // TaskFunc is the actual function that should be executed // when the task runs. TaskFunc TaskFunc // Deadline can be set to the maximum duration the task is // allowed to run. The context passed to TaskFunc is cancelled // with the deadline. Deadline time.Duration // StartNow can be set to true if the task should run immediately // when the task manager is started. StartNow bool // Schedule holds the cron schedule at which the task should be // executed. Schedule string // contains filtered or unexported fields }
Task is a workload that runs async on a given cron schedule. The same task is guaranteed to not run twice.
func (*Task) LastResult ¶
LastResult returns time and the error of the last execution. If the task has not yet been executed a zero time is returned together with a nil error.
Notes ¶
Bugs ¶
see bug in Manager.Register(*Task)
right now it is possible for the task to run twice at the same time because the cron-schedule might also execute the task the next moment.