vendor/shopware/core/Framework/MessageQueue/ScheduledTask/ScheduledTaskHandler.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\MessageQueue\ScheduledTask;
  3. use Shopware\Core\Defaults;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\MessageQueue\Handler\AbstractMessageHandler;
  9. /**
  10.  * @deprecated tag:v6.5.0 - reason:class-hierarchy-change - Will only implement MessageHandlerInterface
  11.  */
  12. #[Package('core')]
  13. abstract class ScheduledTaskHandler extends AbstractMessageHandler
  14. {
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     protected $scheduledTaskRepository;
  19.     public function __construct(EntityRepositoryInterface $scheduledTaskRepository)
  20.     {
  21.         $this->scheduledTaskRepository $scheduledTaskRepository;
  22.     }
  23.     abstract public function run(): void;
  24.     /**
  25.      * @param ScheduledTask $task
  26.      */
  27.     public function handle($task): void
  28.     {
  29.         $taskId $task->getTaskId();
  30.         if ($taskId === null) {
  31.             // run task independent of the schedule
  32.             $this->run();
  33.             return;
  34.         }
  35.         /** @var ScheduledTaskEntity|null $taskEntity */
  36.         $taskEntity $this->scheduledTaskRepository
  37.             ->search(new Criteria([$taskId]), Context::createDefaultContext())
  38.             ->get($taskId);
  39.         if ($taskEntity === null || !$taskEntity->isExecutionAllowed()) {
  40.             return;
  41.         }
  42.         $this->markTaskRunning($task);
  43.         try {
  44.             $this->run();
  45.         } catch (\Throwable $e) {
  46.             $this->markTaskFailed($task);
  47.             throw $e;
  48.         }
  49.         $this->rescheduleTask($task$taskEntity);
  50.     }
  51.     protected function markTaskRunning(ScheduledTask $task): void
  52.     {
  53.         $this->scheduledTaskRepository->update([
  54.             [
  55.                 'id' => $task->getTaskId(),
  56.                 'status' => ScheduledTaskDefinition::STATUS_RUNNING,
  57.             ],
  58.         ], Context::createDefaultContext());
  59.     }
  60.     protected function markTaskFailed(ScheduledTask $task): void
  61.     {
  62.         $this->scheduledTaskRepository->update([
  63.             [
  64.                 'id' => $task->getTaskId(),
  65.                 'status' => ScheduledTaskDefinition::STATUS_FAILED,
  66.             ],
  67.         ], Context::createDefaultContext());
  68.     }
  69.     protected function rescheduleTask(ScheduledTask $taskScheduledTaskEntity $taskEntity): void
  70.     {
  71.         $now = new \DateTimeImmutable();
  72.         $nextExecutionTimeString $taskEntity->getNextExecutionTime()->format(Defaults::STORAGE_DATE_TIME_FORMAT);
  73.         $nextExecutionTime = new \DateTimeImmutable($nextExecutionTimeString);
  74.         $newNextExecutionTime $nextExecutionTime->modify(sprintf('+%d seconds'$taskEntity->getRunInterval()));
  75.         if ($newNextExecutionTime $now) {
  76.             $newNextExecutionTime $now;
  77.         }
  78.         $this->scheduledTaskRepository->update([
  79.             [
  80.                 'id' => $task->getTaskId(),
  81.                 'status' => ScheduledTaskDefinition::STATUS_SCHEDULED,
  82.                 'lastExecutionTime' => $now,
  83.                 'nextExecutionTime' => $newNextExecutionTime,
  84.             ],
  85.         ], Context::createDefaultContext());
  86.     }
  87. }