From 7667b545cbdf2a40627e1b8e9b0d1f1881158033 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 15 May 2019 19:12:26 +0200 Subject: [PATCH] Add on(Final)?Failure callbacks to AbstractBackgroundJob --- .../BackgroundQueueHandler.class.php | 3 +- .../job/AbstractBackgroundJob.class.php | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php index bddbfd354f..5d26a83764 100644 --- a/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php +++ b/wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php @@ -104,7 +104,6 @@ class BackgroundQueueHandler extends SingletonFactory { throw $e; } - // gotta catch 'em all $job->fail(); if ($job->getFailures() <= $job::MAX_FAILURES) { @@ -115,6 +114,8 @@ class BackgroundQueueHandler extends SingletonFactory { } } else { + $job->onFinalFailure(); + // job failed too often: log \wcf\functions\exception\logThrowable($e); } diff --git a/wcfsetup/install/files/lib/system/background/job/AbstractBackgroundJob.class.php b/wcfsetup/install/files/lib/system/background/job/AbstractBackgroundJob.class.php index d8c3f12dc0..b48116bd1f 100644 --- a/wcfsetup/install/files/lib/system/background/job/AbstractBackgroundJob.class.php +++ b/wcfsetup/install/files/lib/system/background/job/AbstractBackgroundJob.class.php @@ -40,6 +40,8 @@ abstract class AbstractBackgroundJob { */ public final function fail() { $this->failures++; + + $this->onFailure(); } /** @@ -57,4 +59,31 @@ abstract class AbstractBackgroundJob { * cronjob comes along). */ abstract public function perform(); + + /** + * Called when the job failed. + * + * Note: This method MUST NOT throw any exceptions. Doing so will lead to this job immediately + * being failed completely. + * + * @see AbstractBackgroundJob::fail() + */ + public function onFailure() { + // empty + } + + /** + * Called when the job failed too often. This method can be used to perform additional + * logging for highly important jobs (e.g. into a dedicated failed_jobs table). + * + * Note: This method MUST NOT throw any exceptions. Doing so will lead to this job immediately + * being failed completely. + * + * Note: Both onFailure() and onFinalFailure() will be called on the final failure. + * + * @see AbstractBackgroundJob::onFailure() + */ + public function onFinalFailure() { + // empty + } } -- 2.20.1