@@ -55,16 +55,36 @@ public final class PostgresJobQueue: JobQueueDriver {
55
55
public struct JobOptions : JobOptionsProtocol {
56
56
/// Delay running job until
57
57
public var delayUntil : Date
58
+ /// Priority for this jobs highest priority 0 to 9, lowest priority
59
+ public var priority : Int16
58
60
59
61
/// Default initializer for JobOptions
60
62
public init ( ) {
61
63
self . delayUntil = . now
64
+ self . priority = 0
62
65
}
63
-
66
+
64
67
/// Initializer for JobOptions
65
68
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
66
69
public init ( delayUntil: Date ? ) {
67
70
self . delayUntil = delayUntil ?? . now
71
+ self . priority = 0
72
+ }
73
+
74
+ /// Initializer for JobOptions
75
+ /// - Parameter delayUntil: Whether job execution should be delayed until a later date
76
+ /// - Parameter priority: The priority for a job
77
+ public init ( delayUntil: Date = . now, priority: Int16 = 0 ) {
78
+ self . delayUntil = delayUntil
79
+ self . priority = priority
80
+ }
81
+
82
+ /// Initializer for JobOptions
83
+ /// - Parameter delayUntil: Whether job execution should be delayed until a later date
84
+ /// - Parameter priority: The priority for a job
85
+ public init ( delayUntil: Date ? , priority: Int16 = 0 ) {
86
+ self . delayUntil = delayUntil ?? . now
87
+ self . priority = priority
68
88
}
69
89
}
70
90
@@ -194,7 +214,7 @@ public final class PostgresJobQueue: JobQueueDriver {
194
214
let jobID = JobID ( )
195
215
try await self . client. withTransaction ( logger: self . logger) { connection in
196
216
try await self . add ( jobID: jobID, jobRequest: jobRequest, queueName: configuration. queueName, connection: connection)
197
- try await self . addToQueue ( jobID: jobID, queueName: configuration. queueName, delayUntil : options. delayUntil , connection: connection)
217
+ try await self . addToQueue ( jobID: jobID, queueName: configuration. queueName, options : options, connection: connection)
198
218
}
199
219
return jobID
200
220
}
@@ -208,7 +228,7 @@ public final class PostgresJobQueue: JobQueueDriver {
208
228
let buffer = try self . jobRegistry. encode ( jobRequest: jobRequest)
209
229
try await self . client. withTransaction ( logger: self . logger) { connection in
210
230
try await self . updateJob ( id: id, buffer: buffer, connection: connection)
211
- try await self . addToQueue ( jobID: id, queueName: configuration. queueName, delayUntil : options . delayUntil , connection: connection)
231
+ try await self . addToQueue ( jobID: id, queueName: configuration. queueName, options : . init ( ) , connection: connection)
212
232
}
213
233
}
214
234
@@ -273,7 +293,7 @@ public final class PostgresJobQueue: JobQueueDriver {
273
293
FROM swift_jobs.queues
274
294
WHERE delayed_until <= NOW()
275
295
AND queue_name = \( configuration. queueName)
276
- ORDER BY created_at, delayed_until ASC
296
+ ORDER BY created_at ASC , delayed_until ASC, priority ASC
277
297
FOR UPDATE SKIP LOCKED
278
298
LIMIT 1
279
299
)
@@ -381,11 +401,11 @@ public final class PostgresJobQueue: JobQueueDriver {
381
401
)
382
402
}
383
403
384
- func addToQueue( jobID: JobID , queueName: String , delayUntil : Date , connection: PostgresConnection ) async throws {
404
+ func addToQueue( jobID: JobID , queueName: String , options : JobOptions , connection: PostgresConnection ) async throws {
385
405
try await connection. query (
386
406
"""
387
- INSERT INTO swift_jobs.queues (job_id, created_at, delayed_until, queue_name)
388
- VALUES ( \( jobID) , \( Date . now) , \( delayUntil) , \( queueName) )
407
+ INSERT INTO swift_jobs.queues (job_id, created_at, delayed_until, queue_name, priority )
408
+ VALUES ( \( jobID) , \( Date . now) , \( options . delayUntil) , \( queueName) , \( options . priority ) )
389
409
-- We have found an existing job with the same id, SKIP this INSERT
390
410
ON CONFLICT (job_id) DO NOTHING
391
411
""" ,
@@ -449,7 +469,7 @@ public final class PostgresJobQueue: JobQueueDriver {
449
469
let jobs = try await getJobs ( withStatus: status)
450
470
self . logger. info ( " Moving \( jobs. count) jobs with status: \( status) to job queue " )
451
471
for jobID in jobs {
452
- try await self . addToQueue ( jobID: jobID, queueName: configuration. queueName, delayUntil : Date . now , connection: connection)
472
+ try await self . addToQueue ( jobID: jobID, queueName: configuration. queueName, options : . init ( ) , connection: connection)
453
473
}
454
474
455
475
case . doNothing:
0 commit comments