protected $argument;
protected ITimeFactory $time;
protected bool $allowParallelRuns = true;
+ private ?ILogger $logger = null;
/**
* @since 15.0.0
* @since 15.0.0
*/
public function execute(IJobList $jobList, ILogger $logger = null) {
+ $this->logger = $logger;
$this->start($jobList);
}
*/
public function start(IJobList $jobList): void {
$jobList->setLastRun($this);
- $logger = \OCP\Server::get(LoggerInterface::class);
+ $logger = $this->logger ?? \OCP\Server::get(LoggerInterface::class);
if (!$this->getAllowParallelRuns() && $jobList->hasReservedJob(get_class($this))) {
$logger->debug('Skipping ' . get_class($this) . ' job with ID ' . $this->getId() . ' because another job with the same class is already running', ['app' => 'cron']);
$logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
$jobList->setExecutionTime($this, $timeTaken);
- } catch (\Exception $e) {
+ } catch (\Throwable $e) {
if ($logger) {
$logger->error('Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')', [
'app' => 'core',
/** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
protected $timeFactory;
+ private bool $ran = false;
protected function setUp(): void {
parent::setUp();
public function testHasReservedJobs() {
$this->clearJobsList();
- $job = new TestJob();
+ $job = new TestJob($this->timeFactory, $this, function () {
+ $this->assertTrue($this->instance->hasReservedJob());
+ $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
+ });
$this->instance->add($job);
$this->assertFalse($this->instance->hasReservedJob());
$job->start($this->instance);
- $this->assertTrue($this->instance->hasReservedJob());
- $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
+ $this->assertTrue($this->ran);
+ }
+
+ public function markRun() {
+ $this->ran = true;
}
}
->disableOriginalConstructor()
->getMock();
$logger->expects($this->once())
- ->method('logException')
- ->with($e);
+ ->method('error');
$this->assertCount(1, $jobList->getAll());
$job->execute($jobList, $logger);
->disableOriginalConstructor()
->getMock();
$logger->expects($this->once())
- ->method('logException')
- ->with($this->isInstanceOf(\Throwable::class));
+ ->method('error');
$this->assertCount(1, $jobList->getAll());
$job->execute($jobList, $logger);
public function testDisallowParallelRunsWithNoOtherJobs() {
$jobList = new DummyJobList();
- $job = new TestJob($this->timeFactory, $this);
+ $job = new TestJob($this->timeFactory, $this, function() {
+ });
$job->setAllowParallelRuns(false);
$jobList->add($job);
public function testAllowParallelRunsWithNoOtherJobs() {
$jobList = new DummyJobList();
- $job = new TestJob($this->timeFactory, $this);
+ $job = new TestJob($this->timeFactory, $this, function() {
+ });
$job->setAllowParallelRuns(true);
$jobList->add($job);
public function testAllowParallelRunsWithOtherJobs() {
$jobList = new DummyJobList();
- $job = new TestJob($this->timeFactory, $this);
+ $job = new TestJob($this->timeFactory, $this, function() {
+ });
$job->setAllowParallelRuns(true);
$jobList->add($job);
public function testDisallowParallelRunsWithOtherJobs() {
$jobList = new DummyJobList();
- $job = new TestJob($this->timeFactory, $this);
+ $job = new TestJob($this->timeFactory, $this, function() {
+ });
$job->setAllowParallelRuns(false);
$jobList->add($job);