Documentation
¶
Index ¶
- Variables
- type BackoffFunc
- type Config
- type CronFunc
- type EnqueueConfig
- type Event
- type EventHandler
- type EventType
- type HandlerFunc
- type Job
- type Queue
- func (q *Queue) Cron(name, spec string, handler CronFunc, config ...ScheduleConfig) (Schedule, error)
- func (q *Queue) Enqueue(ctx context.Context, name string, payload any, config ...EnqueueConfig) (Job, error)
- func (q *Queue) EnqueueIn(ctx context.Context, name string, payload any, delay time.Duration, ...) (Job, error)
- func (q *Queue) Failed() []Job
- func (q *Queue) Handle(name string, handler HandlerFunc) error
- func (q *Queue) Pending() int
- func (q *Queue) Schedule(name, spec string, payload any, config ...ScheduleConfig) (Schedule, error)
- func (q *Queue) Start(ctx context.Context, workers int) (*Runner, error)
- func (q *Queue) Unschedule(id string) error
- type Runner
- type Schedule
- type ScheduleConfig
- type ScheduleSpec
Constants ¶
This section is empty.
Variables ¶
var ( ErrJobNameRequired = errors.New("zincjobs: job name is required") ErrHandlerRequired = errors.New("zincjobs: handler is required") ErrHandlerExists = errors.New("zincjobs: handler already registered") ErrHandlerNotFound = errors.New("zincjobs: handler not found") ErrInvalidWorkerCount = errors.New("zincjobs: worker count must be greater than zero") ErrInvalidMaxAttempts = errors.New("zincjobs: max attempts must be greater than zero") ErrInvalidSchedule = errors.New("zincjobs: invalid schedule") ErrQueueAlreadyRunning = errors.New("zincjobs: queue is already running") ErrScheduleExists = errors.New("zincjobs: schedule already registered") ErrScheduleNotFound = errors.New("zincjobs: schedule not found") ErrScheduleIDRequired = errors.New("zincjobs: schedule id is required") ErrScheduleNameRequired = errors.New("zincjobs: schedule name is required") )
var DefaultConfig = Config{ DefaultQueue: "default", DefaultMaxAttempts: 1, Backoff: FixedBackoff(time.Second), Now: time.Now, }
DefaultConfig is used by New.
Functions ¶
This section is empty.
Types ¶
type BackoffFunc ¶
BackoffFunc returns the delay before the next attempt after a failed job.
func ExponentialBackoff ¶
func ExponentialBackoff(base, max time.Duration) BackoffFunc
ExponentialBackoff returns a backoff function that doubles from base up to max.
func FixedBackoff ¶
func FixedBackoff(delay time.Duration) BackoffFunc
FixedBackoff returns a backoff function that always uses delay.
type Config ¶
type Config struct {
DefaultQueue string
DefaultMaxAttempts int
Backoff BackoffFunc
EventHandler EventHandler
Now func() time.Time
}
Config controls queue defaults.
type EnqueueConfig ¶
EnqueueConfig customizes a single enqueued job.
type EventHandler ¶
type EventHandler func(Event)
EventHandler observes job lifecycle events emitted by workers and schedules.
type HandlerFunc ¶
HandlerFunc processes a job attempt.
type Job ¶
type Job struct {
ID string
Name string
Queue string
Payload []byte
CreatedAt time.Time
RunAt time.Time
Attempts int
MaxAttempts int
LastError string
}
Job is the immutable job snapshot passed to handlers.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue stores jobs, handlers, and schedules for a worker pool.
func NewWithConfig ¶
NewWithConfig creates a queue with explicit defaults.
func (*Queue) Cron ¶
func (q *Queue) Cron(name, spec string, handler CronFunc, config ...ScheduleConfig) (Schedule, error)
Cron registers a named scheduled function.
func (*Queue) Enqueue ¶
func (q *Queue) Enqueue(ctx context.Context, name string, payload any, config ...EnqueueConfig) (Job, error)
Enqueue adds a job to the queue.
func (*Queue) EnqueueIn ¶
func (q *Queue) EnqueueIn(ctx context.Context, name string, payload any, delay time.Duration, config ...EnqueueConfig) (Job, error)
EnqueueIn adds a job that becomes runnable after delay.
func (*Queue) Handle ¶
func (q *Queue) Handle(name string, handler HandlerFunc) error
Handle registers a handler for a named job.
func (*Queue) Schedule ¶
func (q *Queue) Schedule(name, spec string, payload any, config ...ScheduleConfig) (Schedule, error)
Schedule registers a recurring job using a cron expression or @every duration.
func (*Queue) Unschedule ¶
Unschedule removes a recurring job by id.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner controls a running queue.
type Schedule ¶
type Schedule struct {
ID string
Name string
Spec string
Queue string
NextRunAt time.Time
MaxAttempts int
}
Schedule describes a registered recurring job.
type ScheduleConfig ¶
ScheduleConfig customizes jobs created by a schedule.
type ScheduleSpec ¶
ScheduleSpec calculates the next time a recurring job should run.
func ParseSchedule ¶
func ParseSchedule(spec string) (ScheduleSpec, error)
ParseSchedule parses a five-field cron expression or aliases such as @every.