@@ -112,6 +112,7 @@ public final class PostgresJobQueue: JobQueueDriver {
112
112
await migrations. add ( CreateJobQueue ( ) )
113
113
await migrations. add ( CreateJobQueueMetadata ( ) )
114
114
await migrations. add ( CreateJobDelay ( ) )
115
+ await migrations. add ( UpdateJobDelay ( ) )
115
116
}
116
117
117
118
/// Run on initialization of the job queue
@@ -130,7 +131,12 @@ public final class PostgresJobQueue: JobQueueDriver {
130
131
try await self . updateJobsOnInit ( withStatus: . failed, onInit: self . configuration. failedJobsInitialization, connection: connection)
131
132
}
132
133
} catch let error as PSQLError {
133
- print ( " \( String ( reflecting: error) ) " )
134
+ logger. error (
135
+ " JobQueue initialization failed " ,
136
+ metadata: [
137
+ " error " : " \( String ( reflecting: error) ) "
138
+ ]
139
+ )
134
140
throw error
135
141
}
136
142
}
@@ -194,17 +200,19 @@ public final class PostgresJobQueue: JobQueueDriver {
194
200
195
201
let stream = try await connection. query (
196
202
"""
197
- DELETE FROM
198
- _hb_pg_job_queue
199
- USING (
200
- SELECT job_id FROM _hb_pg_job_queue
201
- WHERE ( delayed_until IS NULL OR delayed_until <= NOW() )
203
+ WITH next_job AS (
204
+ SELECT
205
+ job_id
206
+ FROM _hb_pg_job_queue
207
+ WHERE delayed_until <= NOW()
202
208
ORDER BY createdAt, delayed_until ASC
203
- LIMIT 1
204
209
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
210
+ LIMIT 1
211
+ )
212
+ DELETE FROM
213
+ _hb_pg_job_queue
214
+ WHERE job_id = (SELECT job_id FROM next_job)
215
+ RETURNING job_id
208
216
""" ,
209
217
logger: self . logger
210
218
)
@@ -214,7 +222,7 @@ public final class PostgresJobQueue: JobQueueDriver {
214
222
}
215
223
// select job from job table
216
224
let stream2 = try await connection. query (
217
- " SELECT job FROM _hb_pg_jobs WHERE id = \( jobId) FOR UPDATE SKIP LOCKED " ,
225
+ " SELECT job FROM _hb_pg_jobs WHERE id = \( jobId) " ,
218
226
logger: self . logger
219
227
)
220
228
@@ -269,12 +277,13 @@ public final class PostgresJobQueue: JobQueueDriver {
269
277
}
270
278
271
279
func addToQueue( jobId: JobID , connection: PostgresConnection , delayUntil: Date ? ) async throws {
280
+ // TODO: assign Date.now in swift-jobs options?
272
281
try await connection. query (
273
282
"""
274
283
INSERT INTO _hb_pg_job_queue (job_id, createdAt, delayed_until)
275
- VALUES ( \( jobId) , \( Date . now) , \( delayUntil) )
276
- ON CONFLICT (job_id)
277
- DO UPDATE SET delayed_until = \( delayUntil )
284
+ VALUES ( \( jobId) , \( Date . now) , \( delayUntil ?? Date . now ) )
285
+ -- We have found an existing job with the same id, SKIP this INSERT
286
+ ON CONFLICT (job_id) DO NOTHING
278
287
""" ,
279
288
logger: self . logger
280
289
)
@@ -315,8 +324,6 @@ public final class PostgresJobQueue: JobQueueDriver {
315
324
)
316
325
317
326
case . rerun:
318
- guard status != . pending else { return }
319
-
320
327
let jobs = try await getJobs ( withStatus: status)
321
328
self . logger. info ( " Moving \( jobs. count) jobs with status: \( status) to job queue " )
322
329
for jobId in jobs {
0 commit comments