periodic

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EventStop indicates a stop event took place.
	EventStop = "stop"
	// EventKill indicates a kill event took place.
	EventKill = "kill"
	// EventTrigger indicates a trigger event took place.
	EventTrigger = "triggered"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func struct {
	// Task is the function that is executed on Run.
	Task func(context.Context)
	// TaskName is the name returned by Name,
	TaskName string
}

Func implements the Task interface.

func (Func) Name

func (f Func) Name() string

Name returns the task name.

func (Func) Run

func (f Func) Run(ctx context.Context)

Run runs the task function.

type Metrics

type Metrics struct {
	// Events tracks the amount of occurrences of Events defined above.
	Events func(string) metrics.Counter
	// Period is a Gauge describing the current Period.
	Period metrics.Gauge
	// Runtime tracks how long the task has been running.
	Runtime metrics.Gauge
	// StartTime is a timestamp of when the task was started.
	StartTime metrics.Gauge
}

Metrics contains the relevant metrics for a periodic task.

type Runner

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

Runner runs a task periodically.

func Start deprecated

func Start(task Task, period, timeout time.Duration) *Runner

Start creates and starts a new Runner to run the given task periodically. The timeout is used for the context timeout of the task. The timeout can be larger than the periodicity of the task. That means if a tasks takes a long time it will be immediately retriggered.

Deprecated: Start exists for compatibility reasons, use StartWithMetrics instead.

func StartWithMetrics

func StartWithMetrics(task Task, metric *Metrics, period, timeout time.Duration) *Runner

StartWithMetrics is identical to Start but allows the caller to specify the metric or no metric at all to be used.

func (*Runner) Kill

func (r *Runner) Kill()

Kill is like stop but it also cancels the context of the current running method.

func (*Runner) Stop

func (r *Runner) Stop()

Stop stops the periodic execution of the Runner. If the task is currently running this method will block until it is done.

func (*Runner) TriggerRun

func (r *Runner) TriggerRun()

TriggerRun triggers the periodic task to run now. This does not impact the normal periodicity of this task. That means if the periodicity is 5m and you call TriggerNow() after 2 minutes, the next execution will be in 3 minutes.

The method blocks until either the triggered run was started or the runner was stopped, in which case the triggered run will not be executed.

type Task

type Task interface {
	// Run executes the task once, it should return within the context's timeout.
	Run(context.Context)
	// Name returns the task's name for use in metrics and tracing. Each
	// successive call should return the same value as the first call. Names
	// must only contain [a-z] and _ characters.
	Name() string
}

A Task that has to be periodically executed.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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