Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
WorkerActivationService
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAsyncLocalEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ensureWorkerRunning
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 ensureNotificationWorkerRunning
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\Worker;
10
11use OCP\DB\Exception as DatabaseException;
12use Psr\Log\LoggerInterface;
13
14class WorkerActivationService {
15    public function __construct(
16        private WorkerConfiguration $workerConfiguration,
17        private LocalWorkerCoordinator $coordinator,
18        private LoggerInterface $logger,
19    ) {
20    }
21
22    public function isAsyncLocalEnabled(): bool {
23        return $this->workerConfiguration->isAsyncLocalEnabled();
24    }
25
26    /** Best effort. A failed local launch does not invalidate a persisted job. */
27    public function ensureWorkerRunning(): bool {
28        try {
29            if (!$this->isAsyncLocalEnabled()) {
30                return false;
31            }
32            return $this->coordinator->ensureCapacity($this->workerConfiguration->getDesiredWorkerCount());
33        } catch (\RuntimeException|DatabaseException $e) {
34            // A count failure is not an empty queue and must not fail signing.
35            $this->logger->warning('Unable to check or start signing workers; Nextcloud cron can process pending jobs', ['exception' => $e]);
36            return false;
37        }
38    }
39    /** Best effort; notification workers do not depend on signing_mode. */
40    public function ensureNotificationWorkerRunning(): bool {
41        try {
42            if (!$this->workerConfiguration->isNotificationLocalEnabled()) {
43                return false;
44            }
45            return $this->coordinator->ensureCapacity(
46                $this->workerConfiguration->getDesiredNotificationWorkerCount(),
47                WorkerConfiguration::NOTIFICATION,
48            );
49        } catch (\RuntimeException|DatabaseException $e) {
50            $this->logger->warning('Unable to check or start notification workers; Nextcloud cron can process pending jobs', ['exception' => $e]);
51            return false;
52        }
53    }
54}