jobs

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const Schema = `` /* 430-byte string literal not displayed */

Schema bootstraps the jobs table for ephemeral tests only. Production deployments must apply jobs.Migrations() for upgrade-safe evolution.

Variables

View Source
var (
	ErrInvalidRegistration = errors.New("jobs: invalid handler registration")
	ErrHandlerRegistered   = errors.New("jobs: handler already registered")
	ErrHandlerNotFound     = errors.New("jobs: handler not found")
)
View Source
var ErrFailedJobNotFound = errors.New("jobs: permanently failed job not found")

ErrFailedJobNotFound indicates that no permanently failed job matched the requested operation.

Functions

func Enqueue

func Enqueue(ctx context.Context, tx pgx.Tx, job Job) error

Enqueue inserts a job into the devengine_jobs table. It must be called within a transaction.

func Migrations

func Migrations() []migrate.Source

Migrations returns the migrations required by the persistent delayed jobs queue. It does not install Outbox infrastructure.

Types

type FailedJob

type FailedJob struct {
	ID          string
	Name        string
	Payload     json.RawMessage
	RunAt       time.Time
	Attempt     int
	MaxAttempts int
	LastError   string
	CreatedAt   time.Time
}

FailedJob is a permanently failed job available for operational inspection.

type Handler

type Handler interface {
	Handle(ctx context.Context, payload []byte) error
}

Handler is an interface for processing jobs.

type HandlerFunc

type HandlerFunc func(ctx context.Context, payload []byte) error

HandlerFunc allows using a function as a Handler.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, payload []byte) error

type Job

type Job struct {
	ID          string
	Name        string
	Payload     any
	RunAt       time.Time
	MaxAttempts int
}

Job defines a task to be executed by a worker.

type Registry

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

Registry stores the handlers for different job types.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new Registry.

func (*Registry) HandlerFor

func (r *Registry) HandlerFor(name string) Handler

HandlerFor returns the handler registered for the given name.

func (*Registry) Register

func (r *Registry) Register(name string, h Handler) error

Register associates a name with a handler. It rejects invalid and duplicate registrations so startup configuration cannot be overwritten silently.

func (*Registry) Replace

func (r *Registry) Replace(name string, h Handler) error

Replace explicitly replaces an existing handler.

type Repository

type Repository struct {
	Pool *pgxpool.Pool
}

Repository provides PostgreSQL-native administrative operations for jobs.

func NewRepository

func NewRepository(pool *pgxpool.Pool) *Repository

func (*Repository) CountFailed

func (r *Repository) CountFailed(ctx context.Context) (int64, error)

CountFailed returns the number of permanently failed jobs.

func (*Repository) DiscardFailed

func (r *Repository) DiscardFailed(ctx context.Context, id string) error

DiscardFailed permanently deletes a failed job.

func (*Repository) GetFailed

func (r *Repository) GetFailed(ctx context.Context, id string) (FailedJob, error)

GetFailed returns one permanently failed job for inspection.

func (*Repository) ListFailed

func (r *Repository) ListFailed(ctx context.Context, limit int) ([]FailedJob, error)

ListFailed returns permanently failed jobs ordered from oldest to newest. A non-positive limit defaults to 100.

func (*Repository) RequeueFailed

func (r *Repository) RequeueFailed(ctx context.Context, id string, runAt time.Time) error

RequeueFailed makes a permanently failed job eligible for delivery again. It resets its attempt history and schedules it at runAt; a zero runAt means now.

type Worker

type Worker struct {
	Pool     *pgxpool.Pool
	Registry *Registry
	Logger   *slog.Logger
	Config   WorkerConfig
	Tracer   telemetry.Tracer
	Meter    telemetry.Meter
}

Worker is a background worker that polls the jobs table. Jobs use leased claims: active handlers renew their lease, and only the holder of the current claim token can renew or persist an outcome. Handlers must remain idempotent because delivery remains at-least-once (for example after a process crash or a lost lease).

func (*Worker) Name

func (w *Worker) Name() string

Name implements engine.Worker.

func (*Worker) Run

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

Run starts the polling loop.

type WorkerConfig

type WorkerConfig struct {
	BatchSize      int
	PollInterval   time.Duration
	InitialBackoff time.Duration
	// LeaseDuration is the maximum time a worker owns a claimed job.
	LeaseDuration time.Duration
	// LeaseRenewalInterval controls how often an active handler renews its
	// lease. Zero renews at half LeaseDuration. Values at or above the lease
	// duration are clamped to half the lease to prevent expiry before renewal.
	LeaseRenewalInterval time.Duration
}

WorkerConfig configures the jobs Worker.

Jump to

Keyboard shortcuts

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