Skip to content

Fixup after retry and job option changes in swift-jobs #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Sources/JobsPostgres/PostgresJobsQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ public final class PostgresJobQueue: JobQueueDriver {
case remove
}

/// Options for job pushed to queue
public struct JobOptions: JobOptionsProtocol {
/// Delay running job until
public var delayUntil: Date

/// Default initializer for JobOptions
public init() {
self.delayUntil = .now
}

/// Initializer for JobOptions
/// - Parameter delayUntil: Whether job execution should be delayed until a later date
public init(delayUntil: Date?) {
self.delayUntil = delayUntil ?? .now
}
}

/// Errors thrown by PostgresJobQueue
public enum PostgresQueueError: Error, CustomStringConvertible {
case failedToAdd
Expand Down Expand Up @@ -186,8 +203,8 @@ public final class PostgresJobQueue: JobQueueDriver {
/// - Parameters
/// - id: Job instance ID
/// - jobRequest: Job Request
/// - options: JobOptions
public func retry<Parameters: JobParameters>(_ id: JobID, jobRequest: JobRequest<Parameters>, options: JobOptions) async throws {
/// - options: Job retry options
public func retry<Parameters: JobParameters>(_ id: JobID, jobRequest: JobRequest<Parameters>, options: JobRetryOptions) async throws {
let buffer = try self.jobRegistry.encode(jobRequest: jobRequest)
try await self.client.withTransaction(logger: self.logger) { connection in
try await self.updateJob(id: id, buffer: buffer, connection: connection)
Expand Down
16 changes: 9 additions & 7 deletions Tests/JobsPostgresTests/JobsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ final class JobsTests: XCTestCase {
),
numWorkers: numWorkers,
logger: logger,
options: .init(
maximumBackoff: 0.01,
maxJitter: 0.01,
minJitter: 0.0
)
options: .init(defaultRetryStrategy: .exponentialJitter(maxBackoff: 0.01, maxJitter: 0.01))
)
}

Expand Down Expand Up @@ -257,7 +253,10 @@ final class JobsTests: XCTestCase {
let expectation = XCTestExpectation(description: "TestJob.execute was called", expectedFulfillmentCount: 4)
struct FailedError: Error {}
try await self.testJobQueue(numWorkers: 1) { jobQueue in
jobQueue.registerJob(parameters: TestParameters.self, maxRetryCount: 3) { _, _ in
jobQueue.registerJob(
parameters: TestParameters.self,
retryStrategy: .exponentialJitter(maxAttempts: 3, maxBackoff: 0.01, maxJitter: 0.01)
) { _, _ in
expectation.fulfill()
throw FailedError()
}
Expand All @@ -281,7 +280,10 @@ final class JobsTests: XCTestCase {
let currentJobTryCount: NIOLockedValueBox<Int> = .init(0)
struct FailedError: Error {}
try await self.testJobQueue(numWorkers: 1) { jobQueue in
jobQueue.registerJob(parameters: TestParameters.self, maxRetryCount: 3) { _, _ in
jobQueue.registerJob(
parameters: TestParameters.self,
retryStrategy: .exponentialJitter(maxAttempts: 3, maxBackoff: 0.01, maxJitter: 0.01)
) { _, _ in
defer {
currentJobTryCount.withLockedValue {
$0 += 1
Expand Down
Loading