worker

package
v0.0.0-...-e65d92b Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegistryMatchPattern

func RegistryMatchPattern() string

Types

type Config

type Config struct {
	Database DatabaseConfig `yaml:"database"`

	Concurrency   int             `yaml:"concurrency"`
	BRPopTimeout  config.Duration `yaml:"brpop_timeout"`
	QueueStrategy QueueStrategy   `yaml:"queue_strategy"`

	RetryBackoffBase config.Duration `yaml:"retry_backoff_base"`
	RetryBackoffMax  config.Duration `yaml:"retry_backoff_max"`

	OutcomeWriteTimeout config.Duration `yaml:"outcome_write_timeout"`
	ShutdownGrace       config.Duration `yaml:"shutdown_grace"`

	HeartbeatInterval config.Duration `yaml:"heartbeat_interval"`
	StaleMultiplier   int             `yaml:"stale_multiplier"`

	RegistryInterval config.Duration `yaml:"registry_interval"`
	RegistryTTL      config.Duration `yaml:"registry_ttl"`

	LogFlushInterval  config.Duration `yaml:"log_flush_interval"`
	LogFlushTimeout   config.Duration `yaml:"log_flush_timeout"`
	LogFlushThreshold int             `yaml:"log_flush_threshold"`
	LogBufferCapacity int             `yaml:"log_buffer_capacity"`
}

func LoadConfig

func LoadConfig(path string) (Config, error)

An empty path means the embedded defaults, so an importing application can start a worker without vendoring a copy of worker.yaml and re-vendoring it every time a key is added. The defaults go through validate() like any file would.

func (Config) StaleDelta

func (c Config) StaleDelta() time.Duration

type DatabaseConfig

type DatabaseConfig struct {
	Partitioning PartitioningConfig `yaml:"partitioning"`
	Retention    RetentionConfig    `yaml:"retention"`
}

type HandlerFunc

type HandlerFunc func(ctx context.Context, j Job) (string, error)

type InflightJob

type InflightJob struct {
	Name      string             `json:"name"`
	ID        pgtype.UUID        `json:"id"`
	AttemptID pgtype.UUID        `json:"attempt_id"`
	Version   int32              `json:"version"`
	Queue     string             `json:"queue"`
	Handler   string             `json:"handler"`
	StartedAt time.Time          `json:"started_at"`
	Cancel    context.CancelFunc `json:"-"`
}

type Job

type Job struct {
	ID         uuid.UUID
	Name       string
	Queue      string
	Handler    string
	Payload    []byte
	RetryCount int32
	MaxRetries int32
	Logs       *JobLogger
}

type JobLogger

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

func (*JobLogger) Log

func (l *JobLogger) Log(level, line string)

type PartitionInterval

type PartitionInterval string
const (
	PartitionWeekly     PartitionInterval = "weekly"
	PartitionMonthly    PartitionInterval = "monthly"
	PartitionQuarterly  PartitionInterval = "quarterly"
	PartitionHalfYearly PartitionInterval = "half_yearly"
	PartitionYearly     PartitionInterval = "yearly"
)

func (PartitionInterval) Valid

func (p PartitionInterval) Valid() bool

type PartitioningConfig

type PartitioningConfig struct {
	Interval    PartitionInterval `yaml:"interval"`
	CreateAhead int               `yaml:"create_ahead"`
}

type QueueStrategy

type QueueStrategy string
const (
	QueueStrategyPriority   QueueStrategy = "priority"
	QueueStrategyRoundRobin QueueStrategy = "round_robin"
)

func (QueueStrategy) Valid

func (q QueueStrategy) Valid() bool

type RegistryEntry

type RegistryEntry struct {
	Name      string        `json:"name"`
	ID        uuid.UUID     `json:"id"`
	Queues    []string      `json:"queues"`
	Handlers  []string      `json:"handlers"`
	InFlight  []InflightJob `json:"in_flight"`
	StartedAt time.Time     `json:"started_at"`
	LastSeen  time.Time     `json:"last_seen"`
}

type RetentionConfig

type RetentionConfig struct {
	Days int `yaml:"days"`
}

type WorkerService

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

func NewWorkerService

func NewWorkerService(jobRepo *job.Repository, rdb *redis.Client, log *slog.Logger, cfg Config, name string, queues []string, concurrency int, handlers map[string]HandlerFunc) *WorkerService

func (*WorkerService) PopAndDispatchJobs

func (w *WorkerService) PopAndDispatchJobs(ctx context.Context) error

func (*WorkerService) Run

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

ctx cancellation only stops new work being taken. The maintenance loops run on a detached context so claims keep being renewed and logs keep flushing while in-flight jobs drain; they stop once the drain is over.

Jump to

Keyboard shortcuts

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