From f0ee5112b904760d840bd3599aa5ef2702b51713 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Thu, 21 Aug 2014 08:53:16 +0000 Subject: [PATCH] Improved the processing of background task to enable more advanced functionalities like queuing (factorized the error handling) SVN:trunk[3302] --- core/asynctask.class.inc.php | 72 +++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/core/asynctask.class.inc.php b/core/asynctask.class.inc.php index 699c474a0..74bb54479 100644 --- a/core/asynctask.class.inc.php +++ b/core/asynctask.class.inc.php @@ -200,39 +200,7 @@ abstract class AsyncTask extends DBObject } catch(Exception $e) { - $iErrorCode = $e->getCode(); - - if ($this->Get('last_attempt') == '') - { - // First attempt - $this->Set('remaining_retries', $this->GetMaxRetries($iErrorCode)); - } - - $this->Set('last_error', $e->getMessage()); - $this->Set('last_error_code', $iErrorCode); - $this->Set('last_attempt', time()); - - $iRemaining = $this->Get('remaining_retries'); - if ($iRemaining > 0) - { - $iRetryDelay = $this->GetRetryDelay($iErrorCode); - IssueLog::Info('Failed to process async task #'.$this->GetKey().' - reason: '.$e->getMessage().' - remaining retries: '.$iRemaining.' - next retry in '.$iRetryDelay.'s'); - - $this->Set('remaining_retries', $iRemaining - 1); - $this->Set('status', 'planned'); - $this->Set('started', null); - $this->Set('planned', time() + $iRetryDelay); - } - else - { - IssueLog::Error('Failed to process async task #'.$this->GetKey().' - reason: '.$e->getMessage()); - - $this->Set('status', 'error'); - $this->Set('started', null); - $this->Set('planned', null); - $this->OnDefinitiveFailure(); - } - $this->DBUpdate(); + $this->HandleError($e->getMessage(), $e->getCode()); } } else @@ -243,6 +211,44 @@ abstract class AsyncTask extends DBObject return $bRet; } + /** + * Overridable to extend the behavior in case of error (logging) + */ + protected function HandleError($sErrorMessage, $iErrorCode) + { + if ($this->Get('last_attempt') == '') + { + // First attempt + $this->Set('remaining_retries', $this->GetMaxRetries($iErrorCode)); + } + + $this->Set('last_error', $sErrorMessage); + $this->Set('last_error_code', $iErrorCode); // Note: can be ZERO !!! + $this->Set('last_attempt', time()); + + $iRemaining = $this->Get('remaining_retries'); + if ($iRemaining > 0) + { + $iRetryDelay = $this->GetRetryDelay($iErrorCode); + IssueLog::Info('Failed to process async task #'.$this->GetKey().' - reason: '.$sErrorMessage.' - remaining retries: '.$iRemaining.' - next retry in '.$iRetryDelay.'s'); + + $this->Set('remaining_retries', $iRemaining - 1); + $this->Set('status', 'planned'); + $this->Set('started', null); + $this->Set('planned', time() + $iRetryDelay); + } + else + { + IssueLog::Error('Failed to process async task #'.$this->GetKey().' - reason: '.$sErrorMessage); + + $this->Set('status', 'error'); + $this->Set('started', null); + $this->Set('planned', null); + $this->OnDefinitiveFailure(); + } + $this->DBUpdate(); + } + /** * Throws an exception (message and code) */