Skip to content

Commit b496ade

Browse files
committed
Use enum for job priority
1 parent 376f8c7 commit b496ade

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

Sources/JobsPostgres/Migrations/CreateSwiftJobsMigrations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct CreateSwiftJobsMigrations: DatabaseMigration {
5151
try await connection.query(
5252
"""
5353
CREATE INDEX CONCURRENTLY IF NOT EXISTS queues_delayed_until_priority_queue_name_idx
54-
ON swift_jobs.queues(priority ASC, delayed_until ASC, queue_name ASC)
54+
ON swift_jobs.queues(priority DESC, delayed_until ASC, queue_name ASC)
5555
""",
5656
logger: logger
5757
)

Sources/JobsPostgres/PostgresJobsQueue.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,47 @@ public final class PostgresJobQueue: JobQueueDriver {
5151
case remove
5252
}
5353

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+
5463
/// Options for job pushed to queue
5564
public struct JobOptions: JobOptionsProtocol {
5665
/// Delay running job until
5766
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
6069

6170
/// Default initializer for JobOptions
6271
public init() {
6372
self.delayUntil = .now
64-
self.priority = 0
73+
self.priority = .lowest
6574
}
6675

6776
/// Initializer for JobOptions
6877
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
6978
public init(delayUntil: Date?) {
7079
self.delayUntil = delayUntil ?? .now
71-
self.priority = 0
80+
self.priority = .normal
7281
}
7382

7483
/// Initializer for JobOptions
7584
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
7685
/// - 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) {
7887
self.delayUntil = delayUntil
7988
self.priority = priority
8089
}
8190

8291
/// Initializer for JobOptions
8392
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
8493
/// - Parameter priority: The priority for a job
85-
public init(delayUntil: Date?, priority: Int16 = 0) {
94+
public init(delayUntil: Date?, priority: JobPriority = .normal) {
8695
self.delayUntil = delayUntil ?? .now
8796
self.priority = priority
8897
}
@@ -228,7 +237,12 @@ public final class PostgresJobQueue: JobQueueDriver {
228237
let buffer = try self.jobRegistry.encode(jobRequest: jobRequest)
229238
try await self.client.withTransaction(logger: self.logger) { connection in
230239
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+
)
232246
}
233247
}
234248

@@ -293,7 +307,7 @@ public final class PostgresJobQueue: JobQueueDriver {
293307
FROM swift_jobs.queues
294308
WHERE delayed_until <= NOW()
295309
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
297311
FOR UPDATE SKIP LOCKED
298312
LIMIT 1
299313
)

Tests/JobsPostgresTests/JobsTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ final class JobsTests: XCTestCase {
278278
try await queue.push(
279279
TestParameters(value: 20),
280280
options: .init(
281-
priority: 9
281+
priority: .lowest
282282
)
283283
)
284284

285285
try await queue.push(
286286
TestParameters(value: 2025),
287287
options: .init(
288-
priority: 2
288+
priority: .highest
289289
)
290290
)
291291

@@ -308,7 +308,7 @@ final class JobsTests: XCTestCase {
308308
}
309309
XCTAssertEqual(jobExecutionSequence.withLockedValue { $0 }, [2025, 20])
310310
}
311-
311+
312312
func testJobPrioritiesWithDelay() async throws {
313313
struct TestParameters: JobParameters {
314314
static let jobName = "testPriorityJobsWithDelay"
@@ -332,15 +332,15 @@ final class JobsTests: XCTestCase {
332332
try await queue.push(
333333
TestParameters(value: 20),
334334
options: .init(
335-
priority: 9
335+
priority: .lower
336336
)
337337
)
338338

339339
try await queue.push(
340340
TestParameters(value: 2025),
341341
options: .init(
342342
delayUntil: Date.now.addingTimeInterval(1),
343-
priority: 2
343+
priority: .higher
344344
)
345345
)
346346

0 commit comments

Comments
 (0)