Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom #335

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/SwiftQueue/Constraint+Deadline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ internal final class DeadlineConstraint: JobConstraint, CodableConstraint {
}

func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws {
try check(operation: operation)
try check()
}

func willRun(operation: SqOperation) throws {
try check(operation: operation)
try check()
}

func run(operation: SqOperation) -> Bool {
Expand All @@ -55,7 +55,7 @@ internal final class DeadlineConstraint: JobConstraint, CodableConstraint {
return true
}

private func check(operation: SqOperation) throws {
private func check() throws {
if deadline < Date() {
throw SwiftQueueError.deadline
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftQueue/Constraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import Foundation

internal protocol JobConstraint {
public protocol JobConstraint {

/**
- Operation will be added to the queue
Expand Down Expand Up @@ -54,12 +54,12 @@ protocol CodableConstraint: Encodable {

}

internal class SimpleConstraint: JobConstraint {
public class SimpleConstraint: JobConstraint {

func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws {}
public func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws {}

func willRun(operation: SqOperation) throws {}
public func willRun(operation: SqOperation) throws {}

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

}
56 changes: 30 additions & 26 deletions Sources/SwiftQueue/SqOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,32 @@

import Foundation

internal final class SqOperation: Operation {
public final class SqOperation: Operation {

let handler: Job
public let info: JobInfo
public let logger: SwiftQueueLogger
public let dispatchQueue: DispatchQueue

var info: JobInfo
public var nextRunSchedule: Date? = nil

let constraints: [JobConstraint]
internal let handler: Job
internal var lastError: Error?

var lastError: Error?

let logger: SwiftQueueLogger

let listener: JobListener?

let dispatchQueue: DispatchQueue

var nextRunSchedule: Date?
private let constraints: [JobConstraint]
private let listener: JobListener?

/// Current number of repetition. Transient value
internal var currentRepetition: Int = 0

override var name: String? { get { return info.uuid } set { } }
override var queuePriority: QueuePriority { get { return info.priority } set { } }
override var qualityOfService: QualityOfService { get { return info.qualityOfService } set { } }
public override var name: String? { get { info.uuid } set { } }
public override var queuePriority: QueuePriority { get { info.priority } set { } }

@available(iOS 8.0, macCatalyst 13.0, *)
public override var qualityOfService: QualityOfService { get { info.qualityOfService } set { } }

private var jobIsExecuting: Bool = false
override var isExecuting: Bool {
get { return jobIsExecuting }
public override var isExecuting: Bool {
get { jobIsExecuting }
set {
willChangeValue(forKey: "isExecuting")
jobIsExecuting = newValue
Expand All @@ -58,34 +56,40 @@ internal final class SqOperation: Operation {
}

private var jobIsFinished: Bool = false
override var isFinished: Bool {
get { return jobIsFinished }
public override var isFinished: Bool {
get { jobIsFinished }
set {
willChangeValue(forKey: "isFinished")
jobIsFinished = newValue
didChangeValue(forKey: "isFinished")
}
}

internal init(job: Job, info: JobInfo, logger: SwiftQueueLogger, listener: JobListener?, dispatchQueue: DispatchQueue) {
internal init(_ job: Job,
_ info: JobInfo,
_ logger: SwiftQueueLogger,
_ listener: JobListener?,
_ dispatchQueue: DispatchQueue,
_ constraints: [JobConstraint]
) {
self.handler = job
self.info = info
self.logger = logger
self.listener = listener
self.dispatchQueue = dispatchQueue
self.constraints = self.info.buildConstraints()
self.constraints = constraints

super.init()
}

override func start() {
public override func start() {
super.start()
logger.log(.verbose, jobId: name, message: "Job has been started by the system")
isExecuting = true
run()
}

override func cancel() {
public override func cancel() {
self.cancel(with: SwiftQueueError.canceled)
}

Expand All @@ -112,7 +116,7 @@ internal final class SqOperation: Operation {
listener?.onTerminated(job: info, result: .fail(error))
}

internal func run() {
public func run() {
if isCancelled && !isFinished {
isFinished = true
}
Expand Down Expand Up @@ -152,7 +156,7 @@ internal final class SqOperation: Operation {

extension SqOperation: JobResult {

func done(_ result: JobCompletion) {
public func done(_ result: JobCompletion) {
guard !isFinished else { return }

listener?.onAfterRun(job: info, result: result)
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftQueue/SqOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@

import Foundation

internal final class SqOperationQueue: OperationQueue {

private let params: SqManagerParams
public final class SqOperationQueue: OperationQueue {

private let creator: JobCreator
private let queue: Queue

private let dispatchQueue: DispatchQueue
private let persister: JobPersister
private let serializer: JobInfoSerializer
private let logger: SwiftQueueLogger
Expand All @@ -37,9 +36,9 @@ internal final class SqOperationQueue: OperationQueue {
private let trigger: Operation = TriggerOperation()

init(_ params: SqManagerParams, _ queue: Queue, _ isSuspended: Bool) {
self.params = params
self.queue = queue
self.creator = params.jobCreator
self.dispatchQueue = params.dispatchQueue

self.persister = params.persister
self.serializer = params.serializer
Expand All @@ -65,10 +64,11 @@ internal final class SqOperationQueue: OperationQueue {
private func loadSerializedTasks(name: String) {
persister.restore(queueName: name).compactMap { string -> SqOperation? in
do {
let info = try serializer.deserialize(json: string)
var info = try serializer.deserialize(json: string)
let job = creator.create(type: info.type, params: info.params)
let constraints = info.buildConstraints()

return SqOperation(job: job, info: info, logger: logger, listener: listener, dispatchQueue: params.dispatchQueue)
return SqOperation(job, info, logger, listener, dispatchQueue, constraints)
} catch let error {
logger.log(.error, jobId: "UNKNOWN", message: "Unable to deserialize job error=\(error.localizedDescription)")
return nil
Expand All @@ -81,7 +81,7 @@ internal final class SqOperationQueue: OperationQueue {
super.addOperation(trigger)
}

override func addOperation(_ ope: Operation) {
public override func addOperation(_ ope: Operation) {
self.addOperationInternal(ope, wait: true)
}

Expand Down
11 changes: 5 additions & 6 deletions Sources/SwiftQueue/SwiftQueueManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,14 @@ public final class SwiftQueueManager {
}

/// Schedule a job to the queue
/// TODO Need to remove this method
public func enqueue(info: JobInfo) {
let queue = getQueue(queueName: info.queueName)
let job = queue.createHandler(type: info.type, params: info.params)
var info = info
let constraints = info.buildConstraints()

let operation = SqOperation(job: job,
info: info,
logger: params.logger,
listener: params.listener,
dispatchQueue: params.dispatchQueue
)
let operation = SqOperation(job, info, params.logger, params.listener, params.dispatchQueue, constraints)

queue.addOperation(operation)
}
Expand Down Expand Up @@ -115,6 +113,7 @@ public extension SwiftQueueManager {
}

/// number of jobs for all queues
/// TODO remove this method
func jobCount() -> Int {
var count = 0
for element in manage.values {
Expand Down
4 changes: 3 additions & 1 deletion Tests/SwiftQueueTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ class MemorySerializer: JobInfoSerializer {
extension JobBuilder {

internal func build(job: Job, logger: SwiftQueueLogger = NoLogger.shared, listener: JobListener? = nil) -> SqOperation {
return SqOperation(job: job, info: build(), logger: logger, listener: listener, dispatchQueue: DispatchQueue.global(qos: DispatchQoS.QoSClass.utility))
var info = build()
let constraints = info.buildConstraints()
return SqOperation(job, info, logger, listener, DispatchQueue.global(qos: DispatchQoS.QoSClass.utility), constraints)
}

}