-
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.
- Loading branch information
Showing
17 changed files
with
406 additions
and
369 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// 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) | ||
let name = operation.name ?? "" | ||
let queueName = queue.name ?? "" | ||
assertNotEmptyString(name) | ||
assertNotEmptyString(queueName) | ||
persister.put(queueName: queueName, taskId: name, 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,38 @@ | ||
// | ||
// Created by Lucas Nelaupe on 26/5/20. | ||
// | ||
|
||
import Foundation | ||
|
||
internal final class TagConstraint: SimpleConstraint, CodableConstraint { | ||
|
||
internal var tags: Set<String> | ||
|
||
required init(tags: Set<String>) { | ||
self.tags = tags | ||
} | ||
|
||
convenience init?(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: TagConstraintKey.self) | ||
if container.contains(.tags) { | ||
try self.init(tags: container.decode(Set<String>.self, forKey: .tags)) | ||
} else { return nil } | ||
} | ||
|
||
func insert(tag: String) { | ||
tags.insert(tag) | ||
} | ||
|
||
func contains(tag: String) -> Bool { | ||
return tags.contains(tag) | ||
} | ||
|
||
private enum TagConstraintKey: String, CodingKey { | ||
case tags | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: TagConstraintKey.self) | ||
try container.encode(tags, forKey: .tags) | ||
} | ||
} |
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,52 @@ | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2019 Lucas Nelaupe | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import Foundation | ||
|
||
public protocol ConstraintMaker { | ||
|
||
func make(from decoder: Decoder) throws -> [JobConstraint] | ||
|
||
} | ||
|
||
class DefaultConstraintMaker: ConstraintMaker { | ||
|
||
func make(from decoder: Decoder) throws -> [JobConstraint] { | ||
var constraints: [JobConstraint] = [] | ||
|
||
#if os(iOS) | ||
if let deadline = try BatteryChargingConstraint(from: decoder) { constraints.append(deadline) } | ||
#endif | ||
|
||
if let constraint = try DeadlineConstraint(from: decoder) { constraints.append(constraint) } | ||
if let constraint = try DelayConstraint(from: decoder) { constraints.append(constraint) } | ||
if let constraint = try TimeoutConstraint(from: decoder) { constraints.append(constraint) } | ||
if let constraint = try NetworkConstraint(from: decoder) { constraints.append(constraint) } | ||
if let constraint = try RepeatConstraint(from: decoder) { constraints.append(constraint) } | ||
if let constraint = try JobRetryConstraint(from: decoder) { constraints.append(constraint) } | ||
if let constraint = try UniqueUUIDConstraint(from: decoder) { constraints.append(constraint) } | ||
if let constraint = try TagConstraint(from: decoder) { constraints.append(constraint) } | ||
|
||
return constraints | ||
} | ||
|
||
} |
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
Oops, something went wrong.