Merge pull request #6150 from owncloud/backgroundjob-log-exception

Remove background jobs that are giving errors
This commit is contained in:
blizzz 2013-12-06 11:56:53 -08:00
commit 6a747106db
8 changed files with 136 additions and 47 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,35 @@
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());
}
$jobList->remove($this, $this->argument);
}
} }
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);
} }
} }
} }

View File

@ -8,9 +8,6 @@
namespace Test\BackgroundJob; namespace Test\BackgroundJob;
class JobRun extends \Exception {
}
/** /**
* Class DummyJobList * Class DummyJobList
* *

View File

@ -0,0 +1,59 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\BackgroundJob;
class TestJob extends \OC\BackgroundJob\Job {
private $testCase;
/**
* @var callable $callback
*/
private $callback;
/**
* @param Job $testCase
* @param callable $callback
*/
public function __construct($testCase, $callback) {
$this->testCase = $testCase;
$this->callback = $callback;
}
public function run($argument) {
$this->testCase->markRun();
$callback = $this->callback;
$callback($argument);
}
}
class Job extends \PHPUnit_Framework_TestCase {
private $run = false;
public function setUp() {
$this->run = false;
}
public function testRemoveAfterException() {
$jobList = new DummyJobList();
$job = new TestJob($this, function () {
throw new \Exception();
});
$jobList->add($job);
$this->assertCount(1, $jobList->getAll());
$job->execute($jobList);
$this->assertTrue($this->run);
$this->assertCount(0, $jobList->getAll());
}
public function markRun() {
$this->run = true;
}
}

View File

@ -9,8 +9,17 @@
namespace Test\BackgroundJob; namespace Test\BackgroundJob;
class TestQueuedJob extends \OC\BackgroundJob\QueuedJob { class TestQueuedJob extends \OC\BackgroundJob\QueuedJob {
private $testCase;
/**
* @param QueuedJob $testCase
*/
public function __construct($testCase) {
$this->testCase = $testCase;
}
public function run($argument) { public function run($argument) {
throw new JobRun(); //throw an exception so we can detect if this function is called $this->testCase->markRun();
} }
} }
@ -24,19 +33,22 @@ class QueuedJob extends \PHPUnit_Framework_TestCase {
*/ */
private $job; private $job;
private $jobRun = false;
public function markRun() {
$this->jobRun = true;
}
public function setup() { public function setup() {
$this->jobList = new DummyJobList(); $this->jobList = new DummyJobList();
$this->job = new TestQueuedJob(); $this->job = new TestQueuedJob($this);
$this->jobList->add($this->job); $this->jobList->add($this->job);
$this->jobRun = false;
} }
public function testJobShouldBeRemoved() { public function testJobShouldBeRemoved() {
try { $this->assertTrue($this->jobList->has($this->job, null));
$this->assertTrue($this->jobList->has($this->job, null)); $this->job->execute($this->jobList);
$this->job->execute($this->jobList); $this->assertTrue($this->jobRun);
$this->fail("job should have been run");
} catch (JobRun $e) {
$this->assertFalse($this->jobList->has($this->job, null));
}
} }
} }

View File

@ -9,12 +9,18 @@
namespace Test\BackgroundJob; namespace Test\BackgroundJob;
class TestTimedJob extends \OC\BackgroundJob\TimedJob { class TestTimedJob extends \OC\BackgroundJob\TimedJob {
public function __construct() { private $testCase;
/**
* @param TimedJob $testCase
*/
public function __construct($testCase) {
$this->setInterval(10); $this->setInterval(10);
$this->testCase = $testCase;
} }
public function run($argument) { public function run($argument) {
throw new JobRun(); //throw an exception so we can detect if this function is called $this->testCase->markRun();
} }
} }
@ -28,44 +34,37 @@ class TimedJob extends \PHPUnit_Framework_TestCase {
*/ */
private $job; private $job;
private $jobRun = false;
public function markRun() {
$this->jobRun = true;
}
public function setup() { public function setup() {
$this->jobList = new DummyJobList(); $this->jobList = new DummyJobList();
$this->job = new TestTimedJob(); $this->job = new TestTimedJob($this);
$this->jobList->add($this->job); $this->jobList->add($this->job);
$this->jobRun = false;
} }
public function testShouldRunAfterInterval() { public function testShouldRunAfterInterval() {
$this->job->setLastRun(time() - 12); $this->job->setLastRun(time() - 12);
try { $this->job->execute($this->jobList);
$this->job->execute($this->jobList); $this->assertTrue($this->jobRun);
$this->fail("job should have run");
} catch (JobRun $e) {
}
$this->assertTrue(true);
} }
public function testShouldNotRunWithinInterval() { public function testShouldNotRunWithinInterval() {
$this->job->setLastRun(time() - 5); $this->job->setLastRun(time() - 5);
try { $this->job->execute($this->jobList);
$this->job->execute($this->jobList); $this->assertFalse($this->jobRun);
} catch (JobRun $e) {
$this->fail("job should not have run");
}
$this->assertTrue(true);
} }
public function testShouldNotTwice() { public function testShouldNotTwice() {
$this->job->setLastRun(time() - 15); $this->job->setLastRun(time() - 15);
try { $this->job->execute($this->jobList);
$this->job->execute($this->jobList); $this->assertTrue($this->jobRun);
$this->fail("job should have run the first time"); $this->jobRun = false;
} catch (JobRun $e) { $this->job->execute($this->jobList);
try { $this->assertFalse($this->jobRun);
$this->job->execute($this->jobList);
} catch (JobRun $e) {
$this->fail("job should not have run the second time");
}
}
$this->assertTrue(true);
} }
} }