Skip to content

Commit

Permalink
fix retry method (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmcd authored Jun 23, 2019
1 parent bb41a64 commit 4d1be7c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Sources/Jobs/JobsWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ final class JobsWorker {

self.logger.info("Dequeing Job job_id=[\(jobStorage.id)]")
let jobRunPromise = self.eventLoop.makePromise(of: Void.self)
let futureJob = job.anyDequeue(self.context, jobStorage)
self.firstFutureToSucceed(
future: futureJob,
tries: jobStorage.maxRetryCount
).flatMapError { error in
self.firstJobToSucceed(
job: job,
jobContext: self.context,
jobStorage: jobStorage,
tries: jobStorage.maxRetryCount)
.flatMapError { error in
self.logger.error("Error: \(error) job_id=[\(jobStorage.id)]")
return job.error(self.context, error, jobStorage)
}.whenComplete { _ in
Expand All @@ -95,14 +96,20 @@ final class JobsWorker {
}
}

private func firstFutureToSucceed<T>(future: EventLoopFuture<T>, tries: Int) -> EventLoopFuture<T> {
return future.map { complete in
private func firstJobToSucceed(job: AnyJob,
jobContext: JobContext,
jobStorage: JobStorage,
tries: Int) -> EventLoopFuture<Void>
{

let futureJob = job.anyDequeue(jobContext, jobStorage)
return futureJob.map { complete in
return complete
}.flatMapError { error in
if tries == 0 {
return self.eventLoop.makeFailedFuture(error)
} else {
return self.firstFutureToSucceed(future: future, tries: tries - 1)
return self.firstJobToSucceed(job: job, jobContext: jobContext, jobStorage: jobStorage, tries: tries - 1)
}
}
}
Expand Down

0 comments on commit 4d1be7c

Please sign in to comment.