workers

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package workers provides general purpose worker mechanism that outputs stacktrace when given job panics.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEnqueueAfterWorkerShutdown is returned when job is given after worker context cancellation.
	ErrEnqueueAfterWorkerShutdown = errors.New("job can not be enqueued after worker shutdown")

	// ErrQueueOverflow is returned when job is given, but all workers are busy and queue is full.
	ErrQueueOverflow = errors.New("queue is full")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	WorkerNum         uint          `json:"worker_num" yaml:"worker_num"`
	QueueSize         uint          `json:"queue_size" yaml:"queue_size"`
	SuperviseInterval time.Duration `json:"supervise_interval" yaml:"supervise_interval"`
}

Config contains some configuration variables. Use NewConfig to construct Config instance with default value and feed the instance to json.Unmarshal or yaml.Unmarshal to override.

func NewConfig

func NewConfig() *Config

NewConfig returns Config instance with default configuration values. To override with desired value, pass the returned instance to json.Unmarshal or yaml.Unmarshal.

type Reporter

type Reporter interface {
	Report(context.Context, *Stats)
}

Reporter is an interface to report statistics such as queue length to outer service. Implement this to pass statistical variables in desired way. e.g. Report stats to prometheus via exporter

type Stats added in v1.3.0

type Stats struct {
	// QueueSize is the size of queued task to work.
	// Use this value to adjust Config.QueueSize.
	QueueSize int
}

Stats represents a group of statistical data. This can be passed to Reporter.Report() to report current state.

type Worker

type Worker interface {
	Enqueue(func()) error
}

Worker is an interface that all Worker implementation must satisfy. Worker implementation can be fed to sarah.Runner via sarah.RunnerOption as below.

myWorker := NewMyWorkerImpl()
option := sarah.WithWorker(myWorker)

runner, _ := sarah.NewRunner(sarah.NewConfig(), option)

func Run

func Run(ctx context.Context, config *Config, options ...WorkerOption) (Worker, error)

Run creates as many child workers as specified by *Config and start them. When Run completes, Worker is returned so jobs can be enqueued. Multiple calls to Run() creates multiple Worker with separate context, queue and child workers.

type WorkerOption

type WorkerOption func(*worker) error

WorkerOption defines function that worker's functional option must satisfy.

func WithReporter

func WithReporter(reporter Reporter) WorkerOption

WithReporter creates and returns WorkerOption to set preferred Reporter implementation.

Jump to

Keyboard shortcuts

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