worker

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, stream <-chan Job, opts ...Option) error

Run starts a worker and runs until context is cancelled or stream returns io.EOF.

Types

type Job

type Job struct {
	ID      string          // unique identifier for the job.
	Ack     func(err error) // acknowledge callback when message is processed.
	Time    time.Time       // time at which this job was created.
	Error   error           // error during processing if any.
	Payload interface{}     // payload of the message.
}

Job represents the job request to the worker.

func (*Job) EnsureValid

func (j *Job) EnsureValid() error

EnsureValid sets defaults for unset fields where possible and validates the job definition.

func (Job) String

func (j Job) String() string

type Option

type Option func(ws *workerSession) error

Option can be provided to Run() to customise run behaviour of the worker.

func WithLogger

func WithLogger(lg log.Logger) Option

WithLogger sets the logger to be used by worker. If nil, logging is disabled.

func WithProc

func WithProc(proc Proc, workerCount int) Option

WithProc sets the Proc (processor) to be invoked for each Job.

type Proc

type Proc interface {
	Exec(ctx context.Context, job Job) error
}

Proc represents the processor to be applied on each Job by the worker.

type ProcFn

type ProcFn func(ctx context.Context, job Job) error

ProcFn is a adaptor type to implement Proc using simple Go func values.

func (ProcFn) Exec

func (pFn ProcFn) Exec(ctx context.Context, job Job) error

type Stream

type Stream func(ctx context.Context) (<-chan Job, error)

func StreamFn

func StreamFn(buffer int, fn func(ctx context.Context) (*Job, error)) Stream

StreamFn turns a simple Go func into a Stream by invoking it in an infinite loop running on an independent GoRoutine. Stream exits on first error from the 'fn'.

Jump to

Keyboard shortcuts

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