Explain why a claimed queue job is not a completed job
Also published in our Blogger archive.
Claiming work does not complete it
The conditional UPDATE changes queued to claimed, and commit persists that state. The assertions then show claimed and not completed because no completion operation appears in the script. A worker can therefore truthfully record ownership before it performs any external effect.
If a worker exits after the claim, treating it as completion would be false. This one-row model has no lease, completion time, or downstream idempotency record; those govern recovery of uncertain attempts.
Example
import sqlite3
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
connection = sqlite3.connect(Path(directory) / "queue.db")
connection.execute("CREATE TABLE job(id INTEGER PRIMARY KEY, state TEXT)")
connection.execute("INSERT INTO job VALUES (1, 'queued')")
connection.execute("UPDATE job SET state = 'claimed' WHERE id = 1 AND state = 'queued'")
connection.commit()
state = connection.execute("SELECT state FROM job WHERE id = 1").fetchone()[0]
assert state == "claimed"
assert state != "completed"
print("state=claimed completed=no")
Expected stdout:
state=claimed completed=no
Sources
Prepared with AI assistance. The example uses synthetic data; its stated limits apply.