sidekiq

package
v1.5.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	DispatchInterval  = 30 * time.Second
	HeartbeatInterval = 1 * time.Minute
	StaleAfter        = 15 * time.Minute
	// MaxAttempts caps re-claims before a job is permanently failed, preventing poison-job loops.
	MaxAttempts = 5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(ctx context.Context, job tables.TableSidekiqJob, progress ProgressFunc) (finalMetadata string, err error)

HandlerFunc processes one job. Receives the job row (read Metadata for the resume cursor) and a progress callback. Returns final metadata and an error. Nil error completes the job; non-nil fails it. The context is cancelled if this node loses ownership or on shutdown.

type ProgressFunc

type ProgressFunc func(metadata string) error

ProgressFunc persists a checkpoint and bumps the heartbeat. Handlers call it after each unit of work.

type Runner

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

Runner owns the handler registry and job goroutine lifecycle.

func New

func New(store Store, logger schemas.Logger, maxConcurrent int, runnerID string) *Runner

New creates a Runner. maxConcurrent bounds simultaneous job goroutines (<=0 defaults to 4). Pass the node ID as runnerID in cluster mode. Pass "" to make any running job immediately re-claimable on restart (no stale window).

func (*Runner) Enqueue

func (r *Runner) Enqueue(ctx context.Context, id, kind, metadata, createdBy string) error

Enqueue persists a new pending job and starts it as soon as a concurrency slot is free. Returns once the DB row is committed so the caller can respond immediately.

func (*Runner) Register

func (r *Runner) Register(kind string, fn HandlerFunc)

Register binds a handler to a job kind. Call before enqueuing.

func (*Runner) Shutdown

func (r *Runner) Shutdown()

Shutdown cancels the background context and waits for in-flight goroutines to return.

func (*Runner) StartDispatcher

func (r *Runner) StartDispatcher(interval, staleAfter time.Duration) (stop func())

StartDispatcher scans for claimable jobs on an interval. Uses non-blocking semaphore acquisition so it never spawns more goroutines than available concurrency slots — remaining jobs are left for the next tick. Runs one scan immediately on start.

type Store

type Store interface {
	CreateSidekiqJob(ctx context.Context, job *tables.TableSidekiqJob) error
	GetSidekiqJob(ctx context.Context, id string) (*tables.TableSidekiqJob, error)
	// ClaimSidekiqJob atomically claims a job for runnerID; returns true only for the winner.
	ClaimSidekiqJob(ctx context.Context, id, runnerID string, staleBefore time.Time) (bool, error)
	// HeartbeatSidekiqJob bumps updated_at for a job still owned by runnerID; returns false on lost ownership.
	HeartbeatSidekiqJob(ctx context.Context, id, runnerID string) (bool, error)
	UpdateSidekiqJobProgress(ctx context.Context, id, runnerID, metadata string) error
	CompleteSidekiqJob(ctx context.Context, id, runnerID, metadata string) error
	FailSidekiqJob(ctx context.Context, id, runnerID, metadata, lastErr string) error
	// ListClaimableSidekiqJobs returns pending jobs and running jobs whose heartbeat is older than staleBefore.
	ListClaimableSidekiqJobs(ctx context.Context, staleBefore time.Time) ([]tables.TableSidekiqJob, error)
}

Store is the narrow subset of configstore the runner needs.

Jump to

Keyboard shortcuts

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