-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'bc1313d3266c77d6e83da1d5294eb9a35e9fba6f'
- Loading branch information
Lucas Nelaupe
committed
May 26, 2020
1 parent
b96454d
commit 2698fc5
Showing
29 changed files
with
687 additions
and
761 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by Lucas Nelaupe on 25/5/20. | ||
// | ||
|
||
import Foundation | ||
|
||
internal class PersisterConstraint: SimpleConstraint { | ||
|
||
private let serializer: JobInfoSerializer | ||
|
||
private let persister: JobPersister | ||
|
||
init(serializer: JobInfoSerializer, persister: JobPersister) { | ||
self.serializer = serializer | ||
self.persister = persister | ||
} | ||
|
||
override func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws { | ||
let data = try serializer.serialize(info: operation.info) | ||
persister.put(queueName: queue.name!, taskId: "job.info.uuid", data: data) | ||
} | ||
|
||
func remove(queueName: String, taskId: String) { | ||
|
||
persister.remove(queueName: queueName, taskId: taskId) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// Created by Lucas Nelaupe on 26/5/20. | ||
// | ||
|
||
import Foundation | ||
|
||
internal final class RepeatConstraint: SimpleConstraint { | ||
|
||
/// Number of run maximum | ||
public let maxRun: Limit | ||
|
||
/// Time between each repetition of the job | ||
public let interval: TimeInterval | ||
|
||
/// Executor to run job in foreground or background | ||
public let executor: Executor | ||
|
||
/// Current number of run | ||
public var runCount: Double = 0 | ||
|
||
init(maxRun: Limit, interval: TimeInterval, executor: Executor) { | ||
self.maxRun = maxRun | ||
self.interval = interval | ||
self.executor = executor | ||
} | ||
|
||
override func run(operation: SqOperation) -> Bool { | ||
switch executor { | ||
case .background: | ||
return false | ||
case .foreground: | ||
return true | ||
case.any: | ||
return true | ||
} | ||
} | ||
|
||
func completionSuccess(sqOperation: SqOperation) { | ||
if case .limited(let limit) = maxRun { | ||
// Reached run limit | ||
guard runCount + 1 < limit else { | ||
sqOperation.onTerminate() | ||
return | ||
} | ||
} | ||
|
||
guard interval > 0 else { | ||
// Run immediately | ||
runCount += 1 | ||
sqOperation.run() | ||
return | ||
} | ||
|
||
// Schedule run after interval | ||
sqOperation.nextRunSchedule = Date().addingTimeInterval(interval) | ||
sqOperation.dispatchQueue.runAfter(interval, callback: { | ||
self.runCount += 1 // TODO weak | ||
sqOperation.run() | ||
}) | ||
} | ||
|
||
} | ||
|
||
/// Enum to specify background and foreground restriction | ||
public enum Executor: Int { | ||
|
||
/// Job will only run only when the app is in foreground | ||
case foreground = 0 | ||
|
||
/// Job will only run only when the app is in background | ||
case background = 1 | ||
|
||
/// Job can run in both background and foreground | ||
case any = 2 | ||
|
||
} | ||
|
||
internal extension Executor { | ||
|
||
static func fromRawValue(value: Int) -> Executor { | ||
assert(value == 0 || value == 1 || value == 2) | ||
switch value { | ||
case 1: | ||
return Executor.background | ||
case 2: | ||
return Executor.any | ||
default: | ||
return Executor.foreground | ||
} | ||
} | ||
|
||
} | ||
|
||
extension Executor: Codable { | ||
|
||
private enum CodingKeys: String, CodingKey { case value } | ||
|
||
public init(from decoder: Decoder) throws { | ||
let values = try decoder.container(keyedBy: CodingKeys.self) | ||
let value = try values.decode(Int.self, forKey: .value) | ||
self = Executor.fromRawValue(value: value) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.rawValue, forKey: .value) | ||
} | ||
|
||
} |
Oops, something went wrong.