Documentation
¶
Index ¶
- Variables
- func ProvideWorker(c *dig.Container, fn any) error
- func RunAllJobs(ctx context.Context, jobs ...Job) error
- func RunAllWorkers(ctx context.Context, workers ...Worker) error
- func RunProvidedWorkers(ctx context.Context, c *dig.Container) error
- func Wait(ctx context.Context, d time.Duration)
- type Backoff
- type DeclarativeWorker
- type ExponentialBackoff
- type Job
- type JobFunc
- type RepeatOption
- type StaticBackoff
- type Worker
- type WorkerConfiger
- type WorkerFunc
- type WorkerGroup
Constants ¶
This section is empty.
Variables ¶
var ErrWorkerExitedPrematurely = errors.New("worker exited prematurely")
WorkerExitedPrematurely indicates that a worker exited in RunAllWorkers while the context was not cancelled yet.
Functions ¶
func ProvideWorker ¶
ProvideWorker injects a WorkerConfiger, which can later be started with RunProvidedWorkers.
func RunAllJobs ¶
RunAllJobs runs all jobs in parallel and return their errors.
func RunAllWorkers ¶
RunAllWorkers starts all workers in goroutines and waits until all are exited.
Behaviour:
- The execution for all workers get cancelled when the first worker exists, regardless of the exit code.
- Err is nil, if the context gets cancelled and the workers return a nil error too.
- Err contains [WorkerExitedPrematurely], if the workers return a nil error while the context was not cancelled.
- Err contains all errors, returned by the workers.
func RunProvidedWorkers ¶
RunProvidedWorkers starts all workers there were injected using RunAllWorkers.
Types ¶
type Backoff ¶
Backoff is an interface to calculate the wait times between attemts of doing a task. The first attempt must always return 0s. The Duration function can be used together with the Wait function for a cancelable backoff sleep.
type DeclarativeWorker ¶
DeclarativeWorker is an alternative to building the worker behaviour with chained functions.If automatically chains worker functions based on defined field in the most sensful order.
It satisfies the Worker interface for easier use.
type ExponentialBackoff ¶
ExponentialBackoff is a typical exponentail backoff with Jitter, based on this blog post: https://aws.amazon.com/ru/blogs/architecture/exponential-backoff-and-jitter/
type RepeatOption ¶
type RepeatOption func(*jobWorker)
func WithStartImmediately ¶
func WithStartImmediately() RepeatOption
type StaticBackoff ¶
StaticBackoff always returns the same sleep duration to any but the 0th attempt.
type Worker ¶
Worker is a service that is supposed to run continuously until the context gets cancelled.
func NamedWorker ¶
NamedWorker assigns a new logutil subsystem on startup. See logutil.Start.
func NamedWorkerFromType ¶
NamedWorkerFromType assigns a new logutil subsystem on startup based on the provided type name. See logutil.Start.
func Repeat ¶
func Repeat(wait time.Duration, job Job, opts ...RepeatOption) Worker
Repeat reruns a job indefinitely until the context gets cancelled. The job will run at most once in the given time interval. This means the wait duration is not the sleep between executions, but the time between the start of runs (based on time.Ticker).
type WorkerConfiger ¶
type WorkerConfiger interface {
Workers() []Worker
}
WorkerConfiger is for Workers that configure themselfes. This means they can define repeats, backoff and jitter themselves.
func (w *CommitFetcher) Workers() []runutil.Worker { return []runutil.Worker{ runutil.DeclarativeWorker{ Name: "Commits", Worker: runutil.Repeat(5*time.Second, runutil.JobFunc(w.fetchCommits)), Retry: runutil.ExponentialBackoff{ Initial: time.Second, Max: time.Minute, JitterProportion: 0.5, }, }, runutil.DeclarativeWorker{ Name: "PRs", Worker: runutil.Repeat(5*time.Second, runutil.JobFunc(w.fetchPRs)), Retry: runutil.ExponentialBackoff{ Initial: time.Second, Max: time.Minute, JitterProportion: 0.5, }, }, } }
type WorkerFunc ¶
WorkerFunc is a helper to cast a function directly to a Worker.
type WorkerGroup ¶
type WorkerGroup struct { dig.In All []WorkerConfiger `group:"worker"` }
WorkerGroup is a input parameter struct for Dig to retrieve all instances that implement the WorkerConfigerer.