From f9ead9140af9b58796464067b9639309d36961c0 Mon Sep 17 00:00:00 2001 From: Nolan Ehrstrom Date: Thu, 20 Jul 2023 09:53:01 -0700 Subject: [PATCH] Do not fail token until all attempts have been run --- ProcessMaker/Jobs/ErrorHandling.php | 7 +++++-- ProcessMaker/Jobs/RunScriptTask.php | 9 ++++++--- ProcessMaker/Jobs/RunServiceTask.php | 12 +++++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ProcessMaker/Jobs/ErrorHandling.php b/ProcessMaker/Jobs/ErrorHandling.php index 7343c6452f..5082493824 100644 --- a/ProcessMaker/Jobs/ErrorHandling.php +++ b/ProcessMaker/Jobs/ErrorHandling.php @@ -43,13 +43,16 @@ public function __construct( public function handleRetries($job, $exception) { $message = $exception->getMessage(); + $finalAttempt = true; if ($this->retryAttempts() > 0) { if ($job->attemptNum <= $this->retryAttempts()) { Log::info('Retry the job process. Attempt ' . $job->attemptNum . ' of ' . $this->retryAttempts() . ', Wait time: ' . $this->retryWaitTime()); $this->requeue($job); - return $message; + $finalAttempt = false; + + return [$message, $finalAttempt]; } $message = __('Job failed after :attempts total attempts', ['attempts' => $job->attemptNum]) . "\n" . $message; @@ -59,7 +62,7 @@ public function handleRetries($job, $exception) $this->sendExecutionErrorNotification($message); } - return $message; + return [$message, $finalAttempt]; } private function requeue($job) diff --git a/ProcessMaker/Jobs/RunScriptTask.php b/ProcessMaker/Jobs/RunScriptTask.php index 55f86eb244..666685f629 100644 --- a/ProcessMaker/Jobs/RunScriptTask.php +++ b/ProcessMaker/Jobs/RunScriptTask.php @@ -109,11 +109,14 @@ public function action(ProcessRequestToken $token = null, ScriptTaskInterface $e $this->instance = $instance; }); } catch (Throwable $exception) { - $token->setStatus(ScriptTaskInterface::TOKEN_STATE_FAILING); - $message = $exception->getMessage(); + $finalAttempt = true; if ($errorHandling) { - $message = $errorHandling->handleRetries($this, $exception); + [$message, $finalAttempt] = $errorHandling->handleRetries($this, $exception); + } + + if ($finalAttempt) { + $token->setStatus(ScriptTaskInterface::TOKEN_STATE_FAILING); } $error = $element->getRepository()->createError(); diff --git a/ProcessMaker/Jobs/RunServiceTask.php b/ProcessMaker/Jobs/RunServiceTask.php index 72ce729918..08e7f9dde2 100644 --- a/ProcessMaker/Jobs/RunServiceTask.php +++ b/ProcessMaker/Jobs/RunServiceTask.php @@ -73,6 +73,7 @@ public function action(ProcessRequestToken $token = null, ServiceTaskInterface $ if ($configuration === null) { $configuration = []; } + $errorHandling = null; try { if (empty($implementation)) { throw new ScriptException('Service task implementation not defined'); @@ -122,10 +123,15 @@ public function action(ProcessRequestToken $token = null, ServiceTaskInterface $ $this->instance = $instance; }); } catch (Throwable $exception) { - // Change to error status - $token->setStatus(ServiceTaskInterface::TOKEN_STATE_FAILING); + $finalAttempt = true; + if ($errorHandling) { + [$message, $finalAttempt] = $errorHandling->handleRetries($this, $exception); + } - $message = $errorHandling->handleRetries($this, $exception); + if ($finalAttempt) { + // Change to error status + $token->setStatus(ServiceTaskInterface::TOKEN_STATE_FAILING); + } $error = $element->getRepository()->createError(); $error->setName($message);