worker

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// WorkerStatusRunning means the worker is actively consuming deliveries.
	WorkerStatusRunning = "running"
	// WorkerStatusPaused means consumption is paused but worker goroutines remain active.
	WorkerStatusPaused = "paused"
	// WorkerStatusStopped means the worker has been stopped.
	WorkerStatusStopped = "stopped"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffConfig

type BackoffConfig struct {
	Strategy    BackoffStrategy
	Interval    time.Duration
	MaxInterval time.Duration
	Jitter      bool
}

BackoffConfig configures retry delays.

type BackoffStrategy

type BackoffStrategy string

BackoffStrategy controls the retry delay profile.

const (
	BackoffNone        BackoffStrategy = "none"
	BackoffFixed       BackoffStrategy = "fixed"
	BackoffExponential BackoffStrategy = "exponential"
)

type CommanderFactory

type CommanderFactory func(job.Task) *job.TaskCommander

CommanderFactory builds a TaskCommander for a task.

type Correlation added in v0.15.0

type Correlation struct {
	MachineID       string
	EntityID        string
	ExecutionID     string
	ExpectedState   string
	ExpectedVersion int64
	ResumeEvent     string
}

Correlation captures canonical FSM metadata for observability hooks.

type DefaultRetryPolicy

type DefaultRetryPolicy struct {
	MaxAttempts int
	Backoff     BackoffConfig
}

DefaultRetryPolicy provides simple max-attempts with backoff scheduling. MaxAttempts <= 0 means no retries (dead-letter on first failure).

func (DefaultRetryPolicy) Decide

func (p DefaultRetryPolicy) Decide(attempt int, err error) queue.NackOptions

Decide returns nack options based on attempts and policy.

type Entry

type Entry struct {
	Task      job.Task
	Commander *job.TaskCommander
}

Entry stores a registered task and its commander.

type Event

type Event struct {
	Delivery    queue.Delivery
	Message     *job.ExecutionMessage
	Task        job.Task
	Correlation Correlation
	Attempt     int
	Delay       time.Duration
	Err         error
	StartedAt   time.Time
	Duration    time.Duration
}

Event captures worker lifecycle details for hooks.

type Hook

type Hook interface {
	OnStart(ctx context.Context, event Event)
	OnSuccess(ctx context.Context, event Event)
	OnFailure(ctx context.Context, event Event)
	OnRetry(ctx context.Context, event Event)
}

Hook exposes lifecycle callbacks for worker execution.

type HookFuncs

type HookFuncs struct {
	OnStartFunc   func(context.Context, Event)
	OnSuccessFunc func(context.Context, Event)
	OnFailureFunc func(context.Context, Event)
	OnRetryFunc   func(context.Context, Event)
}

HookFuncs provides a function-based hook implementation.

func (HookFuncs) OnFailure

func (h HookFuncs) OnFailure(ctx context.Context, event Event)

func (HookFuncs) OnRetry

func (h HookFuncs) OnRetry(ctx context.Context, event Event)

func (HookFuncs) OnStart

func (h HookFuncs) OnStart(ctx context.Context, event Event)

func (HookFuncs) OnSuccess

func (h HookFuncs) OnSuccess(ctx context.Context, event Event)

type Option

type Option func(*Worker)

Option configures a Worker instance.

func WithCancelKeyExtractor

func WithCancelKeyExtractor(fn func(*job.ExecutionMessage) string) Option

WithCancelKeyExtractor sets the key extractor for cancellation lookups.

func WithCancelPollInterval

func WithCancelPollInterval(interval time.Duration) Option

WithCancelPollInterval sets the polling interval for cancellation checks.

func WithCancellationStore

func WithCancellationStore(store cancellation.Store) Option

WithCancellationStore configures a distributed cancellation store.

func WithCommanderFactory

func WithCommanderFactory(factory CommanderFactory) Option

WithCommanderFactory overrides the TaskCommander construction.

