@@ -43,22 +43,13 @@ CREATE TABLE IF NOT EXISTS ${"schema"}."task" (
4343 " delayed_at" TIMESTAMPTZ ,
4444 " started_at" TIMESTAMPTZ ,
4545 " finished_at" TIMESTAMPTZ ,
46- " dies_at" TIMESTAMPTZ NOT NULL ,
46+ " dies_at" TIMESTAMPTZ ,
4747 " retries" INTEGER NOT NULL CHECK (" retries" >= 0 ),
4848 " initial_retries" INTEGER NOT NULL CHECK (" initial_retries" >= 0 ),
4949 PRIMARY KEY (" id" ),
5050 FOREIGN KEY (" queue" ) REFERENCES ${" schema" }." queue" (" queue" ) ON DELETE CASCADE
5151) PARTITION BY RANGE (id);
5252
53- UPDATE ${" schema" }." task" t
54- SET " dies_at" = COALESCE(t." finished_at" , t." delayed_at" , t." created_at" , NOW()) + q." ttl"
55- FROM ${" schema" }." queue" q
56- WHERE t." queue" = q." queue"
57- AND t." dies_at" IS NULL ;
58-
59- ALTER TABLE ${" schema" }." task"
60- ALTER COLUMN " dies_at" SET NOT NULL ;
61-
6253-- pgqueue.queue_status_index
6354-- Covers 'new' and 'retry' states
6455CREATE INDEX IF NOT EXISTS " task_queue_status_idx"
@@ -97,7 +88,7 @@ CREATE OR REPLACE FUNCTION ${"schema"}.queue_task_status(
9788) RETURNS ${" schema" }.task_status AS $$
9889 SELECT CASE
9990 WHEN started_at IS NOT NULL AND finished_at IS NOT NULL THEN ' done' ::${" schema" }.task_status
100- WHEN dies_at < NOW() THEN ' expired' ::${" schema" }.task_status
91+ WHEN started_at IS NOT NULL AND finished_at IS NULL AND dies_at IS NOT NULL AND dies_at < NOW() THEN ' expired' ::${" schema" }.task_status
10192 WHEN started_at IS NULL AND finished_at IS NULL AND retries = 0 THEN ' failed' ::${" schema" }.task_status
10293 WHEN started_at IS NULL AND finished_at IS NULL AND retries = initial_retries
10394 THEN ' new' ::${" schema" }.task_status
@@ -111,30 +102,26 @@ $$ LANGUAGE SQL STABLE;
111102CREATE OR REPLACE FUNCTION ${" schema" }.queue_insert(q TEXT , p JSONB, delayed_at TIMESTAMPTZ ) RETURNS BIGINT AS $$
112103DECLARE
113104 v_retries INTEGER ;
114- v_dies_at TIMESTAMPTZ ;
115105 v_id BIGINT ;
116106BEGIN
117107 -- Get queue defaults, raise if queue doesn't exist
118108 SELECT
119- " retries" , CASE
120- WHEN delayed_at IS NULL OR delayed_at < NOW() THEN NOW() + " ttl"
121- ELSE delayed_at + " ttl"
122- END
109+ " retries"
123110 INTO STRICT
124- v_retries, v_dies_at
111+ v_retries
125112 FROM
126113 ${" schema" }." queue"
127114 WHERE
128115 " queue" = q;
129116
130117 -- Insert the task
131- INSERT INTO ${" schema" }." task" (" queue" , " payload" , " delayed_at" , " retries" , " initial_retries" , " dies_at " )
118+ INSERT INTO ${" schema" }." task" (" queue" , " payload" , " delayed_at" , " retries" , " initial_retries" )
132119 VALUES (
133120 q, p, CASE
134121 WHEN delayed_at IS NULL THEN NULL
135122 WHEN delayed_at < NOW() THEN NOW()
136123 ELSE delayed_at
137- END, v_retries, v_retries, v_dies_at
124+ END, v_retries, v_retries
138125 )
139126 RETURNING " id" INTO v_id;
140127
@@ -163,13 +150,10 @@ CREATE OR REPLACE TRIGGER queue_insert_trigger
163150
164151-- pgqueue.queue_lock_func
165152CREATE OR REPLACE FUNCTION ${" schema" }.queue_lock(q TEXT [], w TEXT ) RETURNS BIGINT AS $$
166- UPDATE ${" schema" }." task" SET
167- " started_at" = NOW(),
168- " worker" = w,
169- " result" = ' null'
170- WHERE " id" = (
153+ WITH selected AS (
171154 SELECT
172- t." id"
155+ t." id" ,
156+ queue." ttl"
173157 FROM
174158 ${" schema" }." task" t
175159 JOIN
@@ -195,8 +179,6 @@ WHERE "id" = (
195179 (CARDINALITY(q) = 0 OR t." queue" = ANY(q))
196180 AND
197181 (t." started_at" IS NULL AND t." finished_at" IS NULL )
198- AND
199- t." dies_at" > NOW()
200182 AND
201183 (t." delayed_at" IS NULL OR t." delayed_at" <= NOW())
202184 AND
@@ -207,14 +189,22 @@ WHERE "id" = (
207189 t." created_at"
208190 FOR UPDATE OF t SKIP LOCKED LIMIT 1
209191)
210- RETURNING " id" ;
192+ UPDATE ${" schema" }." task" SET
193+ " started_at" = NOW(),
194+ " worker" = w,
195+ " result" = ' null' ,
196+ " dies_at" = NOW() + selected." ttl"
197+ FROM selected
198+ WHERE ${" schema" }." task" ." id" = selected." id"
199+ RETURNING ${" schema" }." task" ." id" ;
211200$$ LANGUAGE SQL;
212201
213202-- pgqueue.queue_unlock_func
214203CREATE OR REPLACE FUNCTION ${" schema" }.queue_unlock(tid BIGINT , r JSONB) RETURNS BIGINT AS $$
215204 UPDATE ${" schema" }." task" SET
216205 " finished_at" = NOW(),
217- " result" = r
206+ " result" = r,
207+ " dies_at" = NULL
218208 WHERE
219209 " id" = tid
220210 AND
@@ -246,7 +236,8 @@ CREATE OR REPLACE FUNCTION ${"schema"}.queue_fail(tid BIGINT, r JSONB) RETURNS B
246236 " result" = r,
247237 " started_at" = NULL ,
248238 " finished_at" = NULL ,
249- " delayed_at" = ${" schema" }.queue_backoff(tid)
239+ " delayed_at" = ${" schema" }.queue_backoff(tid),
240+ " dies_at" = NULL
250241 WHERE
251242 " id" = tid
252243 AND
0 commit comments