|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\ServerInfo; |
| 11 | + |
| 12 | +use OCP\IDBConnection; |
| 13 | + |
| 14 | +class JobQueueInfo { |
| 15 | + private const STUCK_THRESHOLD_SECONDS = 12 * 3600; |
| 16 | + |
| 17 | + public function __construct( |
| 18 | + private IDBConnection $db, |
| 19 | + ) { |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @return array{ |
| 24 | + * total: int, |
| 25 | + * reserved: int, |
| 26 | + * stuck: int, |
| 27 | + * oldestLastRun: int, |
| 28 | + * topClasses: list<array{class: string, count: int}> |
| 29 | + * } |
| 30 | + */ |
| 31 | + public function getJobQueueInfo(): array { |
| 32 | + return [ |
| 33 | + 'total' => $this->countTotal(), |
| 34 | + 'reserved' => $this->countReserved(), |
| 35 | + 'stuck' => $this->countStuck(), |
| 36 | + 'oldestLastRun' => $this->oldestLastRun(), |
| 37 | + 'topClasses' => $this->topClasses(5), |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + private function countTotal(): int { |
| 42 | + $qb = $this->db->getQueryBuilder(); |
| 43 | + $qb->select($qb->func()->count('id')) |
| 44 | + ->from('jobs'); |
| 45 | + $result = $qb->executeQuery(); |
| 46 | + $count = (int)$result->fetchOne(); |
| 47 | + $result->closeCursor(); |
| 48 | + return $count; |
| 49 | + } |
| 50 | + |
| 51 | + private function countReserved(): int { |
| 52 | + $qb = $this->db->getQueryBuilder(); |
| 53 | + $qb->select($qb->func()->count('id')) |
| 54 | + ->from('jobs') |
| 55 | + ->where($qb->expr()->gt('reserved_at', $qb->createNamedParameter(0))); |
| 56 | + $result = $qb->executeQuery(); |
| 57 | + $count = (int)$result->fetchOne(); |
| 58 | + $result->closeCursor(); |
| 59 | + return $count; |
| 60 | + } |
| 61 | + |
| 62 | + private function countStuck(): int { |
| 63 | + $threshold = time() - self::STUCK_THRESHOLD_SECONDS; |
| 64 | + $qb = $this->db->getQueryBuilder(); |
| 65 | + $qb->select($qb->func()->count('id')) |
| 66 | + ->from('jobs') |
| 67 | + ->where($qb->expr()->gt('reserved_at', $qb->createNamedParameter(0))) |
| 68 | + ->andWhere($qb->expr()->lt('reserved_at', $qb->createNamedParameter($threshold))); |
| 69 | + $result = $qb->executeQuery(); |
| 70 | + $count = (int)$result->fetchOne(); |
| 71 | + $result->closeCursor(); |
| 72 | + return $count; |
| 73 | + } |
| 74 | + |
| 75 | + private function oldestLastRun(): int { |
| 76 | + $qb = $this->db->getQueryBuilder(); |
| 77 | + $qb->select($qb->func()->min('last_run')) |
| 78 | + ->from('jobs') |
| 79 | + ->where($qb->expr()->gt('last_run', $qb->createNamedParameter(0))); |
| 80 | + $result = $qb->executeQuery(); |
| 81 | + $min = $result->fetchOne(); |
| 82 | + $result->closeCursor(); |
| 83 | + return $min === false || $min === null ? 0 : (int)$min; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return list<array{class: string, count: int}> |
| 88 | + */ |
| 89 | + private function topClasses(int $limit): array { |
| 90 | + $qb = $this->db->getQueryBuilder(); |
| 91 | + $qb->select('class') |
| 92 | + ->selectAlias($qb->func()->count('id'), 'count') |
| 93 | + ->from('jobs') |
| 94 | + ->groupBy('class') |
| 95 | + ->orderBy('count', 'DESC') |
| 96 | + ->setMaxResults($limit); |
| 97 | + $result = $qb->executeQuery(); |
| 98 | + $out = []; |
| 99 | + while (($row = $result->fetch()) !== false) { |
| 100 | + $out[] = [ |
| 101 | + 'class' => (string)($row['class'] ?? ''), |
| 102 | + 'count' => (int)($row['count'] ?? 0), |
| 103 | + ]; |
| 104 | + } |
| 105 | + $result->closeCursor(); |
| 106 | + return $out; |
| 107 | + } |
| 108 | +} |
0 commit comments