Skip to content

Commit

Permalink
Add delay for retry
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas34 committed Oct 20, 2017
1 parent 55d5349 commit 52cc2b2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftQueue/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public protocol JobResult {
}

public enum RetryConstraint {
case retry
case retry(delay: TimeInterval)
case cancel
case exponential(initial: TimeInterval)
}
Expand Down
8 changes: 5 additions & 3 deletions Sources/SwiftQueue/SwiftQueueJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,14 @@ internal final class SwiftQueueJob: Operation, JobResult {
switch retry {
case .cancel:
cancel()
case .retry:
case .retry(let delay):
retries -= 1
run()
runInBackgroundAfter(delay) {
self.run()
}
case .exponential(let initial):
let decimal: NSDecimalNumber = NSDecimalNumber(decimal: Decimal(initial) * pow(2, max(0, runCount - 1)))
runInBackgroundAfter(Double(decimal)) {
runInBackgroundAfter(TimeInterval(decimal)) {
self.retries -= 1
self.run()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftQueueTests/ConstraintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ConstraintTests: XCTestCase {
let creator = TestCreator([type: job])

job.result = JobError()
job.retryConstraint = .retry
job.retryConstraint = .retry(delay: 0)

let manager = SwiftQueueManager(creators: [creator])
JobBuilder(type: type)
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftQueueTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TestJob: Job {
public var onCompleteCalled = 0
public var onCancelCalled = 0

public var retryConstraint = RetryConstraint.retry
public var retryConstraint = RetryConstraint.retry(delay: 0)

public var params: Any?

Expand Down

0 comments on commit 52cc2b2

Please sign in to comment.