Skip to content

Commit 969987a

Browse files
committed
Job retention
1 parent 81119b9 commit 969987a

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

Sources/JobsPostgres/PostgresJobsQueue.swift

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,15 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
6565
case highest = 4
6666
}
6767
/// Lowest priority
68-
public static func lowest() -> JobPriority {
69-
JobPriority(rawValue: .lowest)
70-
}
68+
public static let lowest: JobPriority = JobPriority(rawValue: .lowest)
7169
/// Lower priority
72-
public static func lower() -> JobPriority {
73-
JobPriority(rawValue: .lower)
74-
}
70+
public static let lower: JobPriority = JobPriority(rawValue: .lower)
7571
/// Normal is the default priority
76-
public static func normal() -> JobPriority {
77-
JobPriority(rawValue: .normal)
78-
}
72+
public static let normal: JobPriority = JobPriority(rawValue: .normal)
7973
/// Higher priority
80-
public static func higher() -> JobPriority {
81-
JobPriority(rawValue: .higher)
82-
}
74+
public static let higher: JobPriority = JobPriority(rawValue: .higher)
8375
/// Higgest priority
84-
public static func highest() -> JobPriority {
85-
JobPriority(rawValue: .highest)
86-
}
76+
public static let highest: JobPriority = JobPriority(rawValue: .highest)
8777
}
8878

8979
/// Options for job pushed to queue
@@ -96,20 +86,20 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
9686
/// Default initializer for JobOptions
9787
public init() {
9888
self.delayUntil = .now
99-
self.priority = .normal()
89+
self.priority = .normal
10090
}
10191

10292
/// Initializer for JobOptions
10393
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
10494
public init(delayUntil: Date?) {
10595
self.delayUntil = delayUntil ?? .now
106-
self.priority = .normal()
96+
self.priority = .normal
10797
}
10898

10999
/// Initializer for JobOptions
110100
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
111101
/// - Parameter priority: The priority for a job
112-
public init(delayUntil: Date = .now, priority: JobPriority = .normal()) {
102+
public init(delayUntil: Date = .now, priority: JobPriority = .normal) {
113103
self.delayUntil = delayUntil
114104
self.priority = priority
115105
}
@@ -142,17 +132,25 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
142132
let pollTime: Duration
143133
/// Which Queue to push jobs into
144134
let queueName: String
135+
/// Retention policy for jobs
136+
let retentionPolicy: RetentionPolicy
145137

146138
/// Initialize configuration
147139
/// - Parameters
148140
/// - pollTime: Queue poll time to wait if queue empties
149141
/// - queueName: Name of queue we are handing
150142
public init(
151143
pollTime: Duration = .milliseconds(100),
152-
queueName: String = "default"
144+
queueName: String = "default",
145+
retentionPolicy: RetentionPolicy = .init(
146+
canceled: .init(duration: "7D"),
147+
completed: .init(duration: "7D"),
148+
failed: .init(duration: "7D")
149+
) //.keepAll()
153150
) {
154151
self.pollTime = pollTime
155152
self.queueName = queueName
153+
self.retentionPolicy = retentionPolicy
156154
}
157155
}
158156

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// RetentionPolicy.swift
3+
// swift-jobs-postgres
4+
//
5+
// Created by Stevenson Michel on 3/15/25.
6+
//
7+
8+
public struct RetentionPolicy: Sendable {
9+
10+
public struct RetainData: Sendable {
11+
/// Duration in ISO 8601 format (e.g., "P30D" for 30 days)
12+
public var duration: String
13+
14+
public init(duration: String) {
15+
self.duration = duration
16+
}
17+
}
18+
19+
// public var days: Int
20+
//
21+
// public init(days: Int) {
22+
// self.days = days
23+
// }
24+
/// Jobs with status cancelled
25+
public var canceled: RetainData
26+
/// Jobs with status completed
27+
public var completed: RetainData
28+
/// Jobs with status failed
29+
public var failed: RetainData
30+
31+
public init(canceled: RetainData, completed: RetainData, failed: RetainData) {
32+
self.canceled = canceled
33+
self.completed = completed
34+
self.failed = failed
35+
}
36+
}

Tests/JobsPostgresTests/JobsTests.swift

Lines changed: 8 additions & 8 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

@@ -717,14 +717,14 @@ final class JobsTests: XCTestCase {
717717
let resumableJob = try await queue.push(
718718
ResumableJob(),
719719
options: .init(
720-
priority: .lowest()
720+
priority: .lowest
721721
)
722722
)
723723

724724
try await queue.push(
725725
TestParameters(),
726726
options: .init(
727-
priority: .normal()
727+
priority: .normal
728728
)
729729
)
730730

@@ -793,14 +793,14 @@ final class JobsTests: XCTestCase {
793793
let cancellableJob = try await queue.push(
794794
TestParameters(value: 42),
795795
options: .init(
796-
priority: .lower()
796+
priority: .lower
797797
)
798798
)
799799

800800
try await queue.push(
801801
NoneCancelledJobParameters(value: 2025),
802802
options: .init(
803-
priority: .highest()
803+
priority: .highest
804804
)
805805
)
806806

0 commit comments

Comments
 (0)