]> source.dussan.org Git - nextcloud-server.git/commitdiff
Catch exceptions from background jobs and log them
authorRobin Appelman <icewind@owncloud.com>
Mon, 2 Dec 2013 12:41:47 +0000 (13:41 +0100)
committerArthur Schiwon <blizzz@owncloud.com>
Fri, 6 Dec 2013 19:57:52 +0000 (20:57 +0100)
cron.php
lib/private/backgroundjob/job.php
lib/private/backgroundjob/queuedjob.php
lib/private/backgroundjob/timedjob.php

index 8e1a3376d53dba768a9e891adc8c3a38c1766d5a..0d2c07b2d956aa17a3fd61cfa27c47e65368f122 100644 (file)
--- a/cron.php
+++ b/cron.php
@@ -50,6 +50,8 @@ try {
 
        session_write_close();
 
+       $logger = \OC_Log::$object;
+
        // Don't do anything if ownCloud has not been installed
        if (!OC_Config::getValue('installed', false)) {
                exit(0);
@@ -98,7 +100,7 @@ try {
                $jobList = new \OC\BackgroundJob\JobList();
                $jobs = $jobList->getAll();
                foreach ($jobs as $job) {
-                       $job->execute($jobList);
+                       $job->execute($jobList, $logger);
                }
        } else {
                // We call cron.php from some website
@@ -109,7 +111,7 @@ try {
                        // Work and success :-)
                        $jobList = new \OC\BackgroundJob\JobList();
                        $job = $jobList->getNext();
-                       $job->execute($jobList);
+                       $job->execute($jobList, $logger);
                        $jobList->setLastJob($job);
                        OC_JSON::success();
                }
index 49fbffbd6849039476790058f8e7bd64b2cf3433..256165975049938b75a0e39a5f801d3b74272b44 100644 (file)
@@ -9,16 +9,34 @@
 namespace OC\BackgroundJob;
 
 abstract class Job {
+       /**
+        * @var int $id
+        */
        protected $id;
+
+       /**
+        * @var int $lastRun
+        */
        protected $lastRun;
+
+       /**
+        * @var mixed $argument
+        */
        protected $argument;
 
        /**
         * @param JobList $jobList
+        * @param \OC\Log $logger
         */
-       public function execute($jobList) {
+       public function execute($jobList, $logger = null) {
                $jobList->setLastRun($this);
-               $this->run($this->argument);
+               try {
+                       $this->run($this->argument);
+               } catch (\Exception $e) {
+                       if ($logger) {
+                               $logger->error('Error while running background job: ' . $e->getMessage());
+                       }
+               }
        }
 
        abstract protected function run($argument);
index 1714182820df3d8ba6c8e32cf5b72e4ae90eb4ae..799eac478481dece19882122961e4ea659185282 100644 (file)
@@ -20,9 +20,10 @@ abstract class QueuedJob extends Job {
         * run the job, then remove it from the joblist
         *
         * @param JobList $jobList
+        * @param \OC\Log $logger
         */
-       public function execute($jobList) {
+       public function execute($jobList, $logger = null) {
                $jobList->remove($this);
-               $this->run($this->argument);
+               parent::execute($jobList, $logger);
        }
 }
index ae9f33505abe169a5d04472e6052476831d11d2b..09e05f1d846a58577d67a4671aeee64ed8802213 100644 (file)
@@ -31,11 +31,11 @@ abstract class TimedJob extends Job {
         * run the job if
         *
         * @param JobList $jobList
+        * @param \OC\Log $logger
         */
-       public function execute($jobList) {
+       public function execute($jobList, $logger = null) {
                if ((time() - $this->lastRun) > $this->interval) {
-                       $jobList->setLastRun($this);
-                       $this->run($this->argument);
+                       parent::execute($jobList, $logger);
                }
        }
 }