File size: 3,289 Bytes
cd0c7a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | -- Phase 0b: Durable worker columns + claim RPCs for docking_jobs, sequencing_jobs, jobs.
-- ---------------------------------------------------------------------------
-- 1. Add worker tracking columns
-- ---------------------------------------------------------------------------
ALTER TABLE docking_jobs
ADD COLUMN IF NOT EXISTS claimed_at timestamptz,
ADD COLUMN IF NOT EXISTS claimed_by text,
ADD COLUMN IF NOT EXISTS attempts integer NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS max_attempts integer NOT NULL DEFAULT 3,
ADD COLUMN IF NOT EXISTS updated_at timestamptz,
ADD COLUMN IF NOT EXISTS payload jsonb;
ALTER TABLE sequencing_jobs
ADD COLUMN IF NOT EXISTS claimed_at timestamptz,
ADD COLUMN IF NOT EXISTS claimed_by text,
ADD COLUMN IF NOT EXISTS attempts integer NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS max_attempts integer NOT NULL DEFAULT 3,
ADD COLUMN IF NOT EXISTS updated_at timestamptz,
ADD COLUMN IF NOT EXISTS payload jsonb;
ALTER TABLE jobs
ADD COLUMN IF NOT EXISTS claimed_at timestamptz,
ADD COLUMN IF NOT EXISTS claimed_by text,
ADD COLUMN IF NOT EXISTS attempts integer NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS max_attempts integer NOT NULL DEFAULT 3;
-- ---------------------------------------------------------------------------
-- 2. Claim RPCs (FOR UPDATE SKIP LOCKED — atomic, no double-processing)
-- ---------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION claim_next_docking_job(worker_id text)
RETURNS docking_jobs
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
job docking_jobs;
BEGIN
SELECT * INTO job
FROM docking_jobs
WHERE status = 'queued'
AND attempts < max_attempts
ORDER BY created_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED;
IF job.id IS NOT NULL THEN
UPDATE docking_jobs
SET status = 'running',
claimed_at = now(),
claimed_by = worker_id,
attempts = attempts + 1,
updated_at = now()
WHERE id = job.id
RETURNING * INTO job;
END IF;
RETURN job;
END;
$$;
CREATE OR REPLACE FUNCTION claim_next_sequencing_job(worker_id text)
RETURNS sequencing_jobs
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
job sequencing_jobs;
BEGIN
SELECT * INTO job
FROM sequencing_jobs
WHERE status = 'queued'
AND attempts < max_attempts
ORDER BY created_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED;
IF job.id IS NOT NULL THEN
UPDATE sequencing_jobs
SET status = 'running',
claimed_at = now(),
claimed_by = worker_id,
attempts = attempts + 1,
updated_at = now()
WHERE id = job.id
RETURNING * INTO job;
END IF;
RETURN job;
END;
$$;
CREATE OR REPLACE FUNCTION claim_next_pipeline_job(worker_id text)
RETURNS jobs
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
job jobs;
BEGIN
SELECT * INTO job
FROM jobs
WHERE status = 'queued'
AND attempts < max_attempts
ORDER BY created_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED;
IF job.id IS NOT NULL THEN
UPDATE jobs
SET status = 'running',
claimed_at = now(),
claimed_by = worker_id,
attempts = attempts + 1
WHERE id = job.id
RETURNING * INTO job;
END IF;
RETURN job;
END;
$$;
|