Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Controller/FileProgressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use OCA\Libresign\Service\FileService;
use OCA\Libresign\Service\SessionService;
use OCA\Libresign\Service\SignRequest\ProgressService;
use OCA\Libresign\Service\WorkerHealthService;
use OCA\Libresign\Service\Worker\WorkerHealthService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/SignFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use OCA\Libresign\Service\IdentifyMethodService;
use OCA\Libresign\Service\RequestMetadataService;
use OCA\Libresign\Service\SignFileService;
use OCA\Libresign\Service\WorkerHealthService;
use OCA\Libresign\Service\Worker\WorkerHealthService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand Down
1 change: 1 addition & 0 deletions lib/Service/AsyncSigningService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Enum\FileStatus;
use OCA\Libresign\Service\Worker\WorkerHealthService;
use OCP\BackgroundJob\IJobList;
use OCP\IUser;
use OCP\Security\ICredentialsManager;
Expand Down
43 changes: 32 additions & 11 deletions lib/Service/Worker/WorkerHealthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@

class WorkerHealthService {
public function __construct(
private WorkerConfiguration $config,
private WorkerCounter $counter,
private StartThrottlePolicy $throttle,
private WorkerStarter $starter,
private WorkerConfiguration $workerConfiguration,
private WorkerCounter $workerCounter,
private WorkerJobCounter $workerJobCounter,
private StartThrottlePolicy $startThrottlePolicy,
private WorkerStarter $workerStarter,
private LoggerInterface $logger,
) {
}

public function isAsyncLocalEnabled(): bool {
return $this->workerConfiguration->isAsyncLocalEnabled();
}

public function ensureWorkerRunning(): bool {
try {
if (!$this->config->isAsyncLocalEnabled()) {
if (!$this->workerConfiguration->isAsyncLocalEnabled()) {
return false;
}

Expand All @@ -31,12 +36,12 @@ public function ensureWorkerRunning(): bool {
return true;
}

if ($this->throttle->isThrottled()) {
if ($this->startThrottlePolicy->isThrottled()) {
return true;
}

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

private function calculateWorkersNeeded(): int {
$desired = $this->config->getDesiredWorkerCount();
$running = $this->counter->countRunning();
return max(0, $desired - $running);
$desired = $this->workerConfiguration->getDesiredWorkerCount();
$running = $this->workerCounter->countRunning();
$pendingJobs = $this->workerJobCounter->countPendingJobs();

if ($this->hasNoPendingWork($pendingJobs)) {
return 0;
}

return $this->limitWorkersByAvailableWork($pendingJobs, $desired, $running);
}

private function hasNoPendingWork(int $pendingJobs): bool {
return $pendingJobs === 0;
}

private function limitWorkersByAvailableWork(int $pendingJobs, int $desired, int $running): int {
$workersNeeded = $desired - $running;
$workersLimitedByJobs = min($pendingJobs, $workersNeeded);
return max(0, $workersLimitedByJobs);
}
}
42 changes: 42 additions & 0 deletions lib/Service/Worker/WorkerJobCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

namespace OCA\Libresign\Service\Worker;

use OCA\Libresign\BackgroundJob\SignFileJob;
use OCA\Libresign\BackgroundJob\SignSingleFileJob;
use OCP\BackgroundJob\IJobList;
use Psr\Log\LoggerInterface;

class WorkerJobCounter {
public function __construct(
private IJobList $jobList,
private LoggerInterface $logger,
) {
}

public function countPendingJobs(): int {
try {
$counts = $this->jobList->countByClass();
$total = 0;

foreach ($counts as $row) {
if ($row['class'] === SignFileJob::class || $row['class'] === SignSingleFileJob::class) {
$total += $row['count'];
}
}

return $total;
} catch (\Throwable $e) {
$this->logger->debug('Failed to count pending jobs', [
'error' => $e->getMessage(),
]);
return 0;
}
}
}
2 changes: 1 addition & 1 deletion lib/Service/Worker/WorkerStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function clampWorkerCount(int $count): int {
private function executeCommand(string $phpPath, string $occPath, array $jobClasses): void {
$jobClassesArg = implode(' ', array_map('escapeshellarg', $jobClasses));
$cmd = sprintf(
'%s %s background-job:worker %s --stop_after=1h >> /dev/null 2>&1 &',
'%s %s background-job:worker %s --stop_after=30m >> /dev/null 2>&1 &',
escapeshellarg($phpPath),
escapeshellarg($occPath),
$jobClassesArg
Expand Down
63 changes: 0 additions & 63 deletions lib/Service/WorkerHealthService.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/php/Unit/Service/AsyncSigningServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Enum\FileStatus;
use OCA\Libresign\Service\AsyncSigningService;
use OCA\Libresign\Service\WorkerHealthService;
use OCA\Libresign\Service\Worker\WorkerHealthService;
use OCP\BackgroundJob\IJobList;
use OCP\IUser;
use OCP\Security\ICredentialsManager;
Expand Down
Loading
Loading