Add on(Final)?Failure callbacks to AbstractBackgroundJob
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 15 May 2019 17:12:26 +0000 (19:12 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 15 May 2019 17:12:26 +0000 (19:12 +0200)
wcfsetup/install/files/lib/system/background/BackgroundQueueHandler.class.php
wcfsetup/install/files/lib/system/background/job/AbstractBackgroundJob.class.php

index bddbfd354f68da1d1ad49f7eb564a09e55e44c09..5d26a83764c558bd4bec89732e5e9457c63474fc 100644 (file)
@@ -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);
                        }
index d8c3f12dc0d1c11089422569b67877fcd64a9a22..b48116bd1f7f190b40b067befc2253b70524a127 100644 (file)
@@ -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
+       }
 }