@@ -51,38 +51,47 @@ public final class PostgresJobQueue: JobQueueDriver {
51
51
case remove
52
52
}
53
53
54
+ /// Job priority
55
+ public enum JobPriority : Int16 , Sendable , PostgresCodable {
56
+ case lowest = 0
57
+ case lower = 1
58
+ case normal = 2
59
+ case higher = 3
60
+ case highest = 4
61
+ }
62
+
54
63
/// Options for job pushed to queue
55
64
public struct JobOptions : JobOptionsProtocol {
56
65
/// Delay running job until
57
66
public var delayUntil : Date
58
- /// Priority for this jobs highest priority 0 to 9, lowest priority
59
- public var priority : Int16
67
+ /// Priority for this job
68
+ public var priority : JobPriority
60
69
61
70
/// Default initializer for JobOptions
62
71
public init ( ) {
63
72
self . delayUntil = . now
64
- self . priority = 0
73
+ self . priority = . lowest
65
74
}
66
75
67
76
/// Initializer for JobOptions
68
77
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
69
78
public init ( delayUntil: Date ? ) {
70
79
self . delayUntil = delayUntil ?? . now
71
- self . priority = 0
80
+ self . priority = . normal
72
81
}
73
82
74
83
/// Initializer for JobOptions
75
84
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
76
85
/// - Parameter priority: The priority for a job
77
- public init ( delayUntil: Date = . now, priority: Int16 = 0 ) {
86
+ public init ( delayUntil: Date = . now, priority: JobPriority = . normal ) {
78
87
self . delayUntil = delayUntil
79
88
self . priority = priority
80
89
}
81
90
82
91
/// Initializer for JobOptions
83
92
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
84
93
/// - Parameter priority: The priority for a job
85
- public init ( delayUntil: Date ? , priority: Int16 = 0 ) {
94
+ public init ( delayUntil: Date ? , priority: JobPriority = . normal ) {
86
95
self . delayUntil = delayUntil ?? . now
87
96
self . priority = priority
88
97
}
@@ -228,7 +237,12 @@ public final class PostgresJobQueue: JobQueueDriver {
228
237
let buffer = try self . jobRegistry. encode ( jobRequest: jobRequest)
229
238
try await self . client. withTransaction ( logger: self . logger) { connection in
230
239
try await self . updateJob ( id: id, buffer: buffer, connection: connection)
231
- try await self . addToQueue ( jobID: id, queueName: configuration. queueName, options: . init( delayUntil: options. delayUntil) , connection: connection)
240
+ try await self . addToQueue (
241
+ jobID: id,
242
+ queueName: configuration. queueName,
243
+ options: . init( delayUntil: options. delayUntil) ,
244
+ connection: connection
245
+ )
232
246
}
233
247
}
234
248
@@ -293,7 +307,7 @@ public final class PostgresJobQueue: JobQueueDriver {
293
307
FROM swift_jobs.queues
294
308
WHERE delayed_until <= NOW()
295
309
AND queue_name = \( configuration. queueName)
296
- ORDER BY priority ASC , delayed_until ASC, created_at ASC
310
+ ORDER BY priority DESC , delayed_until ASC, created_at ASC
297
311
FOR UPDATE SKIP LOCKED
298
312
LIMIT 1
299
313
)
0 commit comments