0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-03-13 16:03:55 +00:00
nextcloud_server/lib/private/TaskProcessing/SynchronousBackgroundJob.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\TaskProcessing;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\Files\GenericFileException;
use OCP\Files\NotPermittedException;
use OCP\Lock\LockedException;
use OCP\TaskProcessing\Exception\Exception;
use OCP\TaskProcessing\Exception\NotFoundException;
use OCP\TaskProcessing\Exception\ProcessingException;
use OCP\TaskProcessing\Exception\UnauthorizedException;
use OCP\TaskProcessing\Exception\ValidationException;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\Task;
use Psr\Log\LoggerInterface;
class SynchronousBackgroundJob extends QueuedJob {
public function __construct(
ITimeFactory $timeFactory,
private readonly IManager $taskProcessingManager,
private readonly IJobList $jobList,
private readonly LoggerInterface $logger,
) {
parent::__construct($timeFactory);
}
/**
* @inheritDoc
*/
protected function run($argument) {
$providers = $this->taskProcessingManager->getProviders();
foreach ($providers as $provider) {
if (!$provider instanceof ISynchronousProvider) {
continue;
}
$taskTypeId = $provider->getTaskTypeId();
// only use this provider if it is the preferred one
$preferredProvider = $this->taskProcessingManager->getPreferredProvider($taskTypeId);
if ($provider->getId() !== $preferredProvider->getId()) {
continue;
}
try {
$task = $this->taskProcessingManager->getNextScheduledTask([$taskTypeId]);
} catch (NotFoundException $e) {
continue;
} catch (Exception $e) {
$this->logger->error('Unknown error while retrieving scheduled TaskProcessing tasks', ['exception' => $e]);
continue;
}
if (!$this->taskProcessingManager->processTask($task, $provider)) {
// Schedule again
$this->jobList->add(self::class, $argument);
}
}
$synchronousProviders = array_filter($providers, fn ($provider) =>
$provider instanceof ISynchronousProvider);
$taskTypes = array_values(array_map(fn ($provider) =>
$provider->getTaskTypeId(),
$synchronousProviders
));
$taskTypesWithTasks = array_filter($taskTypes, function ($taskType) {
try {
$this->taskProcessingManager->getNextScheduledTask([$taskType]);
return true;
} catch (NotFoundException|Exception $e) {
return false;
}
});
if (count($taskTypesWithTasks) > 0) {
// Schedule again
$this->jobList->add(self::class, $argument);
}
}
}