-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathCleanupBackgroundJobsJob.php
More file actions
59 lines (50 loc) · 1.59 KB
/
CleanupBackgroundJobsJob.php
File metadata and controls
59 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\BackgroundJobs;
use DateTimeImmutable;
use OC\BackgroundJob\JobRuns;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\JobStatus;
use OCP\BackgroundJob\TimedJob;
use OCP\Util;
use Override;
class CleanupBackgroundJobsJob extends TimedJob {
public function __construct(
ITimeFactory $time,
private readonly JobRuns $jobRuns,
) {
parent::__construct($time);
$this->setInterval(60 * 60);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
}
#[Override]
protected function run($argument): void {
$this->reapCrashedJobs();
// TODO Clean oldest jobs
}
private function reapCrashedJobs(): void {
$currentServerId = Util::getServerId();
foreach ($this->jobRuns->runningJobs(1000) as $job) {
if ($job->serverId !== $currentServerId) {
continue;
}
exec('ps -p ' . escapeshellarg((string)$job->pid) . ' -o cmd', $output, $result);
if (count($output) === 1 && $output[0] === 'CMD' && $result === 1) {
// Process doesn't exists anymore
$maxDuration = (new DateTimeImmutable())->diff($job->startedAt);
$maxDuration
= ($maxDuration->format('%a') * 24 * 60 * 60 * 1000)
+ ($maxDuration->format('%h') * 60 * 60 * 1000)
+ ($maxDuration->format('%m') * 60 * 1000)
+ ($maxDuration->format('%s') * 1000)
+ (int)($maxDuration->format('%f') / 1000);
$this->jobRuns->finished($job->runId, $maxDuration, 0, JobStatus::CRASHED);
}
}
}
}