File tree Expand file tree Collapse file tree 4 files changed +35
-20
lines changed Expand file tree Collapse file tree 4 files changed +35
-20
lines changed Original file line number Diff line number Diff line change 12
12
- Change signature of Limit(Int) to Limit(Double)
13
13
14
14
#### Fix
15
+ - Delay not waiting for the remaining time (#99 )
15
16
- Deadline now cancel the job properly (#98 )
16
17
- Fix calling ` done ` after termination will remove the lastError (#97 )
17
18
- Breaking support for Swift 3.2 (#75 )
Original file line number Diff line number Diff line change @@ -15,17 +15,24 @@ internal final class DelayConstraint: JobConstraint {
15
15
}
16
16
17
17
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
28
21
}
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
30
37
}
31
38
}
Original file line number Diff line number Diff line change @@ -33,7 +33,9 @@ public final class JobBuilder {
33
33
}
34
34
35
35
/// 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
37
39
public func delay( time: TimeInterval ) -> Self {
38
40
assert ( time >= 0 )
39
41
info. delay = time
Original file line number Diff line number Diff line change @@ -159,17 +159,11 @@ extension SqOperation: JobResult {
159
159
}
160
160
161
161
// 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)
166
163
case . exponential( let initial) :
167
164
info. currentRepetition += 1
168
165
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)
173
167
}
174
168
}
175
169
@@ -247,3 +241,14 @@ extension SqOperation {
247
241
}
248
242
249
243
}
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
+ }
You can’t perform that action at this time.
0 commit comments