task

package module
v0.0.0-...-88f1b86 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: LGPL-3.0 Imports: 6 Imported by: 4

README

task

Task management

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBackoff = errors.Errorf("skip execution of task function")

ErrBackoff is a special error that may be returned by a Schedule function to mean backoff the interval, before re-evaluating the interval of the next execution.

View Source
var ErrSkip = errors.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 ErrTerminate = errors.Errorf("terminate the task")

ErrTerminate is a special error that may be returned by a task function to terminate the task.

View Source
var Linear = func(backoff BackoffOptions) {
	backoff.SetBackoff(func(n int, t time.Duration) time.Duration {
		return t * time.Duration(n)
	})
}

Linear describes a backoff function that grows linearly with time.

View Source
var SkipFirst = func(every EveryOptions) { every.SetSkipFirst(true) }

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

Functions

func Start

func Start(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 BackoffFunc

type BackoffFunc func(n int, t time.Duration) time.Duration

BackoffFunc is a type that represents a way to describe what the interval time should be for each backoff request. The amount is used to say how many iterations have occurred since the backoff was requested.

type BackoffOption

type BackoffOption func(BackoffOptions)

BackoffOption captures a tweak that can be applied to the Backoff schedule.

type BackoffOptions

type BackoffOptions interface {
	SetBackoff(BackoffFunc)
}

BackoffOptions represents a way to set optional values to a backoff option. The BackoffOptions shows what options are available to change.

type Clock

type Clock interface {
	// After waits for the duration to elapse and then sends the current time
	// on the returned channel.
	// It is equivalent to NewTimer(d).C.
	// The underlying Timer is not recovered by the garbage collector
	// until the timer fires. If efficiency is a concern, use NewTimer
	// instead and call Timer.Stop if the timer is no longer needed.
	After(d time.Duration) <-chan time.Time
}

Clock represents the passage of time in a way that can be faked out for tests.

type EveryOption

type EveryOption func(EveryOptions)

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

type EveryOptions

type EveryOptions interface {
	SetSkipFirst(bool)
}

EveryOptions represents a way to set optional values to a every option. The EveryOptions shows what options are available to change.

type Func

type Func func(context.Context) error

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 NewGroup

func NewGroup() *Group

NewGroup creates a Group with sane defaults.

func (*Group) Add

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

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

func (*Group) Kill

func (g *Group) Kill() error

Kill asks the group to stop and returns immediately.

func (*Group) Len

func (g *Group) Len() int

Len returns the number of tasks with in the group.

func (*Group) Start

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

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(err error) (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 Backoff

func Backoff(interval time.Duration, options ...BackoffOption) Schedule

Backoff returns a Schedule that will attempt to backoff if there is a error passed.

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.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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