Przeglądaj źródła

Improved the processing of background task to enable more advanced functionalities like queuing (factorized the error handling)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3302 a333f486-631f-4898-b8df-5754b55c2be0
romainq 10 lat temu
rodzic
commit
45a2329fa5
1 zmienionych plików z 39 dodań i 33 usunięć
  1. 39 33
      core/asynctask.class.inc.php

+ 39 - 33
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
@@ -244,6 +212,44 @@ abstract class AsyncTask extends DBObject
 	}
 
 	/**
+	 * 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)
 	 */	 	
 	abstract public function DoProcess();