pub async fn claim_pending(
pool: &Pool,
parent: &str,
) -> Result<Vec<RunRow>, Error>Expand description
Atomically claim every pending schedule in parent’s subtree (its
own AIH or any descendant): ONE statement both selects the eligible
rows and inserts their tasks_runs rows, so each returned RunRow
carries the freshly-minted run_id. Schedules are never updated or
deleted by a run — exhaustion is purely “has a run” (oneshots) /
“newest run too recent” (recurring), and only the newest version of
each (name, aih) is eligible.
The transaction opens with pg_advisory_xact_lock so concurrent
tasks run callers fully serialize: the second claimer sees the
first’s tasks_runs rows and excludes those schedules. (Row locks
can’t do this — eligibility reads a DIFFERENT table than the one
the claim writes, so a blocked reader would re-check a schedules
row that never changed and double-claim.)