From 5784d96e44d05f5b67784dd80607711822be4b61 Mon Sep 17 00:00:00 2001 From: Jimmy McDermott Date: Sun, 23 Jun 2019 16:31:10 -0500 Subject: [PATCH] fix retrying --- Sources/Jobs/JobsCommand.swift | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Sources/Jobs/JobsCommand.swift b/Sources/Jobs/JobsCommand.swift index bb3bd9d..3a7179d 100644 --- a/Sources/Jobs/JobsCommand.swift +++ b/Sources/Jobs/JobsCommand.swift @@ -119,9 +119,12 @@ public class JobsCommand: Command { console.info("Dequeing Job job_id=[\(jobStorage.id)]", newLine: true) let jobRunPromise = eventLoop.newPromise(Void.self) - - let futureJob = job.anyDequeue(jobContext, jobStorage) - self.firstFutureToSucceed(future: futureJob, tries: jobStorage.maxRetryCount, on: eventLoop).catchFlatMap { error in + self.firstJobToSucceed(job: job,x + jobContext: jobContext, + jobStorage: jobStorage, + tries: jobStorage.maxRetryCount, + on: eventLoop) + .catchFlatMap { error in console.error("Error: \(error) job_id=[\(jobStorage.id)]", newLine: true) return job.error(jobContext, error, jobStorage) }.always { @@ -133,21 +136,21 @@ public class JobsCommand: Command { } } - /// Returns the first time a given future succeeds and is under the `tries` - /// - /// - Parameters: - /// - future: The future to run recursively - /// - tries: The number of tries to execute this future before returning a failure - /// - worker: An `EventLoopGroup` that can be used to generate future values - /// - Returns: The completed future, with or without an error - private func firstFutureToSucceed(future: Future, tries: Int, on worker: EventLoopGroup) -> Future { - return future.map { complete in + private func firstJobToSucceed(job: AnyJob, + jobContext: JobContext, + jobStorage: JobStorage, + tries: Int, + on worker: EventLoopGroup) -> Future + { + + let futureJob = job.anyDequeue(jobContext, jobStorage) + return futureJob.map { complete in return complete }.catchFlatMap { error in if tries == 0 { return worker.future(error: error) } else { - return self.firstFutureToSucceed(future: future, tries: tries - 1, on: worker) + return self.firstJobToSucceed(job: job, jobContext: jobContext, jobStorage: jobStorage, tries: tries - 1, on: worker) } } }