feat(jobs): log job execution in CronService

Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
This commit is contained in:
Benjamin Gaussorgues 2026-05-27 12:16:42 +02:00 committed by backportbot[bot]
parent f936e54b8d
commit 75853a2210
3 changed files with 19 additions and 5 deletions

View file

@ -13,6 +13,8 @@ namespace OC\Core\Service;
use OC;
use OC\Authentication\LoginCredentials\Store;
use OC\BackgroundJob\JobClassesRegistry;
use OC\BackgroundJob\JobRuns;
use OC\DB\Connection;
use OC\Security\CSRF\TokenStorage\SessionStorage;
use OC\Session\CryptoWrapper;
@ -49,6 +51,8 @@ class CronService {
private readonly ITempManager $tempManager,
private readonly IAppConfig $appConfig,
private readonly IJobList $jobList,
private readonly JobRuns $jobRuns,
private readonly JobClassesRegistry $jobClassesRegistry,
private readonly ISetupManager $setupManager,
private readonly bool $isCLI,
) {
@ -185,20 +189,27 @@ class CronService {
break;
}
$jobDetails = get_class($job) . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
$jobClass = get_class($job);
$jobDetails = $jobClass . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')';
$this->logger->debug('CLI cron call has selected job ' . $jobDetails, ['app' => 'cron']);
$this->verboseOutput('Starting job ' . $jobDetails);
$startTime = microtime(true);
$referenceMemory = memory_get_usage();
memory_reset_peak_usage();
$jobClassId = $this->jobClassesRegistry->getId($jobClass);
$jobRunId = $this->jobRuns->started($jobClassId);
$startTime = microtime(true);
$job->start($this->jobList);
$memoryIncrease = memory_get_usage() - $referenceMemory;
$timeSpent = microtime(true) - $startTime;
$jobMemoryPeak = memory_get_peak_usage() - $referenceMemory;
$jobMemoryPeak = memory_get_peak_usage();
// TODO Job failure will never be caught here because exceptions are caught within $job->start method
// The error will only be visible in server logs.
// It should be a temporary state until a proper job runner is implemented.
$this->jobRuns->finished($jobRunId, (int)($timeSpent * 1000), (int)($jobMemoryPeak / 1024));
$cronInterval = 5 * 60;
if ($timeSpent > $cronInterval) {

View file

@ -27,7 +27,7 @@ final readonly class JobRuns implements IJobRuns {
) {
}
#[Override]
// TODO Move it to runner when refactoring
public function started(int|string $classId): string {
$id = $this->snowflakeGenerator->nextId();
$qb = $this->connection->getQueryBuilder();
@ -42,7 +42,7 @@ final readonly class JobRuns implements IJobRuns {
return $id;
}
#[Override]
// TODO Move it to runner when refactoring
public function finished(int|string $runId, int $duration, int $memoryPeakUsage, JobStatus $status = JobStatus::SUCCEEDED): bool {
$qb = $this->connection->getQueryBuilder();
$result = $qb

View file

@ -25,6 +25,7 @@ use OC\Authentication\Token\IProvider;
use OC\Authentication\TwoFactorAuth\Registry;
use OC\Avatar\AvatarManager;
use OC\BackgroundJob\JobList;
use OC\BackgroundJob\JobRuns;
use OC\Blurhash\Listener\GenerateBlurhashMetadata;
use OC\Collaboration\Collaborators\GroupPlugin;
use OC\Collaboration\Collaborators\MailByMailPlugin;
@ -170,6 +171,7 @@ use OCP\Authentication\Token\IProvider as OCPIProvider;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\AutoloadNotAllowedException;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\IJobRuns;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Reference\IReferenceManager;
@ -1319,6 +1321,7 @@ class Server extends ServerContainer implements IServerContainer {
return $c->get(FileSequence::class);
}, false);
$this->registerAlias(ISnowflakeDecoder::class, SnowflakeDecoder::class);
$this->registerAlias(IJobRuns::class, JobRuns::class);
$this->connectDispatcher();
}