1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-task library. |
5
|
|
|
* |
6
|
|
|
* (c) php-task |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\Executor; |
13
|
|
|
|
14
|
|
|
use Task\Execution\TaskExecutionInterface; |
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; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Uses a separate process to start the executions via console-command. |
23
|
|
|
*/ |
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) |
91
|
|
|
{ |
92
|
7 |
|
$handler = $this->handlerFactory->create($handlerClass); |
93
|
7 |
|
if (!$handler instanceof RetryTaskHandlerInterface) { |
94
|
5 |
|
return 1; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
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
|
7 |
|
private function handle(TaskExecutionInterface $execution) |
111
|
|
|
{ |
112
|
7 |
|
$process = $this->processFactory->create($execution->getUuid()); |
113
|
7 |
|
$process->run(); |
114
|
|
|
|
115
|
7 |
|
if (!$process->isSuccessful()) { |
116
|
5 |
|
throw $this->createException($process->getErrorOutput()); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
return $process->getOutput(); |
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
|
5 |
|
private function createException($errorOutput) |
133
|
|
|
{ |
134
|
5 |
|
if (strpos($errorOutput, FailedException::class) !== 0) { |
135
|
3 |
|
return new SeparateProcessException($errorOutput); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
$errorOutput = trim(str_replace(FailedException::class, '', $errorOutput)); |
139
|
|
|
|
140
|
2 |
|
return new FailedException(new SeparateProcessException($errorOutput)); |
141
|
|
|
} |
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: