1 | <?php |
||
24 | class SeparateProcessExecutor implements ExecutorInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var TaskHandlerFactoryInterface |
||
28 | */ |
||
29 | private $handlerFactory; |
||
30 | |||
31 | /** |
||
32 | * @var TaskExecutionRepositoryInterface |
||
33 | */ |
||
34 | private $executionRepository; |
||
35 | |||
36 | /** |
||
37 | * @var ExecutionProcessFactory |
||
38 | */ |
||
39 | private $processFactory; |
||
40 | |||
41 | /** |
||
42 | * @param TaskHandlerFactoryInterface $handlerFactory |
||
43 | * @param TaskExecutionRepositoryInterface $executionRepository |
||
44 | * @param ExecutionProcessFactory $processFactory |
||
45 | */ |
||
46 | 18 | public function __construct( |
|
47 | TaskHandlerFactoryInterface $handlerFactory, |
||
48 | TaskExecutionRepositoryInterface $executionRepository, |
||
49 | ExecutionProcessFactory $processFactory |
||
50 | ) { |
||
51 | 18 | $this->handlerFactory = $handlerFactory; |
|
52 | 18 | $this->executionRepository = $executionRepository; |
|
53 | 18 | $this->processFactory = $processFactory; |
|
54 | 18 | } |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 7 | public function execute(TaskExecutionInterface $execution) |
|
60 | { |
||
61 | 7 | $attempts = $this->getMaximumAttempts($execution->getHandlerClass()); |
|
62 | 7 | $lastException = null; |
|
63 | |||
64 | 7 | for ($attempt = 0; $attempt < $attempts; ++$attempt) { |
|
65 | try { |
||
66 | 7 | return $this->handle($execution); |
|
67 | 5 | } catch (FailedException $exception) { |
|
68 | 2 | throw $exception; |
|
69 | 3 | } catch (SeparateProcessException $exception) { |
|
70 | 3 | if ($execution->getAttempts() < $attempts) { |
|
71 | 1 | $execution->incrementAttempts(); |
|
72 | 1 | $this->executionRepository->save($execution); |
|
73 | 1 | } |
|
74 | |||
75 | 3 | $lastException = $exception; |
|
76 | } |
||
77 | 3 | } |
|
78 | |||
79 | // maximum attempts to pass executions are reached |
||
80 | 3 | throw new FailedException($lastException); |
|
|
|||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns maximum attempts for specified handler. |
||
85 | * |
||
86 | * @param string $handlerClass |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | 7 | private function getMaximumAttempts($handlerClass) |
|
99 | |||
100 | /** |
||
101 | * Handle execution by using console-command. |
||
102 | * |
||
103 | * @param TaskExecutionInterface $execution |
||
104 | * |
||
105 | * @return string |
||
106 | * |
||
107 | * @throws FailedException |
||
108 | * @throws SeparateProcessException |
||
109 | */ |
||
110 | 7 | private function handle(TaskExecutionInterface $execution) |
|
121 | |||
122 | /** |
||
123 | * Create the correct exception. |
||
124 | * |
||
125 | * FailedException for failed executions. |
||
126 | * SeparateProcessExceptions for any exception during execution. |
||
127 | * |
||
128 | * @param string $errorOutput |
||
129 | * |
||
130 | * @return FailedException|SeparateProcessException |
||
131 | */ |
||
132 | 5 | private function createException($errorOutput) |
|
142 | } |
||
143 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: