QueueKeeperCheck.Ledger (queue_keeper_check v0.3.0)

Copy Markdown View Source

Opt-in last-seen ledger for :dead_workers.

Oban (and Oban Pro, unless a rule sets archive: true) deletes finished jobs. There is no leftover row to query after a prune, so a check that only reads oban_jobs cannot see a 14-day window when max_age is 60 seconds. This ledger is the workaround that does not ask you to keep more jobs:

  1. Run QueueKeeperCheck.Ledger.Migration in the host app (a tiny pair of tables, one row per worker).

  2. Attach the handler in the running application (every node that inserts jobs):

     {QueueKeeperCheck.Ledger, repo: MyApp.Repo, prefix: "public", backfill: true}

    or QueueKeeperCheck.Ledger.attach(repo: MyApp.Repo, backfill: true).

The handler listens for [:oban, :engine, :insert_job, :stop] and [:oban, :engine, :insert_all_jobs, :stop], coalesces sightings in ETS, and flushes one upsert per worker every few seconds. Upserts use GREATEST (or the engine equivalent) so multiple nodes cannot move last_inserted_at backwards. Failures are logged and swallowed: a ledger error must never break job insertion.

QueueKeeperCheck.run/2 stays read-only. It selects from the ledger when the tables exist and recording has started; otherwise it falls back to scanning oban_jobs. The writer is only this process, and only after you opt in.

The check stays silent (or emits report_short_history) until the ledger itself has been recording for window_days, so a fresh attach cannot call every never-yet-seen worker dead.

Summary

Functions

Start under a transient task helper; same as start_link/1.

One-time backfill of current oban_jobs into the ledger (a sequential scan). Opt-in; also passed as backfill: true on attach/1. Safe to run more than once: upserts keep the later last_inserted_at.

Child specification for the host application's supervisor.

Detach telemetry and stop the process. Pending ETS rows are flushed first.

Read the ledger for a prefix, or :absent when the tables do not exist or recording has never started. Used by :dead_workers. Never writes.

Flush pending ETS sightings now. Returns :ok or {:error, reason}.

Start the ledger process and attach telemetry. See child_spec/1.

Functions

attach(opts)

Start under a transient task helper; same as start_link/1.

backfill(repo, opts \\ [])

One-time backfill of current oban_jobs into the ledger (a sequential scan). Opt-in; also passed as backfill: true on attach/1. Safe to run more than once: upserts keep the later last_inserted_at.

child_spec(init_arg)

Child specification for the host application's supervisor.

Options: :repo (required), :prefix, :oban_name (:all or an Oban instance name, default :all), :flush_interval (ms, default 5_000), :backfill (default false; one sequential scan of oban_jobs on start), :name (process name, default QueueKeeperCheck.Ledger), :max_pending (ETS cap, default 20_000).

detach(server \\ __MODULE__)

Detach telemetry and stop the process. Pending ETS rows are flushed first.

fetch(repo, opts \\ [])

Read the ledger for a prefix, or :absent when the tables do not exist or recording has never started. Used by :dead_workers. Never writes.

flush(server \\ __MODULE__)

Flush pending ETS sightings now. Returns :ok or {:error, reason}.

start_link(opts)

Start the ledger process and attach telemetry. See child_spec/1.