pgstore

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package pgstore implements store.Store on PostgreSQL. All time comparisons use the database clock (now()), never the server's.

Index

Constants

View Source
const MaxEnqueueBatch = 1000

MaxEnqueueBatch is the largest batch Enqueue accepts.

View Source
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

func Migrate

func Migrate(ctx context.Context, pool *pgxpool.Pool) error

Migrate applies any migrations not yet recorded in tasklane_migrations, in filename order, inside one transaction.

Types

type Store

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

func New

func New(pool *pgxpool.Pool, policy backoff.Policy) *Store

func (*Store) Ack

func (s *Store) Ack(ctx context.Context, id int64, token uuid.UUID) (store.AckResult, error)

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

func (s *Store) CancelJob(ctx context.Context, id int64) (store.Job, bool, error)

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) CreateQueue

func (s *Store) CreateQueue(ctx context.Context, q store.Queue) error

func (*Store) Enqueue

func (s *Store) Enqueue(ctx context.Context, jobs []store.NewJob) ([]store.EnqueueResult, error)

func (*Store) GetJob

func (s *Store) GetJob(ctx context.Context, id int64) (store.Job, error)

func (*Store) GetQueue

func (s *Store) GetQueue(ctx context.Context, name string) (store.Queue, error)

func (*Store) Heartbeat

func (s *Store) Heartbeat(ctx context.Context, id int64, token uuid.UUID, ttl time.Duration) (time.Time, error)

func (*Store) Lease

func (s *Store) Lease(ctx context.Context, queue string, n int, ttl time.Duration) ([]store.Job, error)

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) Nack

func (s *Store) Nack(ctx context.Context, id int64, token uuid.UUID, errMsg string, retryAt *time.Time) (store.NackResult, error)

func (*Store) PruneDead

func (s *Store) PruneDead(ctx context.Context, age time.Duration, limit int) (int, error)

func (*Store) PruneFinished

func (s *Store) PruneFinished(ctx context.Context, age time.Duration, limit int) (int, error)

func (*Store) QueueDepths

func (s *Store) QueueDepths(ctx context.Context) ([]store.QueueDepth, error)

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

func (s *Store) QueueStats(ctx context.Context, queue string) (store.QueueStats, error)

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

func (s *Store) ReapExpired(ctx context.Context, limit int) ([]store.ReapedJob, error)

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

func (s *Store) RedriveDead(ctx context.Context, queue string, ids []int64, limit int) (int, error)

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.

Jump to

Keyboard shortcuts

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