1
0

TaskBackgroundJob.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\TextProcessing;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\QueuedJob;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\TextProcessing\Events\TaskFailedEvent;
  12. use OCP\TextProcessing\Events\TaskSuccessfulEvent;
  13. use OCP\TextProcessing\IManager;
  14. class TaskBackgroundJob extends QueuedJob {
  15. public function __construct(
  16. ITimeFactory $timeFactory,
  17. private IManager $textProcessingManager,
  18. private IEventDispatcher $eventDispatcher,
  19. ) {
  20. parent::__construct($timeFactory);
  21. // We want to avoid overloading the machine with these jobs
  22. // so we only allow running one job at a time
  23. $this->setAllowParallelRuns(false);
  24. }
  25. /**
  26. * @param array{taskId: int} $argument
  27. * @inheritDoc
  28. */
  29. protected function run($argument) {
  30. $taskId = $argument['taskId'];
  31. $task = $this->textProcessingManager->getTask($taskId);
  32. try {
  33. $this->textProcessingManager->runTask($task);
  34. $event = new TaskSuccessfulEvent($task);
  35. } catch (\Throwable $e) {
  36. $event = new TaskFailedEvent($task, $e->getMessage());
  37. }
  38. $this->eventDispatcher->dispatchTyped($event);
  39. }
  40. }