postgres

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store is the networked Store twin of sqlite.Store (ADR-0007): the same facts-first semantics over PostgreSQL, for deployments where many producers/workers share a queue across machines. Claims use SELECT ... FOR UPDATE SKIP LOCKED instead of SQLite's single serialized writer: competing workers lock disjoint rows instead of queueing behind one connection.

Storage mapping mirrors SQLite exactly (unix-milli BIGINT timestamps, deps table, partial unique dedup index) so the projections and the journal remain byte-compatible across backends.

func Open

func Open(ctx context.Context, dsn string, maxConns int32) (*Store, error)

Open connects to dsn (e.g. "postgres://user:pass@host:5432/db"), applies the schema, and returns a ready store. maxConns bounds the pool (0 = pgx default).

func (*Store) Cancel

func (s *Store) Cancel(ctx context.Context, id task.ID, reason string) error

Cancel withdraws a Pending task. A non-empty reason is stored in the task.cancelled fact detail ("reason" key).

func (*Store) CancelOwned

func (s *Store) CancelOwned(ctx context.Context, id task.ID, owner string) error

CancelOwned finalizes a cooperative cancel (lease holder). The operator's reason (from the cancel-requested fact) is carried onto the cancelled fact.

func (*Store) CancelRequested

func (s *Store) CancelRequested(ctx context.Context, id task.ID) (bool, error)

CancelRequested reports a pending cooperative cancel request.

func (*Store) CancelRunning

func (s *Store) CancelRunning(ctx context.Context, id task.ID, reason string) error

CancelRunning records the cooperative cancel request (idempotent fact). A non-empty reason rides the request fact's detail and is carried onto the final task.cancelled fact by CancelOwned / the reclaim finalize.

func (*Store) ClaimDue

func (s *Store) ClaimDue(ctx context.Context, owner string, lease time.Duration) (task.Task, error)

ClaimDue atomically claims one due task for owner: FOR UPDATE SKIP LOCKED keeps competing workers on disjoint rows (the multi-machine replacement for SQLite's single serialized writer). Semantics otherwise match sqlite.Store: deps gate, expired-lease reclaim with task.released, pending cooperative cancels finalized at reclaim.

func (*Store) Close

func (s *Store) Close() error

Close releases the pool.

func (*Store) Complete

func (s *Store) Complete(ctx context.Context, id task.ID, owner string, result json.RawMessage) error

Complete marks a Running task Completed.

func (*Store) CountFacts

func (s *Store) CountFacts(ctx context.Context, ftype journal.FactType, since time.Time) (int64, error)

CountFacts counts facts of one type since a time.

func (*Store) CountTasks

func (s *Store) CountTasks(ctx context.Context, f queue.Filter) (int, error)

CountTasks counts tasks matching the filter.

func (*Store) Enqueue

func (s *Store) Enqueue(ctx context.Context, n task.New) (task.Task, error)

Enqueue persists a new task and records task.enqueued; dedup keys make it idempotent (the partial unique index is the arbiter).

func (*Store) Facts

func (s *Store) Facts(ctx context.Context, after int64, limit int) ([]journal.Fact, error)

Facts exposes the journal in Seq order after the cursor.

func (*Store) FactsForTask

func (s *Store) FactsForTask(ctx context.Context, id string, limit int) ([]journal.Fact, error)

FactsForTask returns one task's facts in Seq order; limit > 0 bounds to the MOST RECENT n facts (same contract as the SQLite store).

func (*Store) Fail

func (s *Store) Fail(
	ctx context.Context,
	id task.ID,
	owner string,
	errText string,
	backoff time.Duration,
	evidence json.RawMessage,
) error

Fail records a failed attempt: retry with backoff or dead-letter.

func (*Store) FailPermanent

func (s *Store) FailPermanent(
	ctx context.Context,
	id task.ID,
	owner string,
	errText string,
	evidence json.RawMessage,
) error

FailPermanent dead-letters regardless of the attempt budget.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id task.ID) (task.Task, error)

Get returns the current task record.

func (*Store) HeadSeq

func (s *Store) HeadSeq(ctx context.Context) (int64, error)

HeadSeq returns the highest fact seq (0 when empty).

func (*Store) Heartbeat

func (s *Store) Heartbeat(ctx context.Context, id task.ID, owner string, extend time.Duration) error

Heartbeat extends the lease of a Running task held by owner.

func (*Store) LastFacts

func (s *Store) LastFacts(ctx context.Context, limit int) ([]journal.Fact, error)

LastFacts returns the most recent facts in ascending order.

func (*Store) List

func (s *Store) List(ctx context.Context, f queue.Filter) ([]task.Task, error)

List returns tasks matching the filter.

func (*Store) ListWatermarks

func (s *Store) ListWatermarks(ctx context.Context) ([]queue.WatermarkEntry, error)

ListWatermarks returns every consumer cursor, by consumer name — the admin read behind `tq watermarks show`.

func (*Store) MarkOrphaned

func (s *Store) MarkOrphaned(ctx context.Context, cutoff time.Time) (int, error)

MarkOrphaned records stranded expired-lease Running tasks (idempotent).

func (*Store) ProjectCounts

func (s *Store) ProjectCounts(ctx context.Context) (map[string]map[task.Status]int, error)

ProjectCounts counts tasks per project per status.

func (*Store) Requeue

func (s *Store) Requeue(
	ctx context.Context,
	id task.ID,
	owner string,
	errText string,
	delay time.Duration,
) error

Requeue returns a claimed task to Pending without counting an attempt.

func (*Store) RescueDead

func (s *Store) RescueDead(ctx context.Context, id task.ID, maxAttempts int) error

RescueDead re-queues a Dead task with a fresh attempt budget.

func (*Store) SaveWatermark

func (s *Store) SaveWatermark(ctx context.Context, consumer string, seq int64) error

SaveWatermark checkpoints a consumer cursor as a monotonic upsert: the stored seq never regresses. Consumer progress, not task state, so no fact is appended.

func (*Store) SetWatermark

func (s *Store) SetWatermark(ctx context.Context, consumer string, seq int64) error

SetWatermark overwrites a consumer cursor unconditionally — the ops rewind hatch (`tq watermarks set`); deliberately bypasses the monotonic guard because a rewind is an intentional force-replay.

func (*Store) StatusCounts

func (s *Store) StatusCounts(ctx context.Context) (map[task.Status]int, error)

StatusCounts counts tasks per status.

func (*Store) Watermark

func (s *Store) Watermark(ctx context.Context, consumer string) (int64, bool, error)

Watermark returns the persisted read cursor for a journal consumer and whether it ever checkpointed. seq 0 with exists=true is a valid cursor.

Jump to

Keyboard shortcuts

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