Phase 9, part 6: background work shared safely between copies of the app
internal/jobs keeps named locks as leases in the database (schema 31: locks, instances), taken with one conditional UPDATE on SQLite or Postgres, renewed while the work runs and lapsing if its copy stops. Each background loop's turn, sending an email, audits, discovery runs and imports run under a lock; OAuth sign-ins under way are kept in the database. Copies check in, and work marked running is recovered at start only when no other copy is alive. Settings -> About lists the copies and work under way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1968,6 +1968,40 @@ than planned: rather than helpers in every query, the SQL stays as it is
|
||||
|
||||
The first users: exports (part 7), then uploads and images.
|
||||
|
||||
**Part 6 built 2026-11-25** on `phase9`.
|
||||
- `internal/jobs` holds locks as leases in the database (schema 31: tables
|
||||
`locks` and `instances`). Taking a lock is one conditional `UPDATE`
|
||||
(free, or its lease lapsed), the same on SQLite and Postgres. That's
|
||||
simpler than a queue with `SKIP LOCKED`: each loop already keeps what's
|
||||
due in the database, so a lock per job is enough.
|
||||
- A lease lasts 2 minutes and is renewed every 30 seconds while the work
|
||||
runs. If a copy stops, its work is taken over once the lease lapses.
|
||||
- If a lease is lost, the work's context is cancelled.
|
||||
- Locks aren't re-entrant: two goroutines in one copy exclude each other
|
||||
too.
|
||||
- What runs under a lock:
|
||||
- each background loop's turn (`job:scheduler`, `job:imports`,
|
||||
`job:discovery`, `job:outreach`, `job:tracking`, `job:seo`,
|
||||
`job:metrics`)
|
||||
- sending an email, from the queue or *Send now* (`outreach:send`,
|
||||
replacing the in-memory mutex)
|
||||
- an audit per site, a discovery per signal and source, and an import per
|
||||
tool (replacing three in-memory guards)
|
||||
- when replies were last read
|
||||
- OAuth sign-ins under way are kept in the database (`oauth:flow:<state>`
|
||||
settings), so the callback can reach any copy; a state works once. Left
|
||||
in memory, as caches per copy: Google's access token, Reddit's app
|
||||
token, and the git working copies' locks (each copy has its own clones).
|
||||
- Each copy checks in every 30 seconds. At start, work marked "running"
|
||||
(publishing, audits, briefs, changes, site pages, discovery) is marked
|
||||
interrupted only when no other copy is alive. A copy that crashes while
|
||||
others run leaves its "running" rows until every copy has restarted;
|
||||
tying each row to its copy would fix that, in Phase 13.
|
||||
- Settings → About lists the copies running and the work under way.
|
||||
- Leases compare the copies' clocks: keep them in sync (NTP), as any
|
||||
server would.
|
||||
- The backup loop stays as it is: it's the local edition's, with one copy.
|
||||
|
||||
Nothing changes for the local user; this makes the rest possible. A
|
||||
commit after each part; the whole test suite runs against both SQLite and
|
||||
Postgres from part 2 on.
|
||||
|
||||
Reference in New Issue
Block a user