custom/plugins/CrswCleverReachOfficial/src/Core/Infrastructure/TaskExecution/TaskRunnerStarter.php line 117

Open in your IDE?
  1. <?php
  2. namespace Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution;
  3. use Crsw\CleverReachOfficial\Core\Infrastructure\Logger\Logger;
  4. use Crsw\CleverReachOfficial\Core\Infrastructure\Serializer\Serializer;
  5. use Crsw\CleverReachOfficial\Core\Infrastructure\ServiceRegister;
  6. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerRunException;
  7. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerStatusStorageUnavailableException;
  8. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Interfaces\Runnable;
  9. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Interfaces\TaskRunnerStatusStorage;
  10. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Interfaces\TaskRunnerWakeup;
  11. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\TaskEvents\TickEvent;
  12. use Crsw\CleverReachOfficial\Core\Infrastructure\Utility\Events\EventBus;
  13. /**
  14.  * Class TaskRunnerStarter.
  15.  *
  16.  * @package Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution
  17.  */
  18. class TaskRunnerStarter implements Runnable
  19. {
  20.     /**
  21.      * Unique runner guid.
  22.      *
  23.      * @var string
  24.      */
  25.     private $guid;
  26.     /**
  27.      * Instance of task runner status storage.
  28.      *
  29.      * @var TaskRunnerStatusStorage
  30.      */
  31.     private $runnerStatusStorage;
  32.     /**
  33.      * Instance of task runner.
  34.      *
  35.      * @var TaskRunner
  36.      */
  37.     private $taskRunner;
  38.     /**
  39.      * Instance of task runner wakeup service.
  40.      *
  41.      * @var TaskRunnerWakeup
  42.      */
  43.     private $taskWakeup;
  44.     /**
  45.      * TaskRunnerStarter constructor.
  46.      *
  47.      * @param string $guid Unique runner guid.
  48.      */
  49.     public function __construct($guid)
  50.     {
  51.         $this->guid $guid;
  52.     }
  53.     /**
  54.      * Transforms array into an serializable object,
  55.      *
  56.      * @param array $array Data that is used to instantiate serializable object.
  57.      *
  58.      * @return \Crsw\CleverReachOfficial\Core\Infrastructure\Serializer\Interfaces\Serializable
  59.      *      Instance of serialized object.
  60.      */
  61.     public static function fromArray(array $array)
  62.     {
  63.         return new static($array['guid']);
  64.     }
  65.     /**
  66.      * Transforms serializable object into an array.
  67.      *
  68.      * @return array Array representation of a serializable object.
  69.      */
  70.     public function toArray()
  71.     {
  72.         return array('guid' => $this->guid);
  73.     }
  74.     /**
  75.      * String representation of object.
  76.      *
  77.      * @inheritdoc
  78.      */
  79.     public function serialize()
  80.     {
  81.         return Serializer::serialize(array($this->guid));
  82.     }
  83.     /**
  84.      * Constructs the object.
  85.      *
  86.      * @inheritdoc
  87.      */
  88.     public function unserialize($serialized)
  89.     {
  90.         list($this->guid) = Serializer::unserialize($serialized);
  91.     }
  92.     /**
  93.      * Get unique runner guid.
  94.      *
  95.      * @return string Unique runner string.
  96.      */
  97.     public function getGuid()
  98.     {
  99.         return $this->guid;
  100.     }
  101.     /**
  102.      * Starts synchronously currently active task runner instance.
  103.      */
  104.     public function run()
  105.     {
  106.         try {
  107.             $this->doRun();
  108.         } catch (TaskRunnerStatusStorageUnavailableException $ex) {
  109.             Logger::logError(
  110.                 'Failed to run task runner. Runner status storage unavailable.',
  111.                 'Core',
  112.                 array('ExceptionMessage' => $ex->getMessage())
  113.             );
  114.             Logger::logDebug(
  115.                 'Failed to run task runner. Runner status storage unavailable.',
  116.                 'Core',
  117.                 array(
  118.                     'ExceptionMessage' => $ex->getMessage(),
  119.                     'ExceptionTrace' => $ex->getTraceAsString(),
  120.                 )
  121.             );
  122.         } catch (TaskRunnerRunException $ex) {
  123.             Logger::logInfo($ex->getMessage());
  124.             Logger::logDebug($ex->getMessage(), 'Core', array('ExceptionTrace' => $ex->getTraceAsString()));
  125.         } catch (\Exception $ex) {
  126.             Logger::logError(
  127.                 'Failed to run task runner. Unexpected error occurred.',
  128.                 'Core',
  129.                 array('ExceptionMessage' => $ex->getMessage())
  130.             );
  131.             Logger::logDebug(
  132.                 'Failed to run task runner. Unexpected error occurred.',
  133.                 'Core',
  134.                 array(
  135.                     'ExceptionMessage' => $ex->getMessage(),
  136.                     'ExceptionTrace' => $ex->getTraceAsString(),
  137.                 )
  138.             );
  139.         }
  140.     }
  141.     /**
  142.      * Runs task execution.
  143.      *
  144.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerRunException
  145.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\TaskRunnerStatusStorageUnavailableException
  146.      */
  147.     private function doRun()
  148.     {
  149.         $runnerStatus $this->getRunnerStorage()->getStatus();
  150.         if ($this->guid !== $runnerStatus->getGuid()) {
  151.             throw new TaskRunnerRunException('Failed to run task runner. Runner guid is not set as active.');
  152.         }
  153.         if ($runnerStatus->isExpired()) {
  154.             $this->getTaskWakeup()->wakeup();
  155.             throw new TaskRunnerRunException('Failed to run task runner. Runner is expired.');
  156.         }
  157.         $this->getTaskRunner()->setGuid($this->guid);
  158.         $this->getTaskRunner()->run();
  159.         /** @var EventBus $eventBus */
  160.         $eventBus ServiceRegister::getService(EventBus::CLASS_NAME);
  161.         $eventBus->fire(new TickEvent());
  162.         // Send wakeup signal when runner is completed.
  163.         $this->getTaskWakeup()->wakeup();
  164.     }
  165.     /**
  166.      * Gets task runner status storage instance.
  167.      *
  168.      * @return TaskRunnerStatusStorage Instance of runner status storage service.
  169.      */
  170.     private function getRunnerStorage()
  171.     {
  172.         if ($this->runnerStatusStorage === null) {
  173.             $this->runnerStatusStorage ServiceRegister::getService(TaskRunnerStatusStorage::CLASS_NAME);
  174.         }
  175.         return $this->runnerStatusStorage;
  176.     }
  177.     /**
  178.      * Gets task runner instance.
  179.      *
  180.      * @return TaskRunner Instance of runner service.
  181.      */
  182.     private function getTaskRunner()
  183.     {
  184.         if ($this->taskRunner === null) {
  185.             $this->taskRunner ServiceRegister::getService(TaskRunner::CLASS_NAME);
  186.         }
  187.         return $this->taskRunner;
  188.     }
  189.     /**
  190.      * Gets task runner wakeup instance.
  191.      *
  192.      * @return TaskRunnerWakeup Instance of runner wakeup service.
  193.      */
  194.     private function getTaskWakeup()
  195.     {
  196.         if ($this->taskWakeup === null) {
  197.             $this->taskWakeup ServiceRegister::getService(TaskRunnerWakeup::CLASS_NAME);
  198.         }
  199.         return $this->taskWakeup;
  200.     }
  201. }