Skip to content

Commit 8dbb5f0

Browse files
feat(cron): Emit event before/after background job execution
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent 4c09ba8 commit 8dbb5f0

7 files changed

Lines changed: 98 additions & 0 deletions

File tree

core/Command/Background/Job.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
namespace OC\Core\Command\Background;
1010

11+
use OCP\BackgroundJob\Events\BeforeJobExecutedEvent;
12+
use OCP\BackgroundJob\Events\JobExecutedEvent;
1113
use OCP\BackgroundJob\IJob;
1214
use OCP\BackgroundJob\IJobList;
15+
use OCP\EventDispatcher\IEventDispatcher;
1316
use Symfony\Component\Console\Command\Command;
1417
use Symfony\Component\Console\Input\InputArgument;
1518
use Symfony\Component\Console\Input\InputInterface;
@@ -19,6 +22,7 @@
1922
class Job extends Command {
2023
public function __construct(
2124
protected IJobList $jobList,
25+
private IEventDispatcher $eventDispatcher,
2226
) {
2327
parent::__construct();
2428
}
@@ -67,8 +71,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6771
$output->writeln('<error>Something went wrong when trying to retrieve Job with ID ' . $jobId . ' from database</error>');
6872
return 1;
6973
}
74+
$this->eventDispatcher->dispatchTyped(new BeforeJobExecutedEvent($job));
7075
/** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
7176
$job->execute($this->jobList);
77+
$this->eventDispatcher->dispatchTyped(new JobExecutedEvent($job));
7278
$job = $this->jobList->getById($jobId);
7379

7480
if (($job === null) || ($lastRun !== $job->getLastRun())) {

core/Command/Background/JobWorker.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
use OC\Core\Command\InterruptedException;
1212
use OC\Files\SetupManager;
13+
use OCP\BackgroundJob\Events\BeforeJobExecutedEvent;
14+
use OCP\BackgroundJob\Events\JobExecutedEvent;
1315
use OCP\BackgroundJob\IJobList;
16+
use OCP\EventDispatcher\IEventDispatcher;
1417
use OCP\ITempManager;
1518
use Psr\Log\LoggerInterface;
1619
use Symfony\Component\Console\Input\InputArgument;
@@ -25,6 +28,7 @@ public function __construct(
2528
protected LoggerInterface $logger,
2629
private ITempManager $tempManager,
2730
private SetupManager $setupManager,
31+
private IEventDispatcher $eventDispatcher,
2832
) {
2933
parent::__construct($jobList, $logger);
3034
}
@@ -125,8 +129,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
125129
$this->printJobInfo($job->getId(), $job, $output);
126130
}
127131

132+
$this->eventDispatcher->dispatchTyped(new BeforeJobExecutedEvent($job));
128133
/** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
129134
$job->execute($this->jobList);
135+
$this->eventDispatcher->dispatchTyped(new JobExecutedEvent($job));
130136

131137
$output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
132138

cron.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
require_once __DIR__ . '/lib/versioncheck.php';
1212

1313
use OCP\App\IAppManager;
14+
use OCP\BackgroundJob\Events\BeforeJobExecutedEvent;
15+
use OCP\BackgroundJob\Events\JobExecutedEvent;
1416
use OCP\BackgroundJob\IJobList;
17+
use OCP\EventDispatcher\IEventDispatcher;
1518
use OCP\IAppConfig;
1619
use OCP\IConfig;
1720
use OCP\ISession;
@@ -72,6 +75,7 @@
7275
$logger = Server::get(LoggerInterface::class);
7376
$appConfig = Server::get(IAppConfig::class);
7477
$tempManager = Server::get(ITempManager::class);
78+
$eventDispatcher = Server::get(IEventDispatcher::class);
7579

7680
$tempManager->cleanOld();
7781

@@ -166,8 +170,10 @@
166170
echo 'Starting job ' . $jobDetails . PHP_EOL;
167171
}
168172

173+
$eventDispatcher->dispatchTyped(new BeforeJobExecutedEvent($job));
169174
/** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
170175
$job->execute($jobList);
176+
$eventDispatcher->dispatchTyped(new JobExecutedEvent($job));
171177

172178
$timeAfter = time();
173179
$memoryAfter = memory_get_usage();

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@
180180
'OCP\\Authentication\\TwoFactorAuth\\TwoFactorProviderForUserUnregistered' => $baseDir . '/lib/public/Authentication/TwoFactorAuth/TwoFactorProviderForUserUnregistered.php',
181181
'OCP\\Authentication\\TwoFactorAuth\\TwoFactorProviderUserDeleted' => $baseDir . '/lib/public/Authentication/TwoFactorAuth/TwoFactorProviderUserDeleted.php',
182182
'OCP\\AutoloadNotAllowedException' => $baseDir . '/lib/public/AutoloadNotAllowedException.php',
183+
'OCP\\BackgroundJob\\Events\\BeforeJobExecutedEvent' => $baseDir . '/lib/public/BackgroundJob/Events/BeforeJobExecutedEvent.php',
184+
'OCP\\BackgroundJob\\Events\\JobExecutedEvent' => $baseDir . '/lib/public/BackgroundJob/Events/JobExecutedEvent.php',
183185
'OCP\\BackgroundJob\\IJob' => $baseDir . '/lib/public/BackgroundJob/IJob.php',
184186
'OCP\\BackgroundJob\\IJobList' => $baseDir . '/lib/public/BackgroundJob/IJobList.php',
185187
'OCP\\BackgroundJob\\IParallelAwareJob' => $baseDir . '/lib/public/BackgroundJob/IParallelAwareJob.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
229229
'OCP\\Authentication\\TwoFactorAuth\\TwoFactorProviderForUserUnregistered' => __DIR__ . '/../../..' . '/lib/public/Authentication/TwoFactorAuth/TwoFactorProviderForUserUnregistered.php',
230230
'OCP\\Authentication\\TwoFactorAuth\\TwoFactorProviderUserDeleted' => __DIR__ . '/../../..' . '/lib/public/Authentication/TwoFactorAuth/TwoFactorProviderUserDeleted.php',
231231
'OCP\\AutoloadNotAllowedException' => __DIR__ . '/../../..' . '/lib/public/AutoloadNotAllowedException.php',
232+
'OCP\\BackgroundJob\\Events\\BeforeJobExecutedEvent' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/Events/BeforeJobExecutedEvent.php',
233+
'OCP\\BackgroundJob\\Events\\JobExecutedEvent' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/Events/JobExecutedEvent.php',
232234
'OCP\\BackgroundJob\\IJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IJob.php',
233235
'OCP\\BackgroundJob\\IJobList' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IJobList.php',
234236
'OCP\\BackgroundJob\\IParallelAwareJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IParallelAwareJob.php',
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-only
8+
*/
9+
10+
namespace OCP\BackgroundJob\Events;
11+
12+
use OCP\BackgroundJob\IJob;
13+
use OCP\EventDispatcher\Event;
14+
15+
/**
16+
* Emitted before a background job is executed
17+
*
18+
* @since 32.0.0
19+
*/
20+
class BeforeJobExecutedEvent extends Event {
21+
22+
/**
23+
* @since 32.0.0
24+
*/
25+
public function __construct(
26+
private IJob $job,
27+
) {
28+
parent::__construct();
29+
}
30+
31+
/**
32+
* @since 32.0.0
33+
*/
34+
public function getJob(): IJob {
35+
return $this->job;
36+
}
37+
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-only
8+
*/
9+
10+
namespace OCP\BackgroundJob\Events;
11+
12+
use OCP\BackgroundJob\IJob;
13+
use OCP\EventDispatcher\Event;
14+
15+
/**
16+
* Emitted before a background job is executed
17+
*
18+
* @since 32.0.0
19+
*/
20+
class JobExecutedEvent extends Event {
21+
22+
/**
23+
* @since 32.0.0
24+
*/
25+
public function __construct(
26+
private IJob $job,
27+
) {
28+
parent::__construct();
29+
}
30+
31+
/**
32+
* @since 32.0.0
33+
*/
34+
public function getJob(): IJob {
35+
return $this->job;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)