Repository navigation
feat(jobs): single-runner leader gate for the durable job worker (#271) #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a056a80
feat(jobs): single-runner leader gate for the durable job worker (#271)
d05acda
fix(jobs): fail-safe leader lease under transaction pooling (#271)
4077f1a
docs(jobs): correct the leader-lease transaction-pooling claim (#271)
81c0c57
docs(jobs): sync deploy-invariant to the #271 leader gate
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| namespace Cluckwork.Infrastructure.Jobs; | ||
|
|
||
| // #271 — the outcome of one leadership-acquisition attempt. A first-class result | ||
| // (not a bool) so the worker can tell "another instance holds the lock" (Follower, | ||
| // a healthy steady state) apart from "I could not even ask" (Faulted, a fault that | ||
| // must degrade /health). Conflating the two let a DB outage read as a healthy | ||
| // follower and silently stop all background work behind a green health check — the | ||
| // exact #69 stall-detection guarantee this must not break. | ||
| public enum LeaseStatus | ||
| { | ||
| // This instance holds the lease: run the poll and the sweeps. | ||
| Leader, | ||
| // Another instance holds the lease: stand down. The loop is alive and healthy — | ||
| // a follower does no work by design. | ||
| Follower, | ||
| // The acquisition attempt itself faulted (e.g. the DB is unreachable): back off | ||
| // and do NOT stamp the heartbeat, so a sustained fault degrades /health. | ||
| Faulted, | ||
| } | ||
|
|
||
| // #271 — the background worker's single-runner gate: at most one API instance (the | ||
| // "leader") runs the durable-job poll and the recurring sweeps. See | ||
| // PostgresLeaderLease for the mechanism and the exact contract (AT MOST ONE ACTIVE | ||
| // LEADER, never "exactly once"). | ||
| public interface ILeaderLease | ||
| { | ||
| // The leadership status of THIS instance after the call. Safe to call every | ||
| // poll: a current leader re-affirms (re-verifying its session) without stacking | ||
| // the lock; a follower retries acquisition; a fault is reported as Faulted, not | ||
| // thrown, so the worker never lets it reach BackgroundServiceExceptionBehavior. | ||
| Task<LeaseStatus> TryAcquireAsync(CancellationToken ct); | ||
| } | ||
|
|
||
| // A lease that is always the leader — for a deploy that is single-instance by | ||
| // construction, and for the worker's resilience unit tests. Deliberately NOT a | ||
| // constructor default: the worker REQUIRES an explicit ILeaderLease, so a host that | ||
| // forgets to register one fails at startup (fail-closed) rather than silently | ||
| // running every replica as leader and re-introducing the #271 double-run. | ||
| public sealed class AlwaysLeaderLease : ILeaderLease | ||
| { | ||
| public Task<LeaseStatus> TryAcquireAsync(CancellationToken ct) => | ||
| Task.FromResult(LeaseStatus.Leader); | ||
| } | ||
|
|
||
| // Hands the already-normalised, TLS-floor-validated connection string (registered | ||
| // by AddCluckworkPersistence) to PostgresLeaderLease without a second configuration | ||
| // lookup — the lease opens its own dedicated, non-pooled connection from this exact | ||
| // string. A tiny typed wrapper so DI injects the right string rather than an ambient | ||
| // one. | ||
| public sealed record LeaderLeaseConnectionString(string Value); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This registration introduces the advisory-lock lease, but the canonical deployment guidance and its linked decision still state that the worker poll has no lease or advisory lock and list that absence as the #271 blocker. Leaving those architecture records unchanged makes subsequent scaling audits reason from behavior that this PR explicitly replaces; update them in the same change to describe the new lease and its transaction-pooling limitation while retaining the remaining single-instance blockers.
AGENTS.md reference: AGENTS.md:L126-L130
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed the AGENTS.md deploy-invariant + decision records go stale once this merges. That edit is deliberately owned by #537 (slice T9 — 'Docs: multi-farm tenancy ADR + AGENTS.md / GLOSSARY sync'), which lands alongside whichever slice settles each claim: the file explicitly warns that blocker list 'was derived wrong twice,' so partial edits from a feature PR are exactly what that split avoids. And AGENTS.md still correctly describes
mainuntil #555 merges. I've flagged the specific #271 'no lease' wording + the transaction-pooling limitation on #537 so it's captured there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: on reflection the #271-specific piece of this is a settled doc change, so I folded it into this PR (81c0c57) rather than leaving all of it for #537. The canonical AGENTS.md deploy-invariant paragraph and the 271 decision doc now describe the advisory-lock leader gate, its session-pinned requirement + transaction-pooling limitation (#556), and the re-derived remaining blockers (#143, #311) — the blocker list re-derived by walking every AddSingleton/AddHostedService under src/, not edited from memory (that list 'was derived wrong twice'). The broader multi-slice AGENTS.md/GLOSSARY/ADR reconciliation (tenancy, login, suspension — most blocked on unlanded slices) stays #537.