Documentation
¶
Overview ¶
Package task supplies some task runners to run the task.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type IntervalRunnerConfig ¶
type IntervalRunnerConfig struct { // The default interval that the task is run between twice. // // Default: 1m Interval time.Duration // Jitter is used to produce a random duration, [0, Jitter], // to be added to the interval duration of the task to avoid // converging on periodic behavior. If 0, disalbe it. // // Default: 0 Jitter time.Duration // The clock tick interval. // // Default: 5s Tick time.Duration // Report whether the tasks can be run or not. // If nil, it is equal to return true. // // Default: nil CanRun func() bool // ErrorLog is used to log the error returned by the task, if set. // // Default: log.Printf ErrorLog func(fmt string, args ...interface{}) }
IntervalRunnerConfig is used to configure the interval runner.
type IntervalTask ¶
IntervalTask is the interval task to be run.
func NewIntervalFuncTask ¶
func NewIntervalFuncTask(name string, interval time.Duration, run TaskFunc) IntervalTask
NewIntervalFuncTask is equal to NewIntervalTask(interval, NewTask(name, run)).
func NewIntervalTask ¶
func NewIntervalTask(interval time.Duration, task Task) IntervalTask
NewIntervalTask returns a new interval task.
type Runner ¶
Runner is a task runner.
func NewIntervalRunner ¶
func NewIntervalRunner(config *IntervalRunnerConfig) Runner
NewIntervalRunner returns a new runner to run the tasks at regular intervals.
Example ¶
package main import ( "context" "fmt" "time" ) func newTask(name string) Task { return NewTask(name, func(ctx context.Context, now time.Time) error { fmt.Printf("%s: run task '%s'\n", now.UTC().Format(time.RFC3339), name) return nil }) } func main() { runner := NewIntervalRunner(&IntervalRunnerConfig{ Interval: time.Second * 3, Tick: time.Second, }) runner.AddTask(newTask("task1")) runner.AddTask(newTask("task2")) runner.AddTask(NewIntervalTask(time.Second*5, newTask("task3"))) runner.AddTask(NewIntervalTask(time.Second*10, newTask("task4"))) time.Sleep(time.Second * 25) runner.Stop() // It will output like this: // 2021-06-03T14:56:36Z: run task 'task3' // 2021-06-03T14:56:36Z: run task 'task4' // 2021-06-03T14:56:36Z: run task 'task1' // 2021-06-03T14:56:36Z: run task 'task2' // 2021-06-03T14:56:39Z: run task 'task2' // 2021-06-03T14:56:39Z: run task 'task1' // 2021-06-03T14:56:42Z: run task 'task2' // 2021-06-03T14:56:42Z: run task 'task3' // 2021-06-03T14:56:42Z: run task 'task1' // 2021-06-03T14:56:46Z: run task 'task1' // 2021-06-03T14:56:46Z: run task 'task4' // 2021-06-03T14:56:46Z: run task 'task2' // 2021-06-03T14:56:48Z: run task 'task3' // 2021-06-03T14:56:49Z: run task 'task1' // 2021-06-03T14:56:49Z: run task 'task2' // 2021-06-03T14:56:53Z: run task 'task2' // 2021-06-03T14:56:53Z: run task 'task3' // 2021-06-03T14:56:53Z: run task 'task1' // 2021-06-03T14:56:56Z: run task 'task4' // 2021-06-03T14:56:56Z: run task 'task1' // 2021-06-03T14:56:56Z: run task 'task2' // 2021-06-03T14:56:58Z: run task 'task3' }
Output:
type Task ¶
Task represents a task.
func GetTaskFromCtx ¶
GetTaskFromCtx returns the task from the context.
Click to show internal directories.
Click to hide internal directories.