|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2021-2022 The Recognize contributors. |
| 5 | + * This file is licensed under the Affero General Public License version 3 or later. See the COPYING file. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | +namespace OCA\Recognize\BackgroundJobs; |
| 9 | + |
| 10 | +use OCA\Recognize\Db\FsAccessUpdate; |
| 11 | +use OCA\Recognize\Db\FsActionMapper; |
| 12 | +use OCA\Recognize\Db\FsCreation; |
| 13 | +use OCA\Recognize\Db\FsDeletion; |
| 14 | +use OCA\Recognize\Db\FsMove; |
| 15 | +use OCA\Recognize\Service\FsActionService; |
| 16 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 17 | +use OCP\BackgroundJob\IJobList; |
| 18 | +use OCP\BackgroundJob\TimedJob; |
| 19 | +use OCP\DB\Exception; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | + |
| 22 | +final class ProcessFsActionsJob extends TimedJob { |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + ITimeFactory $timeFactory, |
| 26 | + private FsActionService $accessUpdateService, |
| 27 | + private IJobList $jobList, |
| 28 | + private FsActionMapper $accessUpdateMapper, |
| 29 | + private LoggerInterface $logger, |
| 30 | + ) { |
| 31 | + parent::__construct($timeFactory); |
| 32 | + $this->setInterval(5 * 60); |
| 33 | + $this->setTimeSensitivity(self::TIME_SENSITIVE); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param array{storage_id:int, type: class-string<FsAccessUpdate|FsCreation|FsDeletion|FsMove>} $argument |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + protected function run($argument): void { |
| 41 | + $storageId = $argument['storage_id'] ?? null; |
| 42 | + $className = $argument['type']; |
| 43 | + |
| 44 | + if (isset($storageId)) { |
| 45 | + $this->accessUpdateService->processActionsByClassAndStorageId($className, $storageId); |
| 46 | + try { |
| 47 | + $remainingCount = $this->accessUpdateMapper->countByStorageId($className, $storageId); |
| 48 | + } catch (Exception $e) { |
| 49 | + $this->logger->error('Failed to count fs actions: ' . $e->getMessage(), ['exception' => $e]); |
| 50 | + $remainingCount = 1; |
| 51 | + } |
| 52 | + } else { |
| 53 | + $this->accessUpdateService->processActionsByClass($className); |
| 54 | + try { |
| 55 | + $remainingCount = $this->accessUpdateMapper->count($className); |
| 56 | + } catch (Exception $e) { |
| 57 | + $this->logger->error('Failed to count fs actions: ' . $e->getMessage(), ['exception' => $e]); |
| 58 | + $remainingCount = 1; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + if ($remainingCount === 0) { |
| 64 | + // Remove job from queue |
| 65 | + $this->jobList->remove(self::class, $argument); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments