go-sarah: github.com/oklahomer/go-sarah/workers Index | Files

package workers

import "github.com/oklahomer/go-sarah/workers"

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

Index

Package Files

worker.go

Variables

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")
)

type Config Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

Worker is an interface that all Worker implementation must satisfy. Worker implementation can be fed to sarah.RegisterWorker() to replace default implementation as below. Given worker is used on sarah.Run() call.

myWorker := NewMyWorkerImpl()
sarah.RegisterWorker(myWorker)

func Run Uses

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 Uses

type WorkerOption func(*worker)

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

func WithReporter Uses

func WithReporter(reporter Reporter) WorkerOption

WithReporter creates and returns WorkerOption to set preferred Reporter implementation.

Package workers imports 7 packages (graph). Updated 2020-09-27. Refresh now. Tools for package owners.