Skip to content

Commit

Permalink
Merge branch 'backuo'
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Nelaupe committed Jun 8, 2020
1 parent e547d26 commit 2a9e3c6
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 524 deletions.
31 changes: 31 additions & 0 deletions Sources/SwiftQueue/Constraint+Persister.swift
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)
}

}
38 changes: 38 additions & 0 deletions Sources/SwiftQueue/Constraint+Tag.swift
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)
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftQueue/Constraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public protocol JobConstraint {

}

protocol CodableConstraint: Encodable {
public protocol CodableConstraint: Encodable {

/**
Build constraint when deserialize
Expand Down
50 changes: 29 additions & 21 deletions Sources/SwiftQueue/JobBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public final class JobBuilder {
/// If override = true the previous job will be canceled and the new job will be scheduled
public func singleInstance(forId: String, override: Bool = false, includeExecutingJob: Bool = true) -> Self {
assertNotEmptyString(forId)
info.uuid = forId
info.override = override
info.includeExecutingJob = includeExecutingJob
info.constraints.append(UniqueUUIDConstraint(uuid: forId, override: override, includeExecutingJob: includeExecutingJob))
return self
}

Expand All @@ -62,14 +60,14 @@ public final class JobBuilder {
/// Otherwise it will wait for the remaining time
public func delay(time: TimeInterval) -> Self {
assert(time >= 0)
info.delay = time
info.constraints.append(DelayConstraint(delay: time))
return self
}

/// If the job hasn't run after the date, It will be removed
/// will call onRemove(SwiftQueueError.deadline)
public func deadline(date: Date) -> Self {
info.deadline = date
info.constraints.append(DeadlineConstraint(deadline: date))
return self
}

Expand All @@ -80,47 +78,52 @@ public final class JobBuilder {
public func periodic(limit: Limit = .unlimited, interval: TimeInterval = 0) -> Self {
assert(limit.validate)
assert(interval >= 0)
info.maxRun = limit
info.interval = interval
info.executor = .foreground
info.constraints.append(RepeatConstraint(maxRun: limit, interval: interval, executor: .foreground))
return self
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func periodic(limit: Limit = .unlimited, interval: TimeInterval = 0, executor: Executor = .foreground) -> Self {
assert(limit.validate)
assert(interval >= 0)
info.maxRun = limit
info.interval = interval
info.executor = executor
info.constraints.append(RepeatConstraint(maxRun: limit, interval: interval, executor: executor))
return self
}

/// Connectivity constraint.
public func internet(atLeast: NetworkType) -> Self {
assert(atLeast != .any)
info.requireNetwork = atLeast
info.constraints.append(NetworkConstraint(networkType: atLeast))
return self
}

private var requirePersist = false

/// Job should be persisted.
public func persist() -> Self {
info.isPersisted = true
requirePersist = true
return self
}

/// Limit number of retry. Overall for the lifecycle of the SwiftQueueManager.
/// For a periodic job, the retry count will not be reset at each period.
public func retry(limit: Limit) -> Self {
assert(limit.validate)
info.retries = limit
info.constraints.append(JobRetryConstraint(limit: limit))
return self
}

/// Custom tag to mark the job
public func addTag(tag: String) -> Self {
assertNotEmptyString(tag)
info.tags.insert(tag)
if let constraint: TagConstraint = getConstraint(info.constraints) {
constraint.insert(tag: tag)
return self
}

var set = Set<String>()
set.insert(tag)

info.constraints.append(TagConstraint(tags: set))
return self
}

Expand All @@ -142,15 +145,15 @@ public final class JobBuilder {
return self
}

/// Call if the job can only run when the device is charging
/// Call if job can only run when the device is charging
public func requireCharging() -> Self {
info.requireCharging = true
info.constraints.append(BatteryChargingConstraint())
return self
}

/// Maximum time in second that the job is allowed to run
public func timeout(value: TimeInterval) -> Self {
info.timeout = value
info.constraints.append(TimeoutConstraint(timeout: value))
return self
}

Expand All @@ -166,11 +169,16 @@ public final class JobBuilder {

/// Add job to the JobQueue
public func schedule(manager: SwiftQueueManager) {
if info.isPersisted {
// Check if we will be able to serialize args
if requirePersist {
let constraint: UniqueUUIDConstraint? = getConstraint(info)
if constraint == nil {
info.constraints.append(UniqueUUIDConstraint(uuid: UUID().uuidString, override: false, includeExecutingJob: false))
}
assert(JSONSerialization.isValidJSONObject(info.params))
info.constraints.append(PersisterConstraint(serializer: manager.params.serializer, persister: manager.params.persister))
}

manager.enqueue(info: info)
}

}
Loading

0 comments on commit 2a9e3c6

Please sign in to comment.