Skip to content

Commit 0a4318d

Browse files
authored
Delay not waiting for the remaining time (#99)
1 parent c80ae00 commit 0a4318d

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Change signature of Limit(Int) to Limit(Double)
1313

1414
#### Fix
15+
- Delay not waiting for the remaining time (#99)
1516
- Deadline now cancel the job properly (#98)
1617
- Fix calling `done` after termination will remove the lastError (#97)
1718
- Breaking support for Swift 3.2 (#75)

Sources/SwiftQueue/Constrains+Delay.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@ internal final class DelayConstraint: JobConstraint {
1515
}
1616

1717
func run(operation: SqOperation) -> Bool {
18-
if let delay = operation.info.delay {
19-
if Date().timeIntervalSince(operation.info.createTime) < delay {
20-
runInBackgroundAfter(delay, callback: { [weak operation] in
21-
// If the operation in already deInit, it may have been canceled
22-
// It's safe to ignore the nil check
23-
// This is mostly to prevent job retention when cancelling operation with delay
24-
operation?.run()
25-
})
26-
return false
27-
}
18+
guard let delay = operation.info.delay else {
19+
// No delay run immediately
20+
return true
2821
}
29-
return true
22+
23+
let epoch = Date().timeIntervalSince(operation.info.createTime)
24+
guard epoch < delay else {
25+
// Epoch already greater than delay
26+
return true
27+
}
28+
29+
runInBackgroundAfter(abs(epoch - delay), callback: { [weak operation] in
30+
// If the operation in already deInit, it may have been canceled
31+
// It's safe to ignore the nil check
32+
// This is mostly to prevent job retention when cancelling operation with delay
33+
operation?.run()
34+
})
35+
36+
return false
3037
}
3138
}

Sources/SwiftQueue/JobBuilder.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public final class JobBuilder {
3333
}
3434

3535
/// Delay the execution of the job.
36-
/// Only start the countdown when the job should run and not when scheduled
36+
/// Base on the job creation, when the job is supposed to run,
37+
/// If the delay is already pass (longer job before) it will run immediately
38+
/// Otherwise it will wait for the remaining time
3739
public func delay(time: TimeInterval) -> Self {
3840
assert(time >= 0)
3941
info.delay = time

Sources/SwiftQueue/SqOperation.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,11 @@ extension SqOperation: JobResult {
159159
}
160160

161161
// Retry after time in parameter
162-
runInBackgroundAfter(after) { [weak self] in
163-
self?.info.retries.decreaseValue(by: 1)
164-
self?.run()
165-
}
162+
retryInBackgroundAfter(after)
166163
case .exponential(let initial):
167164
info.currentRepetition += 1
168165
let delay = info.currentRepetition == 1 ? initial : initial * pow(2, Double(info.currentRepetition - 1))
169-
runInBackgroundAfter(delay) { [weak self] in
170-
self?.info.retries.decreaseValue(by: 1)
171-
self?.run()
172-
}
166+
retryInBackgroundAfter(delay)
173167
}
174168
}
175169

@@ -247,3 +241,14 @@ extension SqOperation {
247241
}
248242

249243
}
244+
245+
extension SqOperation {
246+
247+
private func retryInBackgroundAfter(_ delay: TimeInterval) {
248+
runInBackgroundAfter(delay) { [weak self] in
249+
self?.info.retries.decreaseValue(by: 1)
250+
self?.run()
251+
}
252+
}
253+
254+
}

0 commit comments

Comments
 (0)