Documentation
¶
Overview ¶
Package singleton runs a periodic job on one replica at a time. The replica that takes the job's Postgres advisory lock runs it; one that finds the lock held skips that tick (counted and logged at debug), and the next tick catches up, since every job that uses this is idempotent. The lock is session-level on a dedicated connection, so a pod that dies releases it with its connection.
Index ¶
Constants ¶
const ( Ran = "ran" Skipped = "skipped" // another replica held the lock Errored = "error" // the lock could not be tried (no connection, database error) )
Outcomes, as the metric labels them.
Variables ¶
This section is empty.
Functions ¶
func Acquire ¶
func Acquire(ctx context.Context, d *sql.DB, job string, key Key) (release func(), ok bool, err error)
Acquire takes job's lock without waiting. When it returns ok, the caller holds the lock until it calls release (on a connection kept for the purpose; a pod that dies releases it with the connection). A held lock is (nil, false, nil), counted as skipped; a fault trying it is (nil, false, err).
func Run ¶
func Run(ctx context.Context, d *sql.DB, job string, key Key, fn func(ctx context.Context) error) (bool, error)
Run is Acquire around fn: it runs fn under job's lock and reports whether it ran. The lock is released when fn returns, however it ends.
func SetObserver ¶
func SetObserver(f func(job, outcome string))
SetObserver tells f every attempt's outcome (job, outcome), for rivolt_singleton_runs_total. Set once at boot.