Give background work an expiring owner
Your objectiveExplain why a running flag can outlive its process.
Understand the idea.
A database row can say running even after the process that wrote it has stopped. A lease adds an owner, a heartbeat and an expiry. Another worker must acquire the lease transactionally before acting. Expiry does not prove why the old process stopped; it establishes that the old ownership is no longer current.
A worked example.
Worker A owns a collection lease and renews it every ten seconds. It then stops unexpectedly. Worker B observes the expired lease and atomically takes ownership. The previous job can be marked interrupted instead of remaining running forever. Two simultaneous takeover attempts must not both succeed.
Try the reasoning.
Draw the states idle, leased, renewed, expired and recovered. Add a test where two workers attempt takeover at the same time. Describe the one-owner invariant the test must preserve.
Carry it into practice.
Show lease expiry and heartbeat age beside the job state, without exposing credentials.
Read the reference: SQLite · Transactions ↗