Catch exceptions from background jobs and log them

This commit is contained in:
Robin Appelman 2013-12-02 13:41:47 +01:00
parent c2e83e635d
commit 35bb6f7e3a
4 changed files with 30 additions and 9 deletions

View File

@ -50,6 +50,8 @@ try {
session_write_close(); session_write_close();
$logger = \OC_Log::$object;
// Don't do anything if ownCloud has not been installed // Don't do anything if ownCloud has not been installed
if (!OC_Config::getValue('installed', false)) { if (!OC_Config::getValue('installed', false)) {
exit(0); exit(0);
@ -98,7 +100,7 @@ try {
$jobList = new \OC\BackgroundJob\JobList(); $jobList = new \OC\BackgroundJob\JobList();
$jobs = $jobList->getAll(); $jobs = $jobList->getAll();
foreach ($jobs as $job) { foreach ($jobs as $job) {
$job->execute($jobList); $job->execute($jobList, $logger);
} }
} else { } else {
// We call cron.php from some website // We call cron.php from some website
@ -109,7 +111,7 @@ try {
// Work and success :-) // Work and success :-)
$jobList = new \OC\BackgroundJob\JobList(); $jobList = new \OC\BackgroundJob\JobList();
$job = $jobList->getNext(); $job = $jobList->getNext();
$job->execute($jobList); $job->execute($jobList, $logger);
$jobList->setLastJob($job); $jobList->setLastJob($job);
OC_JSON::success(); OC_JSON::success();
} }

View File

@ -9,16 +9,34 @@
namespace OC\BackgroundJob; namespace OC\BackgroundJob;
abstract class Job { abstract class Job {
/**
* @var int $id
*/
protected $id; protected $id;
/**
* @var int $lastRun
*/
protected $lastRun; protected $lastRun;
/**
* @var mixed $argument
*/
protected $argument; protected $argument;
/** /**
* @param JobList $jobList * @param JobList $jobList
* @param \OC\Log $logger
*/ */
public function execute($jobList) { public function execute($jobList, $logger = null) {
$jobList->setLastRun($this); $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); abstract protected function run($argument);

View File

@ -20,9 +20,10 @@ abstract class QueuedJob extends Job {
* run the job, then remove it from the joblist * run the job, then remove it from the joblist
* *
* @param JobList $jobList * @param JobList $jobList
* @param \OC\Log $logger
*/ */
public function execute($jobList) { public function execute($jobList, $logger = null) {
$jobList->remove($this); $jobList->remove($this);
$this->run($this->argument); parent::execute($jobList, $logger);
} }
} }

View File

@ -31,11 +31,11 @@ abstract class TimedJob extends Job {
* run the job if * run the job if
* *
* @param JobList $jobList * @param JobList $jobList
* @param \OC\Log $logger
*/ */
public function execute($jobList) { public function execute($jobList, $logger = null) {
if ((time() - $this->lastRun) > $this->interval) { if ((time() - $this->lastRun) > $this->interval) {
$jobList->setLastRun($this); parent::execute($jobList, $logger);
$this->run($this->argument);
} }
} }
} }