scheduler

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package scheduler provides a small retry-aware job runner that can be embedded in services needing persisted scheduling semantics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AttemptUpdate

type AttemptUpdate struct {
	Status            string
	ProviderMessageID string
	RetryCount        int
	LastAttemptedAt   time.Time
}

AttemptUpdate describes the mutation that must be persisted after a dispatch attempt.

type ClaimingRepository added in v0.2.1

type ClaimingRepository interface {
	ClaimJobForAttempt(ctx context.Context, job Job, attemptedAt time.Time) (bool, error)
}

ClaimingRepository optionally provides an ownership claim gate before dispatch.

type Clock

type Clock interface {
	Now() time.Time
}

Clock abstracts time acquisition for deterministic tests.

type Config

type Config struct {
	Repository    Repository
	Dispatcher    Dispatcher
	Logger        *slog.Logger
	Interval      time.Duration
	MaxRetries    int
	SuccessStatus string
	FailureStatus string
	Clock         Clock
}

Config contains all inputs required to construct a Worker.

type DispatchResult

type DispatchResult struct {
	Status            string
	ProviderMessageID string
}

DispatchResult carries dispatcher-supplied metadata, including status overrides and provider IDs.

type Dispatcher

type Dispatcher interface {
	Attempt(ctx context.Context, job Job) (DispatchResult, error)
}

Dispatcher performs the effectful work for a job (sending an email, firing an SMS, etc.).

type Job

type Job struct {
	ID              string
	ScheduledFor    *time.Time
	RetryCount      int
	LastAttemptedAt time.Time
	Payload         any
}

Job represents a scheduled unit of work alongside metadata the scheduler needs for backoff decisions.

type Repository

type Repository interface {
	PendingJobs(ctx context.Context, maxRetries int, now time.Time) ([]Job, error)
	ApplyAttemptResult(ctx context.Context, job Job, update AttemptUpdate) error
}

Repository exposes persistence hooks for fetching pending jobs and recording attempt results.

type Worker

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

Worker orchestrates scheduled retries with exponential backoff and contextual logging.

func NewWorker

func NewWorker(cfg Config) (*Worker, error)

NewWorker validates the configuration and returns a ready-to-run Worker.

func (*Worker) Run

func (worker *Worker) Run(ctx context.Context)

Run executes the retry loop until the provided context is canceled.

func (*Worker) RunOnce

func (worker *Worker) RunOnce(ctx context.Context)

RunOnce executes a single scheduler cycle. This is primarily used in tests.

Jump to

Keyboard shortcuts

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