Documentation
¶
Overview ¶
Package queue implements job queues with delayed jobs, a visibility timeout, retries and a dead-letter list.
Every job is in exactly one state:
ready waiting to be popped, ordered by (at, seq) where at is when it became ready delayed not ready before at inflight popped, must be acked before at (the visibility deadline) dead ran out of retries (at = when it died)
Time-driven transitions are pure functions of a job's own fields and the clock, and are applied lazily whenever a queue is touched:
delayed → ready at at inflight → ready (or dead when attempts > retries) at the deadline
Replication works by sending "set this job to this state" operations (Restore, Claim, Ack, Purge) with absolute timestamps, so a replica or an AOF replay converges to the same contents whenever it applies them. The command layer turns each mutation into such operations.
Index ¶
- func RestoreArgs(name string, j Job) []string
- type Counts
- type Job
- type Manager
- func (m *Manager) AckLocked(name, id string, now int64) bool
- func (m *Manager) ClaimLocked(name, id string, attempts, deadline int64) bool
- func (m *Manager) DeadJobs(name string, count int) []Job
- func (m *Manager) DeleteLocked(name, id string) bool
- func (m *Manager) Dump(emit func(args []string))
- func (m *Manager) ExistsLocked(name, id string) bool
- func (m *Manager) Len(name string) Counts
- func (m *Manager) Lock()
- func (m *Manager) NackLocked(name, id string, delay time.Duration, now int64) (Job, bool)
- func (m *Manager) Names() []string
- func (m *Manager) NewID() string
- func (m *Manager) PopLocked(name string, visibility time.Duration, now int64) (Job, bool)
- func (m *Manager) PurgeLocked(name string) int
- func (m *Manager) Restore(name string, j Job)
- func (m *Manager) RestoreLocked(name string, j Job)
- func (m *Manager) RetryDeadLocked(name string, now int64) []Job
- func (m *Manager) Snapshot() map[string][]Job
- func (m *Manager) Unlock()
- func (m *Manager) WaitInfoLocked(name string) (<-chan struct{}, int64)
- type State
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RestoreArgs ¶
RestoreArgs is the internal command that recreates one job exactly: q.restore queue id payload retries attempts state at.
Types ¶
type Job ¶
type Job struct {
ID string
Payload string
Retries int64
Attempts int64
State State
At int64 // unix ms, meaning depends on State
}
Job is a snapshot of a job (returned by value; never shared).
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager holds every queue. All methods are safe for concurrent use.
func (*Manager) AckLocked ¶
AckLocked deletes an in-flight job. Returns false if it isn't in flight.
func (*Manager) ClaimLocked ¶
ClaimLocked puts job id in flight with the given attempts and deadline, whatever its current state (replay of a QPOP). Returns false if unknown.
func (*Manager) DeleteLocked ¶
DeleteLocked deletes a job in any state (replay of QACK).
func (*Manager) ExistsLocked ¶
ExistsLocked reports whether a job with id is in the queue (any state).
func (*Manager) Lock ¶
func (m *Manager) Lock()
Lock/Unlock let the command layer run a mutation and propagate it atomically, so AOF/replicas see operations in execution order. Methods ending in "Locked" require it.
func (*Manager) NackLocked ¶
NackLocked returns an in-flight job to the queue: ready (or delayed by delay), or dead when its attempts exceed its retries. It returns the job's new state, or false if the job isn't in flight.
func (*Manager) PopLocked ¶
PopLocked claims the oldest ready job for visibility ms. It returns the job as it is after the claim (Inflight, Attempts incremented).
func (*Manager) PurgeLocked ¶
PurgeLocked deletes the queue. Returns how many jobs it held.
func (*Manager) RestoreLocked ¶
RestoreLocked creates or replaces a job with exactly the given fields. This is the replication primitive behind QPUSH, QNACK, QRETRYDEAD and Dump.
func (*Manager) RetryDeadLocked ¶
RetryDeadLocked moves every dead job back to ready with attempts reset. Returns the jobs as restored, oldest first.
func (*Manager) Snapshot ¶
Snapshot returns every job of every queue, ordered by queue name, then state (ready, delayed, inflight, dead), then (at, seq). Restoring them in this order recreates the same queues.
func (*Manager) WaitInfoLocked ¶
WaitInfoLocked returns a channel closed when a job of name may become ready, and the time of the next time-driven transition (0 if none).