worker

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package worker is the durable-queue claim-loop: it pulls jobs from a ports.JobQueue, dispatches each to a Handler registered by Kind, heartbeats long runs so their lease does not expire mid-flight, and Completes or Fails (with backoff) the job. It is the process body of synapse-worker, and reusable in-process. It owns no business logic – the handlers (recon/SCA) carry the same gate/audit/ evidence invariants as the synchronous path.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Visibility  time.Duration // lease per claim
	Poll        time.Duration // idle sleep when the queue is empty
	Heartbeat   time.Duration // lease-extension interval for an in-flight job
	Backoff     time.Duration // base requeue delay on failure
	MaxAttempts int           // give up (dead-letter) after this many deliveries
}

Config tunes the loop; zero values fall back to sane defaults.

type DeadLetterer

type DeadLetterer interface {
	OnDeadLetter(ctx context.Context, job ports.QueuedJob, cause error) error
}

DeadLetterer is an optional capability a Handler may implement. When the worker is about to dead-letter a job (terminal failure after MaxAttempts), it calls OnDeadLetter FIRST so the handler can drive its backing domain entity (agent session / recon run) to a terminal state. Without it, a reconciler that keys on the ENTITY's status – not the job's – re-enqueues the stranded entity forever (the dead-letter → re-drive livelock), and the job/entity states permanently disagree. Best-effort: an OnDeadLetter error is logged, never blocking the dead-letter itself. cause is the last handler error that exhausted the retries.

type Enqueuer

type Enqueuer interface {
	Enqueue(ctx context.Context, kind string, payload []byte) (string, error)
}

Enqueuer is the write side a use case uses to defer work to the worker. It is the subset of ports.JobQueue producers need.

type Handler

type Handler interface {
	Handle(ctx context.Context, job ports.QueuedJob) error
}

Handler processes one claimed job. A nil error means success (the job is Completed); any error requeues the job with backoff until MaxAttempts is reached. Handlers must be IDEMPOTENT: at-least-once delivery means a job can run more than once (e.g. after a crash mid-run), and recon hits real hosts.

type HandlerFunc

type HandlerFunc func(ctx context.Context, job ports.QueuedJob) error

HandlerFunc adapts a function to Handler.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, job ports.QueuedJob) error

Handle calls f.

type Worker

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

Worker runs the claim/dispatch/complete loop over a JobQueue.

func New

func New(queue ports.JobQueue, handlers map[string]Handler, cfg Config, log *slog.Logger) *Worker

New builds a worker. handlers maps job Kind → Handler.

func (*Worker) Run

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

Run claims and processes jobs until ctx is cancelled (graceful shutdown drains the current job, then returns). It never returns on a transient queue error – it logs and keeps polling – so a brief DB blip doesn't kill the worker.

Jump to

Keyboard shortcuts

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