job

package
v1.14.0-snapshot.4 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cell = cell.Provide(newRegistry)

Cell provides job.Registry which constructs job.Group-s. Job groups automate a lot of the logic involved with lifecycle management of goroutines within a Hive Cell. Providing a context that is canceled on shutdown and making sure multiple goroutines properly shutdown takes a lot of boilerplate. Job groups make it easy to queue, spawn, and collect jobs with minimal boilerplate. The registry maintains references to all groups which will allow us to add automatic metrics collection and/or status reporting in the future.

Functions

func NewTrigger

func NewTrigger() *trigger

NewTrigger creates a new trigger, which can be used to trigger a timer job.

func WithLogger

func WithLogger(logger logrus.FieldLogger) groupOpt

WithLogger replaces the default logger with the given logger, useful if you want to add certain fields to the logs created by the group/jobs.

func WithPprofLabels

func WithPprofLabels(pprofLabels pprof.LabelSet) groupOpt

WithPprofLabels adds pprof labels which will be added to the goroutines spawned for the jobs and thus included in the pprof profiles.

func WithRetry

func WithRetry(times int, backoff workqueue.RateLimiter) jobOneShotOpt

WithRetry option configures a one shot job to retry `times` amount of times. Each retry attempt the `backoff` ratelimiter is consulted to check how long the job should wait before making another attempt.

func WithShutdown

func WithShutdown() jobOneShotOpt

WithShutdown option configures a one shot job to shutdown the whole hive if the job returns an error. If the WithRetry option is also configured, all retries must be exhausted before we trigger the shutdown.

func WithTrigger

func WithTrigger(trig Trigger) timerOpt

WithTrigger option allows a user to specify a trigger, which if triggered will invoke the function of a timer before the configured interval has expired.

Types

type Group

type Group interface {
	Add(...Job)
	hive.HookInterface
}

Group aims to streamline the management of work within a cell. Group implements hive.HookInterface and takes care of proper start and stop behavior as expected by hive. A group allows you to add multiple types of jobs which different kinds of logic. No matter the job type, the function provided to is always called with a context which is bound to the lifecycle of the cell.

type Job

type Job interface {
	// contains filtered or unexported methods
}

Job in an interface that describes a unit of work which can be added to a Group. This interface contains unexported methods and thus can only be implemented by functions in this package such as OneShot, Timer, or Observer.

func Observer

func Observer[T any](name string, fn ObserverFunc[T], observable stream.Observable[T], opts ...observerOpt[T]) Job

AddObserver adds an observer job to the group. Observer jobs invoke the given `fn` for each item observed on `observable`. If the `observable` completes, the job stops. The context given to the observable is also canceled once the group stops.

func OneShot

func OneShot(name string, fn OneShotFunc, opts ...jobOneShotOpt) Job

OneShot creates a "One shot" job which can be added to a Group. The function passed to a one shot job is invoked once at startup. It can live for the entire lifetime of the group or exit early depending on its task. If it returns an error, it can optionally be retried if the WithRetry option. If retries are not configured or all retries failed as well, a shutdown of the hive can be triggered by specifying the WithShutdown option.

The given function is expected to exit as soon as the context given to it expires, this is especially important for blocking or long running jobs.

func Timer

func Timer(name string, fn TimerFunc, interval time.Duration, opts ...timerOpt) Job

Timer creates a timer job which can be added to a Group. Timer jobs invoke the given function at the specified interval. Timer jobs are particularly useful to implement periodic syncs and cleanup actions. Timer jobs can optionally be triggered by an external Trigger with the WithTrigger option. This trigger can for example be passed between cells or between jobs in the same cell to allow for an additional invocation of the function.

The interval between invocations is counted from the start of the last invocation. If the `fn` takes longer than the interval, its next invocation is not delayed. The `fn` is expected to stop as soon as the context passed to it expires. This is especially important for long running functions. The signal created by a Trigger is coalesced so multiple calls to trigger before the invocation takes place can result in just a single invocation.

type ObserverFunc

type ObserverFunc[T any] func(ctx context.Context, event T) error

ObserverFunc is the func type invoked by observer jobs. A ObserverFunc is expected to return as soon as ctx is canceled.

type OneShotFunc

type OneShotFunc func(ctx context.Context) error

OneShotFunc is the function type which is invoked by a one shot job. The given function is expected to exit as soon as the context given to it expires, this is especially important for blocking or long running jobs.

type Registry

type Registry interface {
	NewGroup(opts ...groupOpt) Group
}

A Registry creates Groups, it maintains references to these groups for the purposes of collecting information centralized like metrics.

type TimerFunc

type TimerFunc func(ctx context.Context) error

TimerFunc is the func type invoked by a timer job. A TimerFunc is expected to return as soon as the ctx expires.

type Trigger

type Trigger interface {
	Trigger()
	// contains filtered or unexported methods
}

Trigger which can be used to trigger a timer job, trigger events are coalesced.

Jump to

Keyboard shortcuts

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