custom/plugins/CrswCleverReachOfficial/src/Core/BusinessLogic/Utility/Tasks/TaskCleanupTask.php line 20

Open in your IDE?
  1. <?php
  2. namespace Crsw\CleverReachOfficial\Core\BusinessLogic\Utility\Tasks;
  3. use Crsw\CleverReachOfficial\Core\BusinessLogic\Scheduler\ScheduledTask;
  4. use Crsw\CleverReachOfficial\Core\Infrastructure\ORM\Interfaces\RepositoryInterface;
  5. use Crsw\CleverReachOfficial\Core\Infrastructure\ORM\QueryFilter\Operators;
  6. use Crsw\CleverReachOfficial\Core\Infrastructure\ORM\QueryFilter\QueryFilter;
  7. use Crsw\CleverReachOfficial\Core\Infrastructure\ORM\RepositoryRegistry;
  8. use Crsw\CleverReachOfficial\Core\Infrastructure\Serializer\Serializer;
  9. use Crsw\CleverReachOfficial\Core\Infrastructure\ServiceRegister;
  10. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem;
  11. use Crsw\CleverReachOfficial\Core\Infrastructure\Utility\TimeProvider;
  12. /**
  13.  * Class TaskCleanupTask
  14.  *
  15.  * @package Crsw\CleverReachOfficial\Core\BusinessLogic\Utility\Tasks
  16.  */
  17. class TaskCleanupTask extends ScheduledTask
  18. {
  19.     /**
  20.      * Limit in number of tasks that will be deleted. Since this task will be used in scheduler and run
  21.      * every 5min it will be limited to fixed amount of tasks per run to avoid memory leaks.
  22.      * Once we have batch delete this should be removed and redesigned!
  23.      */
  24.     const CLEANUP_LIMIT 1000;
  25.     /**
  26.      * The class name of the task.
  27.      *
  28.      * @var string
  29.      */
  30.     private $taskType;
  31.     /**
  32.      * A list of task statuses.
  33.      *
  34.      * @var array
  35.      */
  36.     private $taskStatuses;
  37.     /**
  38.      * An age of the task in seconds.
  39.      *
  40.      * @var int
  41.      */
  42.     private $taskAge;
  43.     /**
  44.      * Current progress.
  45.      *
  46.      * @var float
  47.      */
  48.     private $progress;
  49.     /**
  50.      * TaskCleanupTask constructor.
  51.      *
  52.      * @param string $taskType The type of the task to delete.
  53.      * @param array $taskStatuses The list of queue item statuses.
  54.      * @param int $taskAge The min age of the task.
  55.      */
  56.     public function __construct($taskType, array $taskStatuses$taskAge 60)
  57.     {
  58.         $this->taskType $taskType;
  59.         $this->taskStatuses $taskStatuses;
  60.         $this->taskAge $taskAge;
  61.         $this->progress 0.0;
  62.     }
  63.     /**
  64.      * Defines whether schedulable task can be enqueued for execution if there is already instance with queued status.
  65.      *
  66.      * @return bool False indeicates that the schedulable task should not enqueued if there
  67.      *      is already instance in queued status.
  68.      */
  69.     public function canHaveMultipleQueuedInstances()
  70.     {
  71.         return true;
  72.     }
  73.     /**
  74.      * @inheritDoc
  75.      */
  76.     public static function fromArray(array $array)
  77.     {
  78.         return new static(
  79.             $array['task_type'],
  80.             !empty($array['task_statuses']) ? $array['task_statuses'] : array(),
  81.             $array['task_age']
  82.         );
  83.     }
  84.     /**
  85.      * @inheritdoc
  86.      */
  87.     public function serialize()
  88.     {
  89.         return Serializer::serialize($this->toArray());
  90.     }
  91.     /**
  92.      * @inheritDoc
  93.      */
  94.     public function toArray()
  95.     {
  96.         return array(
  97.             'task_type' => $this->taskType,
  98.             'task_statuses' => $this->taskStatuses,
  99.             'task_age' => $this->taskAge,
  100.         );
  101.     }
  102.     /**
  103.      * @inheritdoc
  104.      */
  105.     public function unserialize($serialized)
  106.     {
  107.         list($this->taskType$this->taskStatuses$this->taskAge) = array_values(Serializer::unserialize($serialized));
  108.     }
  109.     /**
  110.      * Runs task logic.
  111.      *
  112.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException
  113.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\ORM\Exceptions\RepositoryClassException
  114.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException
  115.      */
  116.     public function execute()
  117.     {
  118.         // cleanup requested tasks
  119.         $this->cleanupTasks($this->taskType$this->taskStatuses$this->taskAge90);
  120.         // self cleanup
  121.         $this->cleanupTasks(static::getClassName(), array(QueueItem::COMPLETED), 360010);
  122.         $this->reportProgress(100);
  123.     }
  124.     /**
  125.      * Cleans up the tasks with the specified parameters.
  126.      *
  127.      * @param string $taskType The type of the task to delete.
  128.      * @param array $taskStatuses The list of queue item statuses.
  129.      * @param string $taskAge The min age of the task.
  130.      * @param int $progressPart Progress report part of the overall task.
  131.      *
  132.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException
  133.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\ORM\Exceptions\RepositoryClassException
  134.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException
  135.      */
  136.     private function cleanupTasks($taskType, array $taskStatuses$taskAge$progressPart)
  137.     {
  138.         /** @var TimeProvider $timeProvider */
  139.         $timeProvider ServiceRegister::getService(TimeProvider::CLASS_NAME);
  140.         $time $timeProvider->getCurrentLocalTime()->getTimestamp();
  141.         $filter = new QueryFilter();
  142.         /** @noinspection PhpUnhandledExceptionInspection */
  143.         $filter->where('taskType'Operators::EQUALS$taskType)
  144.             ->where('status'Operators::IN$taskStatuses)
  145.             ->where('lastUpdateTimestamp'Operators::LESS_OR_EQUAL_THAN$time $taskAge)
  146.             ->setLimit(static::CLEANUP_LIMIT);
  147.         $queueItemRepository RepositoryRegistry::getQueueItemRepository();
  148.         $archivedRepository RepositoryRegistry::getArchivedQueueItemRepository();
  149.         $dividedProgressPart $progressPart 2;
  150.         $this->cleanTasksFromRepository($queueItemRepository$filter$dividedProgressPart);
  151.         $this->cleanTasksFromRepository($archivedRepository$filter$dividedProgressPart);
  152.     }
  153.     /**
  154.      * @param RepositoryInterface $repository
  155.      * @param QueryFilter $filter
  156.      * @param int $progressPart
  157.      *
  158.      * @return void
  159.      */
  160.     private function cleanTasksFromRepository($repository$filter$progressPart)
  161.     {
  162.         $queueItems $repository->select($filter);
  163.         $totalItems count($queueItems);
  164.         if ($totalItems 0) {
  165.             $progressStep $progressPart $totalItems;
  166.             foreach ($queueItems as $item) {
  167.                 $repository->delete($item);
  168.                 $this->progress += $progressStep;
  169.                 $this->reportProgress($this->progress);
  170.             }
  171.         }
  172.     }
  173. }