• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LibreSign / libresign / 21295982003

23 Jan 2026 05:58PM UTC coverage: 45.379%. First build
21295982003

Pull #6551

github

web-flow
Merge a37b742cc into fc4e6a680
Pull Request #6551: feat: optimize worker initialization

26 of 28 new or added lines in 3 files covered. (92.86%)

7464 of 16448 relevant lines covered (45.38%)

4.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/lib/Service/Worker/WorkerHealthService.php
1
<?php
2

3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8

9
namespace OCA\Libresign\Service\Worker;
10

11
use Psr\Log\LoggerInterface;
12

13
class WorkerHealthService {
14
        public function __construct(
15
                private WorkerConfiguration $workerConfiguration,
16
                private WorkerCounter $workerCounter,
17
                private WorkerJobCounter $workerJobCounter,
18
                private StartThrottlePolicy $startThrottlePolicy,
19
                private WorkerStarter $workerStarter,
20
                private LoggerInterface $logger,
21
        ) {
22
        }
16✔
23

24
        public function isAsyncLocalEnabled(): bool {
NEW
25
                return $this->workerConfiguration->isAsyncLocalEnabled();
×
26
        }
27

28
        public function ensureWorkerRunning(): bool {
29
                try {
30
                        if (!$this->workerConfiguration->isAsyncLocalEnabled()) {
10✔
31
                                return false;
1✔
32
                        }
33

34
                        $workersNeeded = $this->calculateWorkersNeeded();
8✔
35
                        if ($workersNeeded === 0) {
8✔
36
                                return true;
2✔
37
                        }
38

39
                        if ($this->startThrottlePolicy->isThrottled()) {
6✔
40
                                return true;
1✔
41
                        }
42

43
                        $this->startThrottlePolicy->recordAttempt();
5✔
44
                        $this->workerStarter->startWorkers($workersNeeded);
5✔
45
                        return true;
5✔
46
                } catch (\Throwable $e) {
1✔
47
                        $this->logger->error('Failed to ensure worker is running: {error}', [
1✔
48
                                'error' => $e->getMessage(),
1✔
49
                                'exception' => $e,
1✔
50
                        ]);
1✔
51
                        return false;
1✔
52
                }
53
        }
54

55
        private function calculateWorkersNeeded(): int {
56
                $desired = $this->workerConfiguration->getDesiredWorkerCount();
8✔
57
                $running = $this->workerCounter->countRunning();
8✔
58
                $pendingJobs = $this->workerJobCounter->countPendingJobs();
8✔
59

60
                if ($this->hasNoPendingWork($pendingJobs)) {
8✔
61
                        return 0;
1✔
62
                }
63

64
                return $this->limitWorkersByAvailableWork($pendingJobs, $desired, $running);
7✔
65
        }
66

67
        private function hasNoPendingWork(int $pendingJobs): bool {
68
                return $pendingJobs === 0;
8✔
69
        }
70

71
        private function limitWorkersByAvailableWork(int $pendingJobs, int $desired, int $running): int {
72
                $workersNeeded = $desired - $running;
7✔
73
                $workersLimitedByJobs = min($pendingJobs, $workersNeeded);
7✔
74
                return max(0, $workersLimitedByJobs);
7✔
75
        }
76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc