|
11 | 11 |
|
12 | 12 | namespace Task\TaskBundle\Executor; |
13 | 13 |
|
14 | | -use Symfony\Component\Process\ProcessBuilder; |
15 | 14 | use Task\Execution\TaskExecutionInterface; |
16 | 15 | use Task\Executor\ExecutorInterface; |
| 16 | +use Task\Executor\FailedException; |
| 17 | +use Task\Executor\RetryTaskHandlerInterface; |
| 18 | +use Task\Handler\TaskHandlerFactoryInterface; |
| 19 | +use Task\Storage\TaskExecutionRepositoryInterface; |
17 | 20 |
|
18 | 21 | /** |
19 | 22 | * Uses a separate process to start the executions via console-command. |
20 | 23 | */ |
21 | 24 | class SeparateProcessExecutor implements ExecutorInterface |
22 | 25 | { |
23 | 26 | /** |
24 | | - * @var string |
| 27 | + * @var TaskHandlerFactoryInterface |
25 | 28 | */ |
26 | | - private $consolePath; |
| 29 | + private $handlerFactory; |
27 | 30 |
|
28 | 31 | /** |
29 | | - * @var string |
| 32 | + * @var TaskExecutionRepositoryInterface |
30 | 33 | */ |
31 | | - private $environment; |
| 34 | + private $executionRepository; |
32 | 35 |
|
33 | 36 | /** |
34 | | - * @param string $consolePath |
35 | | - * @param string $environment |
| 37 | + * @var ExecutionProcessFactory |
36 | 38 | */ |
37 | | - public function __construct($consolePath, $environment) |
38 | | - { |
39 | | - $this->consolePath = $consolePath; |
40 | | - $this->environment = $environment; |
| 39 | + private $processFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param TaskHandlerFactoryInterface $handlerFactory |
| 43 | + * @param TaskExecutionRepositoryInterface $executionRepository |
| 44 | + * @param ExecutionProcessFactory $processFactory |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + TaskHandlerFactoryInterface $handlerFactory, |
| 48 | + TaskExecutionRepositoryInterface $executionRepository, |
| 49 | + ExecutionProcessFactory $processFactory |
| 50 | + ) { |
| 51 | + $this->handlerFactory = $handlerFactory; |
| 52 | + $this->executionRepository = $executionRepository; |
| 53 | + $this->processFactory = $processFactory; |
41 | 54 | } |
42 | 55 |
|
43 | 56 | /** |
44 | 57 | * {@inheritdoc} |
45 | 58 | */ |
46 | 59 | public function execute(TaskExecutionInterface $execution) |
47 | 60 | { |
48 | | - $process = ProcessBuilder::create( |
49 | | - [$this->consolePath, 'task:execute', $execution->getUuid(), '-e ' . $this->environment] |
50 | | - )->getProcess(); |
| 61 | + $attempts = $this->getMaximumAttempts($execution->getHandlerClass()); |
| 62 | + $lastException = null; |
| 63 | + |
| 64 | + for ($attempt = 0; $attempt < $attempts; ++$attempt) { |
| 65 | + try { |
| 66 | + return $this->handle($execution); |
| 67 | + } catch (FailedException $exception) { |
| 68 | + throw $exception; |
| 69 | + } catch (SeparateProcessException $exception) { |
| 70 | + if ($execution->getAttempts() < $attempts) { |
| 71 | + $execution->incrementAttempts(); |
| 72 | + $this->executionRepository->save($execution); |
| 73 | + } |
| 74 | + |
| 75 | + $lastException = $exception; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // maximum attempts to pass executions are reached |
| 80 | + 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 | + private function getMaximumAttempts($handlerClass) |
| 91 | + { |
| 92 | + $handler = $this->handlerFactory->create($handlerClass); |
| 93 | + if (!$handler instanceof RetryTaskHandlerInterface) { |
| 94 | + return 1; |
| 95 | + } |
51 | 96 |
|
| 97 | + return $handler->getMaximumAttempts(); |
| 98 | + } |
| 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 | + private function handle(TaskExecutionInterface $execution) |
| 111 | + { |
| 112 | + $process = $this->processFactory->create($execution->getUuid()); |
52 | 113 | $process->run(); |
53 | 114 |
|
54 | 115 | if (!$process->isSuccessful()) { |
55 | | - throw new SeparateProcessException($process->getErrorOutput()); |
| 116 | + throw $this->createException($process->getErrorOutput()); |
56 | 117 | } |
57 | 118 |
|
58 | 119 | return $process->getOutput(); |
59 | 120 | } |
| 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 | + private function createException($errorOutput) |
| 133 | + { |
| 134 | + if (strpos($errorOutput, FailedException::class) !== 0) { |
| 135 | + return new SeparateProcessException($errorOutput); |
| 136 | + } |
| 137 | + |
| 138 | + $errorOutput = trim(str_replace(FailedException::class, '', $errorOutput)); |
| 139 | + |
| 140 | + return new FailedException(new SeparateProcessException($errorOutput)); |
| 141 | + } |
60 | 142 | } |
0 commit comments