Skip to content

Commit 9454f56

Browse files
authored
Merge pull request #6555 from LibreSign/backport/6551/stable32
[stable32] feat: optimize worker initialization
2 parents dc2cac9 + db18534 commit 9454f56

File tree

9 files changed

+417
-108
lines changed

9 files changed

+417
-108
lines changed

lib/Controller/FileProgressController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use OCA\Libresign\Service\FileService;
1919
use OCA\Libresign\Service\SessionService;
2020
use OCA\Libresign\Service\SignRequest\ProgressService;
21-
use OCA\Libresign\Service\WorkerHealthService;
21+
use OCA\Libresign\Service\Worker\WorkerHealthService;
2222
use OCP\AppFramework\Http;
2323
use OCP\AppFramework\Http\Attribute\ApiRoute;
2424
use OCP\AppFramework\Http\Attribute\NoAdminRequired;

lib/Controller/SignFileController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use OCA\Libresign\Service\IdentifyMethodService;
2424
use OCA\Libresign\Service\RequestMetadataService;
2525
use OCA\Libresign\Service\SignFileService;
26-
use OCA\Libresign\Service\WorkerHealthService;
26+
use OCA\Libresign\Service\Worker\WorkerHealthService;
2727
use OCP\AppFramework\Http;
2828
use OCP\AppFramework\Http\Attribute\ApiRoute;
2929
use OCP\AppFramework\Http\Attribute\NoAdminRequired;

lib/Service/AsyncSigningService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\Libresign\Db\FileMapper;
1414
use OCA\Libresign\Db\SignRequest;
1515
use OCA\Libresign\Enum\FileStatus;
16+
use OCA\Libresign\Service\Worker\WorkerHealthService;
1617
use OCP\BackgroundJob\IJobList;
1718
use OCP\IUser;
1819
use OCP\Security\ICredentialsManager;

lib/Service/Worker/WorkerHealthService.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@
1212

1313
class WorkerHealthService {
1414
public function __construct(
15-
private WorkerConfiguration $config,
16-
private WorkerCounter $counter,
17-
private StartThrottlePolicy $throttle,
18-
private WorkerStarter $starter,
15+
private WorkerConfiguration $workerConfiguration,
16+
private WorkerCounter $workerCounter,
17+
private WorkerJobCounter $workerJobCounter,
18+
private StartThrottlePolicy $startThrottlePolicy,
19+
private WorkerStarter $workerStarter,
1920
private LoggerInterface $logger,
2021
) {
2122
}
2223

24+
public function isAsyncLocalEnabled(): bool {
25+
return $this->workerConfiguration->isAsyncLocalEnabled();
26+
}
27+
2328
public function ensureWorkerRunning(): bool {
2429
try {
25-
if (!$this->config->isAsyncLocalEnabled()) {
30+
if (!$this->workerConfiguration->isAsyncLocalEnabled()) {
2631
return false;
2732
}
2833

@@ -31,12 +36,12 @@ public function ensureWorkerRunning(): bool {
3136
return true;
3237
}
3338

34-
if ($this->throttle->isThrottled()) {
39+
if ($this->startThrottlePolicy->isThrottled()) {
3540
return true;
3641
}
3742

38-
$this->throttle->recordAttempt();
39-
$this->starter->startWorkers($workersNeeded);
43+
$this->startThrottlePolicy->recordAttempt();
44+
$this->workerStarter->startWorkers($workersNeeded);
4045
return true;
4146
} catch (\Throwable $e) {
4247
$this->logger->error('Failed to ensure worker is running: {error}', [
@@ -48,8 +53,24 @@ public function ensureWorkerRunning(): bool {
4853
}
4954

5055
private function calculateWorkersNeeded(): int {
51-
$desired = $this->config->getDesiredWorkerCount();
52-
$running = $this->counter->countRunning();
53-
return max(0, $desired - $running);
56+
$desired = $this->workerConfiguration->getDesiredWorkerCount();
57+
$running = $this->workerCounter->countRunning();
58+
$pendingJobs = $this->workerJobCounter->countPendingJobs();
59+
60+
if ($this->hasNoPendingWork($pendingJobs)) {
61+
return 0;
62+
}
63+
64+
return $this->limitWorkersByAvailableWork($pendingJobs, $desired, $running);
65+
}
66+
67+
private function hasNoPendingWork(int $pendingJobs): bool {
68+
return $pendingJobs === 0;
69+
}
70+
71+
private function limitWorkersByAvailableWork(int $pendingJobs, int $desired, int $running): int {
72+
$workersNeeded = $desired - $running;
73+
$workersLimitedByJobs = min($pendingJobs, $workersNeeded);
74+
return max(0, $workersLimitedByJobs);
5475
}
5576
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 OCA\Libresign\BackgroundJob\SignFileJob;
12+
use OCA\Libresign\BackgroundJob\SignSingleFileJob;
13+
use OCP\BackgroundJob\IJobList;
14+
use Psr\Log\LoggerInterface;
15+
16+
class WorkerJobCounter {
17+
public function __construct(
18+
private IJobList $jobList,
19+
private LoggerInterface $logger,
20+
) {
21+
}
22+
23+
public function countPendingJobs(): int {
24+
try {
25+
$counts = $this->jobList->countByClass();
26+
$total = 0;
27+
28+
foreach ($counts as $row) {
29+
if ($row['class'] === SignFileJob::class || $row['class'] === SignSingleFileJob::class) {
30+
$total += $row['count'];
31+
}
32+
}
33+
34+
return $total;
35+
} catch (\Throwable $e) {
36+
$this->logger->debug('Failed to count pending jobs', [
37+
'error' => $e->getMessage(),
38+
]);
39+
return 0;
40+
}
41+
}
42+
}

lib/Service/Worker/WorkerStarter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private function clampWorkerCount(int $count): int {
4343
private function executeCommand(string $phpPath, string $occPath, array $jobClasses): void {
4444
$jobClassesArg = implode(' ', array_map('escapeshellarg', $jobClasses));
4545
$cmd = sprintf(
46-
'%s %s background-job:worker %s --stop_after=1h >> /dev/null 2>&1 &',
46+
'%s %s background-job:worker %s --stop_after=30m >> /dev/null 2>&1 &',
4747
escapeshellarg($phpPath),
4848
escapeshellarg($occPath),
4949
$jobClassesArg

lib/Service/WorkerHealthService.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)