task

package
v6.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSkip = fmt.Errorf("skip execution of task function")

ErrSkip is a special error that may be returned by a Schedule function to mean to skip a particular execution of the task function, and just wait the returned interval before re-evaluating.

View Source
var SkipFirst = func(every *every) { every.skipFirst = true }

SkipFirst is an option for the Every schedule that will make the schedule skip the very first invokation of the task function.

Functions

func Start

func Start(ctx context.Context, f Func, schedule Schedule) (func(time.Duration) error, func())

Start a single task executing the given function with the given schedule.

This is a convenience around Group and it returns two functions that can be used to control the task. The first is a "stop" function trying to terminate the task gracefully within the given timeout and the second is a "reset" function to reset the task's state. See Group.Stop() and Group.Reset() for more details.

Types

type EveryOption

type EveryOption func(*every)

EveryOption captures a tweak that can be applied to the Every schedule.

type Func

type Func func(context.Context)

Func captures the signature of a function executable by a Task.

When the given context is done, the function must gracefully terminate whatever logic it's executing.

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group of tasks sharing the same lifecycle.

All tasks in a group will be started and stopped at the same time.

func (*Group) Add

func (g *Group) Add(f Func, schedule Schedule) *Task

Add a new task to the group, returning its index.

func (*Group) Start

func (g *Group) Start(ctx context.Context)

Start all the tasks in the group.

func (*Group) Stop

func (g *Group) Stop(timeout time.Duration) error

Stop all tasks in the group.

This works by sending a cancellation signal to all tasks of the group and waiting for them to terminate.

If a task is idle (i.e. not executing its task function) it will terminate immediately.

If a task is busy executing its task function, the cancellation signal will propagate through the context passed to it, and the task will block waiting for the function to terminate.

In case the given timeout expires before all tasks complete, this method exits immediately and returns an error, otherwise it returns nil.

type Schedule

type Schedule func() (time.Duration, error)

Schedule captures the signature of a schedule function.

It should return the amount of time to wait before triggering the next execution of a task function.

If it returns zero, the function does not get run at all.

If it returns a duration greater than zero, the task function gets run once immediately and then again after the specified amount of time. At that point the Task re-invokes the schedule function and repeats the same logic.

If ErrSkip is returned, the immediate execution of the task function gets skipped, and it will only be possibly executed after the returned interval.

If any other error is returned, the task won't execute the function, however if the returned interval is greater than zero it will re-try to run the schedule function after that amount of time.

func Daily

func Daily(options ...EveryOption) Schedule

Daily is a convenience for creating a schedule that runs once a day.

func Every

func Every(interval time.Duration, options ...EveryOption) Schedule

Every returns a Schedule that always returns the given time interval.

func Hourly

func Hourly(options ...EveryOption) Schedule

Hourly is a convenience for creating a schedule that runs once an hour.

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task executes a certain function periodically, according to a certain schedule.

func (*Task) Reset

func (t *Task) Reset()

Reset the state of the task as if it had just been started.

This is handy if the schedule logic has changed, since the schedule function will be invoked immediately to determine whether and when to run the task function again.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL