Skip to content

Commit 60e4fca

Browse files
committed
Keep job priority enum internal
1 parent b6b17f2 commit 60e4fca

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

Sources/JobsPostgres/PostgresJobsQueue.swift

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,38 @@ 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
54+
/// Job priority from lowest to highest
55+
public struct JobPriority: Equatable, Sendable {
56+
let rawValue: Priority
57+
58+
// Job priority
59+
enum Priority: Int16, Sendable, PostgresCodable {
60+
case lowest = 0
61+
case lower = 1
62+
case normal = 2
63+
case higher = 3
64+
case highest = 4
65+
}
66+
/// Lowest priority
67+
public static func lowest() -> JobPriority {
68+
JobPriority(rawValue: .lowest)
69+
}
70+
/// Lower priority
71+
public static func lower() -> JobPriority {
72+
JobPriority(rawValue: .lower)
73+
}
74+
/// Normal is the default priority
75+
public static func normal() -> JobPriority {
76+
JobPriority(rawValue: .normal)
77+
}
78+
/// Higher priority
79+
public static func higher() -> JobPriority {
80+
JobPriority(rawValue: .higher)
81+
}
82+
/// Higgest priority
83+
public static func highest() -> JobPriority {
84+
JobPriority(rawValue: .highest)
85+
}
6186
}
6287

6388
/// Options for job pushed to queue
@@ -70,20 +95,20 @@ public final class PostgresJobQueue: JobQueueDriver {
7095
/// Default initializer for JobOptions
7196
public init() {
7297
self.delayUntil = .now
73-
self.priority = .normal
98+
self.priority = .normal()
7499
}
75100

76101
/// Initializer for JobOptions
77102
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
78103
public init(delayUntil: Date?) {
79104
self.delayUntil = delayUntil ?? .now
80-
self.priority = .normal
105+
self.priority = .normal()
81106
}
82107

83108
/// Initializer for JobOptions
84109
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
85110
/// - Parameter priority: The priority for a job
86-
public init(delayUntil: Date = .now, priority: JobPriority = .normal) {
111+
public init(delayUntil: Date = .now, priority: JobPriority = .normal()) {
87112
self.delayUntil = delayUntil
88113
self.priority = priority
89114
}
@@ -411,7 +436,7 @@ public final class PostgresJobQueue: JobQueueDriver {
411436
try await connection.query(
412437
"""
413438
INSERT INTO swift_jobs.queues (job_id, created_at, delayed_until, queue_name, priority)
414-
VALUES (\(jobID), \(Date.now), \(options.delayUntil), \(queueName), \(options.priority))
439+
VALUES (\(jobID), \(Date.now), \(options.delayUntil), \(queueName), \(options.priority.rawValue))
415440
-- We have found an existing job with the same id, SKIP this INSERT
416441
ON CONFLICT (job_id) DO NOTHING
417442
""",

Tests/JobsPostgresTests/JobsTests.swift

Lines changed: 4 additions & 4 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: .lowest
281+
priority: .lowest()
282282
)
283283
)
284284

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

@@ -332,15 +332,15 @@ final class JobsTests: XCTestCase {
332332
try await queue.push(
333333
TestParameters(value: 20),
334334
options: .init(
335-
priority: .lower
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: .higher
343+
priority: .higher()
344344
)
345345
)
346346

0 commit comments

Comments
 (0)