Documentation
¶
Overview ¶
Package pgstore implements store.Store on PostgreSQL. All time comparisons use the database clock (now()), never the server's.
Index ¶
- Constants
- func Migrate(ctx context.Context, pool *pgxpool.Pool) error
- type Store
- func (s *Store) Ack(ctx context.Context, id int64, token uuid.UUID) (store.AckResult, error)
- func (s *Store) CancelJob(ctx context.Context, id int64) (store.Job, bool, error)
- func (s *Store) CreateQueue(ctx context.Context, q store.Queue) error
- func (s *Store) Enqueue(ctx context.Context, jobs []store.NewJob) ([]store.EnqueueResult, error)
- func (s *Store) GetJob(ctx context.Context, id int64) (store.Job, error)
- func (s *Store) GetQueue(ctx context.Context, name string) (store.Queue, error)
- func (s *Store) Heartbeat(ctx context.Context, id int64, token uuid.UUID, ttl time.Duration) (time.Time, error)
- func (s *Store) Lease(ctx context.Context, queue string, n int, ttl time.Duration) ([]store.Job, error)
- func (s *Store) ListDeadJobs(ctx context.Context, queue string, after *store.DeadCursor, limit int) ([]store.Job, error)
- func (s *Store) Nack(ctx context.Context, id int64, token uuid.UUID, errMsg string, ...) (store.NackResult, error)
- func (s *Store) PruneDead(ctx context.Context, age time.Duration, limit int) (int, error)
- func (s *Store) PruneFinished(ctx context.Context, age time.Duration, limit int) (int, error)
- func (s *Store) QueueDepths(ctx context.Context) ([]store.QueueDepth, error)
- func (s *Store) QueueStats(ctx context.Context, queue string) (store.QueueStats, error)
- func (s *Store) ReapExpired(ctx context.Context, limit int) ([]store.ReapedJob, error)
- func (s *Store) RedriveDead(ctx context.Context, queue string, ids []int64, limit int) (int, error)
Constants ¶
const MaxEnqueueBatch = 1000
MaxEnqueueBatch is the largest batch Enqueue accepts.
const NotifyChannel = "tasklane_jobs"
NotifyChannel is the Postgres channel that wakes long-polling leases. The payload is the queue name, a space, and the sending server's token (see package notify). The store doesn't send on it; notify.Publisher does, after the write has committed.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func (*Store) Ack ¶
Ack keeps the token on the completed row, so a retried Ack (the first response was lost) can be recognised and answered with success.
func (*Store) CancelJob ¶
CancelJob is one conditional update, so it is safe against a concurrent Lease: if Lease locked the row first, this UPDATE waits, then re-checks state = 'available' on the new row version (EvalPlanQual), finds 'leased' and changes nothing; if Cancel locked it first, Lease skips the row.
func (*Store) ListDeadJobs ¶
func (s *Store) ListDeadJobs(ctx context.Context, queue string, after *store.DeadCursor, limit int) ([]store.Job, error)
ListDeadJobs pages with a keyset, not OFFSET: (finished_at, id) < cursor is a range scan on jobs_dead (queue, finished_at, id), so a deep page costs what the first one does. The first page has no cursor condition at all (rather than "$3 IS NULL OR …"), so the planner never has to guess.
func (*Store) PruneFinished ¶
func (*Store) QueueDepths ¶
QueueDepths reports each queue's available and leased counts and the age of its oldest ready job. The cost is one pass over the available and leased jobs per call, O(backlog), not O(queues x table). Within (queue, priority) jobs_available is ordered by run_at, so the first entry is the oldest, and it is ready if its run_at has passed. A queue with no jobs still gets a row, with zeros.
func (*Store) QueueStats ¶
QueueStats counts in one statement. available and dead use the partial indexes that start with queue; leased reads the jobs_leased partial index, which has no queue column, but leased jobs are bounded by the workers' slots. Completed jobs are counted only over the last StatsWindow through the B-tree index on finished_at, which jumps to the start of the window, so that count costs what the recent throughput costs, not the table size.
func (*Store) ReapExpired ¶
ReapExpired treats expired leases as failed attempts. SKIP LOCKED lets every server run the reaper at the same time without processing a job twice.
func (*Store) RedriveDead ¶
RedriveDead takes its batch FOR UPDATE SKIP LOCKED, so two operators redriving the same queue at once split the jobs instead of waiting on each other. There are two statements, chosen here, so neither plan depends on a NULL check: "all" walks jobs_dead (queue, finished_at, id) oldest death first, and redriven rows leave that partial index, so every batch starts at the front; "by ids" looks the ids up in the primary key. It sends no NOTIFY; the API server publishes after the commit when n > 0.