custom/plugins/SendcloudShipping/src/Core/Infrastructure/TaskExecution/TaskRunnerStarter.php line 149

Open in your IDE?
  1. <?php
  2. namespace Sendcloud\Shipping\Core\Infrastructure\TaskExecution;
  3. use Sendcloud\Shipping\Core\Infrastructure\Interfaces\Exposed\Runnable;
  4. use Sendcloud\Shipping\Core\Infrastructure\Interfaces\Exposed\TaskRunnerStatusStorage as TaskRunnerStatusStorageInterface;
  5. use Sendcloud\Shipping\Core\Infrastructure\Interfaces\Exposed\TaskRunnerWakeup as TaskRunnerWakeupInterface;
  6. use Sendcloud\Shipping\Core\Infrastructure\Logger\Logger;
  7. use Sendcloud\Shipping\Core\Infrastructure\ServiceRegister;
  8. use Sendcloud\Shipping\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerRunException;
  9. use Sendcloud\Shipping\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerStatusStorageUnavailableException;
  10. use Sendcloud\Shipping\Core\Infrastructure\TaskExecution\TaskEvents\TickEvent;
  11. use Sendcloud\Shipping\Core\Infrastructure\Utility\Events\EventBus;
  12. /**
  13.  * Class TaskRunnerStarter
  14.  * @package Sendcloud\Shipping\Core\Infrastructure\TaskExecution
  15.  */
  16. class TaskRunnerStarter implements Runnable
  17. {
  18.     /**
  19.      * @var string
  20.      */
  21.     private $guid;
  22.     /**
  23.      * @var TaskRunnerStatusStorageInterface
  24.      */
  25.     private $runnerStatusStorage;
  26.     /**
  27.      * @var TaskRunner
  28.      */
  29.     private $taskRunner;
  30.     /**
  31.      * @var TaskRunnerWakeupInterface
  32.      */
  33.     private $taskWakeup;
  34.     /**
  35.      * TaskRunnerStarter constructor.
  36.      *
  37.      * @param string $guid
  38.      */
  39.     public function __construct($guid)
  40.     {
  41.         $this->guid $guid;
  42.     }
  43.     /**
  44.      * String representation of object
  45.      * @link http://php.net/manual/en/serializable.serialize.php
  46.      * @return string the string representation of the object or null
  47.      * @since 5.1.0
  48.      */
  49.     public function serialize()
  50.     {
  51.         return serialize(array($this->guid));
  52.     }
  53.     /**
  54.      * Constructs the object
  55.      * @link http://php.net/manual/en/serializable.unserialize.php
  56.      *
  57.      * @param string $serialized <p>
  58.      * The string representation of the object.
  59.      * </p>
  60.      *
  61.      * @since 5.1.0
  62.      */
  63.     public function unserialize($serialized)
  64.     {
  65.         list($this->guid) = unserialize($serialized);
  66.     }
  67.     /**
  68.      * Retrieves task guid
  69.      *
  70.      * @return string
  71.      */
  72.     public function getGuid()
  73.     {
  74.         return $this->guid;
  75.     }
  76.     /**
  77.      * Starts synchronously currently active task runner instance
  78.      *
  79.      * @throws TaskRunnerRunException
  80.      */
  81.     public function run()
  82.     {
  83.         try {
  84.             $this->doRun();
  85.         } catch (TaskRunnerStatusStorageUnavailableException $ex) {
  86.             Logger::logError(
  87.                 'Failed to run task runner. Runner status storage unavailable.',
  88.                 'Core',
  89.                 array('ExceptionMessage' => $ex->getMessage())
  90.             );
  91.             Logger::logDebug(
  92.                 'Failed to run task runner. Runner status storage unavailable.',
  93.                 'Core',
  94.                 array(
  95.                     'ExceptionMessage' => $ex->getMessage(),
  96.                     'ExceptionTrace' => $ex->getTraceAsString()
  97.                 )
  98.             );
  99.         } catch (TaskRunnerRunException $ex) {
  100.             Logger::logInfo($ex->getMessage());
  101.             Logger::logDebug($ex->getMessage(), 'Core',  array('ExceptionTrace' => $ex->getTraceAsString()));
  102.         } catch (\Exception $ex) {
  103.             Logger::logError(
  104.                 'Failed to run task runner. Unexpected error occurred.',
  105.                 'Core',
  106.                 array('ExceptionMessage' => $ex->getMessage())
  107.             );
  108.             Logger::logDebug(
  109.                 'Failed to run task runner. Unexpected error occurred.',
  110.                 'Core',
  111.                 array(
  112.                     'ExceptionMessage' => $ex->getMessage(),
  113.                     'ExceptionTrace' => $ex->getTraceAsString()
  114.                 )
  115.             );
  116.         }
  117.     }
  118.     /**
  119.      * Runs task execution
  120.      *
  121.      * @throws \Sendcloud\Shipping\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerRunException
  122.      * @throws \Sendcloud\Shipping\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerStatusStorageUnavailableException
  123.      */
  124.     private function doRun()
  125.     {
  126.         $runnerStatus $this->getRunnerStorage()->getStatus();
  127.         if ($this->guid !== $runnerStatus->getGuid()) {
  128.             throw new TaskRunnerRunException('Failed to run task runner. Runner guid is not set as active.');
  129.         }
  130.         if ($runnerStatus->isExpired()) {
  131.             $this->getTaskWakeup()->wakeup();
  132.             throw new TaskRunnerRunException('Failed to run task runner. Runner is expired.');
  133.         }
  134.         $this->getTaskRunner()->setGuid($this->guid);
  135.         $this->getTaskRunner()->run();
  136.         /** @var EventBus $eventBus */
  137.         $eventBus ServiceRegister::getService(EventBus::CLASS_NAME);
  138.         $eventBus->fire(new TickEvent());
  139.     }
  140.     /**
  141.      * @return TaskRunnerStatusStorageInterface
  142.      */
  143.     private function getRunnerStorage()
  144.     {
  145.         if (empty($this->runnerStatusStorage)) {
  146.             $this->runnerStatusStorage ServiceRegister::getService(TaskRunnerStatusStorageInterface::CLASS_NAME);
  147.         }
  148.         return $this->runnerStatusStorage;
  149.     }
  150.     /**
  151.      * @return TaskRunner
  152.      */
  153.     private function getTaskRunner()
  154.     {
  155.         if (empty($this->taskRunner)) {
  156.             $this->taskRunner ServiceRegister::getService(TaskRunner::CLASS_NAME);
  157.         }
  158.         return $this->taskRunner;
  159.     }
  160.     /**
  161.      * @return TaskRunnerWakeupInterface
  162.      */
  163.     private function getTaskWakeup()
  164.     {
  165.         if (empty($this->taskWakeup)) {
  166.             $this->taskWakeup ServiceRegister::getService(TaskRunnerWakeupInterface::CLASS_NAME);
  167.         }
  168.         return $this->taskWakeup;
  169.     }
  170. }