-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathJobRuns.php
More file actions
81 lines (70 loc) · 2.46 KB
/
Copy pathJobRuns.php
File metadata and controls
81 lines (70 loc) · 2.46 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\BackgroundJob;
use OCP\BackgroundJob\IJobRuns;
use OCP\BackgroundJob\JobRun;
use OCP\BackgroundJob\JobStatus;
use OCP\IDBConnection;
use OCP\Snowflake\ISnowflakeDecoder;
use OCP\Snowflake\ISnowflakeGenerator;
use Override;
final readonly class JobRuns implements IJobRuns {
private const TABLE = 'job_runs';
public function __construct(
private IDBConnection $connection,
private ISnowflakeGenerator $snowflakeGenerator,
private ISnowflakeDecoder $snowflakeDecoder,
private JobClassesRegistry $classesRegistry,
) {
}
// TODO Move it to runner when refactoring
public function started(int|string $classId): string {
$id = $this->snowflakeGenerator->nextId();
$qb = $this->connection->getQueryBuilder();
$qb
->insert(self::TABLE)
->setValue('run_id', $id)
->setValue('class_id', $qb->createNamedParameter($classId))
->setValue('pid', $qb->createNamedParameter(posix_getpid()))
->setValue('status', $qb->createNamedParameter(JobStatus::RUNNING->value))
->executeStatement();
return $id;
}
// 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
->update(self::TABLE)
->set('status', $qb->createNamedParameter($status->value))
->set('duration', $qb->createNamedParameter($duration))
->set('ram_peak_usage', $qb->createNamedParameter($memoryPeakUsage))
->where($qb->expr()->eq('run_id', $qb->createNamedParameter($runId)))
->executeStatement();
return $result === 1;
}
#[Override]
public function runningJobs(int $limit = 200): \Generator {
$qb = $this->connection->getQueryBuilder();
$result = $qb
->select('run_id', 'class_id', 'pid', 'status')
->from(self::TABLE)
->where($qb->expr()->eq('status', $qb->createNamedParameter(JobStatus::RUNNING->value)))
->setMaxResults($limit)
->executeQuery();
foreach ($result->iterateAssociative() as $row) {
$snowflakeInfo = $this->snowflakeDecoder->decode((string)$row['run_id']);
yield new JobRun(
$row['run_id'],
$this->classesRegistry->getName($row['class_id']),
$snowflakeInfo->getServerId(),
(int)$row['pid'],
$snowflakeInfo->getCreatedAt(),
JobStatus::from((int)$row['status']),
);
}
}
}