@@ -130,7 +130,9 @@ public final class PostgresJobQueue: JobQueueDriver {
130
130
try await self . updateJobsOnInit ( withStatus: . failed, onInit: self . configuration. failedJobsInitialization, connection: connection)
131
131
}
132
132
} catch let error as PSQLError {
133
- print ( " \( String ( reflecting: error) ) " )
133
+ logger. error ( " JobQueue initialization failed " , metadata: [
134
+ " error " : " \( String ( reflecting: error) ) "
135
+ ] )
134
136
throw error
135
137
}
136
138
}
@@ -194,17 +196,19 @@ public final class PostgresJobQueue: JobQueueDriver {
194
196
195
197
let stream = try await connection. query (
196
198
"""
197
- DELETE FROM
198
- _hb_pg_job_queue
199
- USING (
200
- SELECT job_id FROM _hb_pg_job_queue
199
+ WITH next_job AS (
200
+ SELECT
201
+ job_id
202
+ FROM _hb_pg_job_queue
201
203
WHERE (delayed_until IS NULL OR delayed_until <= NOW())
202
204
ORDER BY createdAt, delayed_until ASC
203
- LIMIT 1
204
205
FOR UPDATE SKIP LOCKED
205
- ) queued
206
- WHERE queued.job_id = _hb_pg_job_queue.job_id
207
- RETURNING _hb_pg_job_queue.job_id
206
+ LIMIT 1
207
+ )
208
+ DELETE FROM
209
+ _hb_pg_job_queue
210
+ WHERE job_id = (SELECT job_id FROM next_job)
211
+ RETURNING job_id
208
212
""" ,
209
213
logger: self . logger
210
214
)
@@ -214,7 +218,7 @@ public final class PostgresJobQueue: JobQueueDriver {
214
218
}
215
219
// select job from job table
216
220
let stream2 = try await connection. query (
217
- " SELECT job FROM _hb_pg_jobs WHERE id = \( jobId) FOR UPDATE SKIP LOCKED " ,
221
+ " SELECT job FROM _hb_pg_jobs WHERE id = \( jobId) " ,
218
222
logger: self . logger
219
223
)
220
224
@@ -274,7 +278,9 @@ public final class PostgresJobQueue: JobQueueDriver {
274
278
INSERT INTO _hb_pg_job_queue (job_id, createdAt, delayed_until)
275
279
VALUES ( \( jobId) , \( Date . now) , \( delayUntil) )
276
280
ON CONFLICT (job_id)
277
- DO UPDATE SET delayed_until = \( delayUntil)
281
+ DO UPDATE
282
+ SET delayed_until = COALESCE(_hb_pg_job_queue.delayed_until, EXCLUDED.delayed_until),
283
+ createdAt = \( Date . now)
278
284
""" ,
279
285
logger: self . logger
280
286
)
@@ -315,8 +321,6 @@ public final class PostgresJobQueue: JobQueueDriver {
315
321
)
316
322
317
323
case . rerun:
318
- guard status != . pending else { return }
319
-
320
324
let jobs = try await getJobs ( withStatus: status)
321
325
self . logger. info ( " Moving \( jobs. count) jobs with status: \( status) to job queue " )
322
326
for jobId in jobs {
0 commit comments