Skip to content

Commit 7bc2ea3

Browse files
committed
Add comments, more test cleanup
1 parent ad5096b commit 7bc2ea3

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

Sources/JobsPostgres/Migrations/UpdateJobDelay.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// UpdateJobDelay.swift
3-
// swift-jobs-postgres
4-
//
5-
// Created by Stevenson Michel on 2/17/25.
6-
//
7-
81
//===----------------------------------------------------------------------===//
92
//
103
// This source file is part of the Hummingbird server framework project

Sources/JobsPostgres/PostgresJobsQueue.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class PostgresJobQueue: JobQueueDriver {
4646
public typealias JobID = UUID
4747

4848
/// what to do with failed/processing jobs from last time queue was handled
49-
public enum JobInitialization: Sendable {
49+
public enum JobCleanup: Sendable {
5050
case doNothing
5151
case rerun
5252
case remove
@@ -73,8 +73,11 @@ public final class PostgresJobQueue: JobQueueDriver {
7373

7474
/// Queue configuration
7575
public struct Configuration: Sendable {
76+
/// Queue poll time to wait if queue empties
7677
let pollTime: Duration
7778

79+
/// Initialize configuration
80+
/// - Parameter pollTime: Queue poll time to wait if queue empties
7881
public init(
7982
pollTime: Duration = .milliseconds(100)
8083
) {
@@ -107,15 +110,27 @@ public final class PostgresJobQueue: JobQueueDriver {
107110
}
108111

109112
/// Cleanup job queues
113+
///
114+
/// This function is used to re-run or delete jobs in a certain state. Failed jobs can be
115+
/// pushed back into the pending queue to be re-run or removed. When called at startup in
116+
/// theory no job should be set to processing, or set to pending but not in the queue. but if
117+
/// your job server crashes these states are possible, so we also provide options to re-queue
118+
/// these jobs so they are run again.
119+
///
120+
/// The job queue needs to be running when you call cleanup. You can call `cleanup` with
121+
/// `failedJobs`` set to whatever you like at any point to re-queue failed jobs. Moving processing
122+
/// or pending jobs should only be done if you are certain there is nothing else processing
123+
/// the job queue.
124+
///
110125
/// - Parameters:
111126
/// - failedJobs: What to do with jobs tagged as failed
112127
/// - processingJobs: What to do with jobs tagged as processing
113128
/// - pendingJobs: What to do with jobs tagged as pending
114129
/// - Throws:
115130
public func cleanup(
116-
failedJobs: JobInitialization = .doNothing,
117-
processingJobs: JobInitialization = .doNothing,
118-
pendingJobs: JobInitialization = .doNothing
131+
failedJobs: JobCleanup = .doNothing,
132+
processingJobs: JobCleanup = .doNothing,
133+
pendingJobs: JobCleanup = .doNothing
119134
) async throws {
120135
do {
121136
self.logger.info("Waiting for JobQueue migrations")
@@ -315,7 +330,7 @@ public final class PostgresJobQueue: JobQueueDriver {
315330
return jobs
316331
}
317332

318-
func updateJobsOnInit(withStatus status: Status, onInit: JobInitialization, connection: PostgresConnection) async throws {
333+
func updateJobsOnInit(withStatus status: Status, onInit: JobCleanup, connection: PostgresConnection) async throws {
319334
switch onInit {
320335
case .remove:
321336
try await connection.query(

Tests/JobsPostgresTests/JobsTests.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ final class JobsTests: XCTestCase {
8787
/// shutdown correctly
8888
@discardableResult public func testJobQueue<T>(
8989
jobQueue: JobQueue<PostgresJobQueue>,
90-
failedJobsInitialization: PostgresJobQueue.JobInitialization = .remove,
91-
processingJobsInitialization: PostgresJobQueue.JobInitialization = .remove,
92-
pendingJobsInitialization: PostgresJobQueue.JobInitialization = .doNothing,
90+
failedJobsInitialization: PostgresJobQueue.JobCleanup = .remove,
91+
processingJobsInitialization: PostgresJobQueue.JobCleanup = .remove,
92+
pendingJobsInitialization: PostgresJobQueue.JobCleanup = .remove,
9393
revertMigrations: Bool = false,
9494
test: (JobQueue<PostgresJobQueue>) async throws -> T
9595
) async throws -> T {
@@ -138,8 +138,9 @@ final class JobsTests: XCTestCase {
138138
/// shutdown correctly
139139
@discardableResult public func testJobQueue<T>(
140140
numWorkers: Int,
141-
failedJobsInitialization: PostgresJobQueue.JobInitialization = .remove,
142-
processingJobsInitialization: PostgresJobQueue.JobInitialization = .remove,
141+
failedJobsInitialization: PostgresJobQueue.JobCleanup = .remove,
142+
processingJobsInitialization: PostgresJobQueue.JobCleanup = .remove,
143+
pendingJobsInitialization: PostgresJobQueue.JobCleanup = .remove,
143144
revertMigrations: Bool = true,
144145
function: String = #function,
145146
test: (JobQueue<PostgresJobQueue>) async throws -> T
@@ -149,6 +150,7 @@ final class JobsTests: XCTestCase {
149150
jobQueue: jobQueue,
150151
failedJobsInitialization: failedJobsInitialization,
151152
processingJobsInitialization: processingJobsInitialization,
153+
pendingJobsInitialization: pendingJobsInitialization,
152154
revertMigrations: revertMigrations,
153155
test: test
154156
)

0 commit comments

Comments
 (0)