feat: add worker job-claim logic

This commit is contained in:
Jonathan
2026-07-10 16:58:28 +02:00
parent 8085ed719f
commit 49b69c592d
4 changed files with 104 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
import os
import psycopg
import pytest
@pytest.fixture()
def conn():
dsn = os.environ["DATABASE_URL"]
connection = psycopg.connect(dsn)
yield connection
# Clean up rows created during the test (Job cascades from Request).
with connection.cursor() as cur:
cur.execute('DELETE FROM "Job"')
cur.execute('DELETE FROM "Request"')
connection.commit()
connection.close()
def insert_request(conn, artist="Artist", album="Album"):
"""Insert a Request + its Job (state 'requested') and return the job id."""
with conn.cursor() as cur:
cur.execute(
'INSERT INTO "Request" (id, artist, album, status, "createdAt") '
"VALUES (gen_random_uuid()::text, %s, %s, 'pending', now()) RETURNING id",
(artist, album),
)
request_id = cur.fetchone()[0]
cur.execute(
'INSERT INTO "Job" (id, "requestId", state, "currentStage", attempts, "createdAt", "updatedAt") '
"VALUES (gen_random_uuid()::text, %s, 'requested', 'intake', 0, now(), now()) RETURNING id",
(request_id,),
)
job_id = cur.fetchone()[0]
conn.commit()
return job_id