Skip to content

Commit

Permalink
Migrate more code to constraint (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas34 authored May 29, 2020
1 parent 6d18e4d commit 27f14e2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 31 deletions.
10 changes: 5 additions & 5 deletions Sources/SwiftQueue/Constraint+Deadline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import Foundation

internal final class DeadlineConstraint: JobConstraint {

/// Cancel the job after a certain date
private let deadline: Date

init(deadline: Date) {
required init(deadline: Date) {
self.deadline = deadline
}

Expand All @@ -40,10 +41,9 @@ internal final class DeadlineConstraint: JobConstraint {

func run(operation: SqOperation) -> Bool {
operation.dispatchQueue.runAfter(deadline.timeIntervalSinceNow, callback: { [weak operation] in
guard let ope = operation else { return }
guard !ope.isFinished else { return }

ope.cancel(with: SwiftQueueError.deadline)
if operation?.isFinished != false {
operation?.cancel(with: SwiftQueueError.deadline)
}
})
return true
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftQueue/Constraint+Delay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import Foundation

internal final class DelayConstraint: SimpleConstraint {

private let delay: TimeInterval
/// Delay for the first execution of the job
internal let delay: TimeInterval

init(delay: TimeInterval) {
required init(delay: TimeInterval) {
self.delay = delay
}

Expand Down
17 changes: 17 additions & 0 deletions Sources/SwiftQueue/Constraint+Repeat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,20 @@ internal extension Executor {
}

}

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)
}

}
5 changes: 3 additions & 2 deletions Sources/SwiftQueue/Constraint+Timeout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import Foundation

internal final class TimeoutConstraint: SimpleConstraint {

private let timeout: TimeInterval
/// Auto cancel job if not completed after this time
internal let timeout: TimeInterval

init(timeout: TimeInterval) {
required init(timeout: TimeInterval) {
self.timeout = timeout
}

Expand Down
22 changes: 19 additions & 3 deletions Sources/SwiftQueue/Constraint+UniqueUUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,26 @@ import Foundation

internal final class UniqueUUIDConstraint: SimpleConstraint {

/// Unique identifier for a job
internal let uuid: String

/// Override job when scheduling a job with same uuid
/// True = Override, False = Abort job with duplicate failure
internal let override: Bool

/// Including job that are executing when scheduling with same uuid
private let includeExecutingJob: Bool

required init(uuid: String, override: Bool, includeExecutingJob: Bool) {
self.uuid = uuid
self.override = override
self.includeExecutingJob = includeExecutingJob
}

override func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws {
for ope in queue.operations where ope.name == operation.info.uuid {
for ope in queue.operations where ope.name == uuid {
if shouldAbort(ope: ope, operation: operation) {
if operation.info.override {
if override {
ope.cancel()
break
} else {
Expand All @@ -38,7 +54,7 @@ internal final class UniqueUUIDConstraint: SimpleConstraint {
}

private func shouldAbort(ope: Operation, operation: SqOperation) -> Bool {
return (ope.isExecuting && operation.info.includeExecutingJob) || !ope.isExecuting
return (ope.isExecuting && includeExecutingJob) || !ope.isExecuting
}

}
2 changes: 1 addition & 1 deletion Sources/SwiftQueue/Constraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ internal class SimpleConstraint: JobConstraint {

func willRun(operation: SqOperation) throws {}

func run(operation: SqOperation) -> Bool { return true }
func run(operation: SqOperation) -> Bool { true }

}
2 changes: 1 addition & 1 deletion Sources/SwiftQueue/JobInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public struct JobInfo {
func buildConstraints() -> [JobConstraint] {
var constraints = [JobConstraint]()

constraints.append(UniqueUUIDConstraint())
constraints.append(UniqueUUIDConstraint(uuid: uuid, override: override, includeExecutingJob: includeExecutingJob))
constraints.append(ExecutorConstraint())

if requireCharging {
Expand Down
17 changes: 0 additions & 17 deletions Sources/SwiftQueue/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,6 @@ internal extension QualityOfService {

}

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)
}

}

internal extension String {

static func fromUTF8(data: Data, key: [CodingKey] = []) throws -> String {
Expand Down

0 comments on commit 27f14e2

Please sign in to comment.