func WithConcurrency

func WithConcurrency(concurrency int) Option

WithConcurrency sets the number of worker goroutines.

func WithHooks

func WithHooks(hooks ...Hook) Option

WithHooks adds lifecycle hooks.

func WithIdempotencyStore added in v0.15.0

func WithIdempotencyStore(store qidempotency.Store, ttl time.Duration) Option

WithIdempotencyStore configures distributed deduplication across workers.

func WithIdleDelay

func WithIdleDelay(delay time.Duration) Option

WithIdleDelay controls the delay between dequeue attempts when idle.

func WithLeaseExtensionTTL added in v0.15.0

func WithLeaseExtensionTTL(ttl time.Duration) Option

WithLeaseExtensionTTL sets the lease extension duration used by heartbeats.

func WithLeaseHeartbeatInterval added in v0.15.0

func WithLeaseHeartbeatInterval(interval time.Duration) Option

WithLeaseHeartbeatInterval sets the cadence for lease renewal heartbeats.

func WithLogger

func WithLogger(logger job.Logger) Option

WithLogger injects a logger for worker events.

func WithRegistry

func WithRegistry(registry *Registry) Option

WithRegistry supplies a task registry.

func WithRetryPolicy

func WithRetryPolicy(policy RetryPolicy) Option

WithRetryPolicy sets the retry policy for nacks.

func WithShutdownHook

func WithShutdownHook(hook ShutdownHook) Option

WithShutdownHook registers a shutdown hook.

func WithTaskCommanderRetries

func WithTaskCommanderRetries(enabled bool) Option

WithTaskCommanderRetries enables or disables TaskCommander retries.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry stores tasks by job_id and validates registrations.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty task registry.

func (*Registry) Add

func (r *Registry) Add(task job.Task, commander *job.TaskCommander) error

Add registers a task in the registry.

func (*Registry) Get

func (r *Registry) Get(id string) (Entry, bool)

Get retrieves a registered task entry.

func (*Registry) List

func (r *Registry) List() []job.Task

List returns registered tasks.

type RetryPolicy

type RetryPolicy interface {
	Decide(attempt int, err error) queue.NackOptions
}

RetryPolicy decides how the worker should nack a failed delivery.

type ShutdownHook

type ShutdownHook func(context.Context) error

ShutdownHook runs during worker shutdown.

type Status added in v0.15.0

type Status struct {
	Status      string
	Running     bool
	Paused      bool
	Concurrency int
	Registered  int
	StartedAt   time.Time
	StoppedAt   time.Time
}

Status reports worker control/runtime state for orchestration surfaces.

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker consumes queue deliveries and dispatches tasks.

func NewWorker

func NewWorker(dequeuer queue.Dequeuer, opts ...Option) *Worker

NewWorker builds a worker with default settings.

func (*Worker) Pause added in v0.15.0

func (w *Worker) Pause() error

Pause temporarily halts delivery consumption without stopping worker goroutines.

func (*Worker) Register

func (w *Worker) Register(task job.Task) error

Register adds a task to the worker registry.

func (*Worker) RegisterAll

func (w *Worker) RegisterAll(tasks []job.Task) error

RegisterAll adds multiple tasks.

func (*Worker) RegisteredTasks

func (w *Worker) RegisteredTasks() []job.Task

RegisteredTasks returns the current task list.

func (*Worker) Resume added in v0.15.0

func (w *Worker) Resume() error

Resume restarts delivery consumption after Pause.

func (*Worker) Start

func (w *Worker) Start(ctx context.Context) error

Start begins consuming messages using configured concurrency.

func (*Worker) Status added in v0.15.0

func (w *Worker) Status() Status

Status returns current worker execution control state.

func (*Worker) Stop

func (w *Worker) Stop(ctx context.Context) error

Stop cancels active workers and waits for completion.

Jump to

Keyboard shortcuts

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