Skip to content

Commit

Permalink
add id
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmcd committed Jan 24, 2019
1 parent 9bdb14d commit 11906f3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Sources/Jobs/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ extension Job {
/// - Parameters:
/// - key: The persistence key specified by the end-user
/// - maxRetryCount: The maxRetryCount for the job
/// - id: A unique ID for the job
/// - Returns: A string representing the job. Will be `nil` if there is an encoding error.
public func stringValue(key: String, maxRetryCount: Int) -> String? {
let jobData = JobData(key: key, data: self, maxRetryCount: maxRetryCount)
public func stringValue(key: String, maxRetryCount: Int, id: String) -> String? {
let jobData = JobData(key: key, data: self, maxRetryCount: maxRetryCount, id: id)
guard let data = try? JSONEncoder().encode(jobData) else { return nil }
guard let jobString = String(data: data, encoding: .utf8) else { return nil }
return jobString
Expand Down
9 changes: 7 additions & 2 deletions Sources/Jobs/JobData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ public struct JobData: Encodable {
/// The maxRetryCount for the `Job`.
var maxRetryCount: Int

/// A unique ID for the job
var id: String

/// Creates a new `JobData` holding object
///
/// - Parameters:
/// - key: See `key`
/// - data: See `data`
/// - maxRetryCount: See `maxRetryCount`
public init(key: String, data: Job, maxRetryCount: Int) {
public init(key: String, data: Job, maxRetryCount: Int, id: String) {
self.key = key
self.data = data
self.maxRetryCount = maxRetryCount
self.id = id
}

/// Coding keys for the `JobData` encodable object
Expand All @@ -32,7 +36,7 @@ public struct JobData: Encodable {
/// - data: See `data`
/// - maxRetryCount: See `maxRetryCount`
enum CodingKeys: String, CodingKey {
case key, type, data, maxRetryCount
case key, type, data, maxRetryCount, id
}

/// Encodes a new `JobData` object from a given `Encoder`.
Expand All @@ -42,6 +46,7 @@ public struct JobData: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.key, forKey: .key)
try container.encode(self.maxRetryCount, forKey: .maxRetryCount)
try container.encode(self.id, forKey: .id)
let typeString = String(describing: type(of: self.data))
try container.encode(typeString, forKey: .type)

Expand Down
18 changes: 14 additions & 4 deletions Sources/Jobs/JobsCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ public class JobsCommand: Command {
//SIGTERM
let termSignalSource = DispatchSource.makeSignalSource(signal: SIGTERM, queue: signalQueue)
termSignalSource.setEventHandler {
print("SIGTERM RECEIVED")
print("Shutting down remaining jobs.")
self.isShuttingDown = true
termSignalSource.cancel()
}
signal(SIGTERM, SIG_IGN)
termSignalSource.resume()

//SIGINT
let intSignalSource = DispatchSource.makeSignalSource(signal: SIGINT, queue: signalQueue)
intSignalSource.setEventHandler {
print("Shutting down remaining jobs.")
self.isShuttingDown = true
intSignalSource.cancel()
}
signal(SIGINT, SIG_IGN)
intSignalSource.resume()

var shutdownPromises: [EventLoopPromise<Void>] = []
for eventLoop in elg.makeIterator()! {
let sub = context.container.subContainer(on: eventLoop)
Expand Down Expand Up @@ -101,12 +111,12 @@ public class JobsCommand: Command {
//No job found, go to the next iteration
guard let jobData = jobData else { return eventLoop.future() }
let job = jobData.data
console.info("Dequeing Job", newLine: true)
console.info("Dequeing Job \(jobData.id)", newLine: true)

let futureJob = job.dequeue(context: jobContext, worker: eventLoop)
return self.firstFutureToSucceed(future: futureJob, tries: jobData.maxRetryCount, on: eventLoop)
.flatMap { _ in
guard let jobString = job.stringValue(key: key, maxRetryCount: jobData.maxRetryCount) else {
guard let jobString = job.stringValue(key: key, maxRetryCount: jobData.maxRetryCount, id: jobData.id) else {
return eventLoop.future(error: Abort(.internalServerError))
}

Expand All @@ -115,7 +125,7 @@ public class JobsCommand: Command {
.catchFlatMap { error in
console.error("Job error: \(error)", newLine: true)

guard let jobString = job.stringValue(key: key, maxRetryCount: jobData.maxRetryCount) else {
guard let jobString = job.stringValue(key: key, maxRetryCount: jobData.maxRetryCount, id: jobData.id) else {
return eventLoop.future(error: Abort(.internalServerError))
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/Jobs/JobsConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ public struct JobsConfig: Service {
/// - Returns: A `JobData` container
public func decode(from decoder: Decoder) throws -> JobData? {
enum Keys: String, CodingKey {
case key, type, data, maxRetryCount
case key, type, data, maxRetryCount, id
}

let container = try decoder.container(keyedBy: Keys.self)
let type = try container.decode(String.self, forKey: .type)
let maxRetryCount = try container.decode(Int.self, forKey: .maxRetryCount)
let key = try container.decode(String.self, forKey: .key)
let id = try container.decode(String.self, forKey: .id)

guard let jobType = storage[type] else { return nil }
let job = try jobType(container.superDecoder(forKey: .data))

return JobData(key: key, data: job, maxRetryCount: maxRetryCount)
return JobData(key: key, data: job, maxRetryCount: maxRetryCount, id: id)
}
}

0 comments on commit 11906f3

Please sign in to comment.