|
7 | 7 |
|
8 | 8 | namespace yii\queue\cli; |
9 | 9 |
|
| 10 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
10 | 11 | use Symfony\Component\Process\Exception\RuntimeException as ProcessRuntimeException; |
11 | 12 | use Symfony\Component\Process\Process; |
12 | 13 | use yii\console\Controller; |
13 | | -use yii\console\ExitCode; |
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * Base Command. |
|
19 | 19 | */ |
20 | 20 | abstract class Command extends Controller |
21 | 21 | { |
| 22 | + /** |
| 23 | + * The exit code of the exec action which is returned when job was done. |
| 24 | + */ |
| 25 | + const EXEC_DONE = 0; |
| 26 | + /** |
| 27 | + * The exit code of the exec action which is returned when job wasn't done and wanted next attempt. |
| 28 | + */ |
| 29 | + const EXEC_RETRY = 3; |
| 30 | + |
22 | 31 | /** |
23 | 32 | * @var Queue |
24 | 33 | */ |
@@ -46,7 +55,6 @@ abstract class Command extends Controller |
46 | 55 | */ |
47 | 56 | public $phpBinary; |
48 | 57 |
|
49 | | - |
50 | 58 | /** |
51 | 59 | * @inheritdoc |
52 | 60 | */ |
@@ -134,10 +142,9 @@ public function beforeAction($action) |
134 | 142 | public function actionExec($id, $ttr, $attempt, $pid) |
135 | 143 | { |
136 | 144 | if ($this->queue->execute($id, file_get_contents('php://stdin'), $ttr, $attempt, $pid)) { |
137 | | - return ExitCode::OK; |
| 145 | + return self::EXEC_DONE; |
138 | 146 | } |
139 | | - |
140 | | - return ExitCode::UNSPECIFIED_ERROR; |
| 147 | + return self::EXEC_RETRY; |
141 | 148 | } |
142 | 149 |
|
143 | 150 | /** |
@@ -174,18 +181,20 @@ protected function handleMessage($id, $message, $ttr, $attempt) |
174 | 181 |
|
175 | 182 | $process = new Process($cmd, null, null, $message, $ttr); |
176 | 183 | try { |
177 | | - $process->run(function ($type, $buffer) { |
| 184 | + $result = $process->run(function ($type, $buffer) { |
178 | 185 | if ($type === Process::ERR) { |
179 | 186 | $this->stderr($buffer); |
180 | 187 | } else { |
181 | 188 | $this->stdout($buffer); |
182 | 189 | } |
183 | 190 | }); |
| 191 | + if (!in_array($result, [self::EXEC_DONE, self::EXEC_RETRY])) { |
| 192 | + throw new ProcessFailedException($process); |
| 193 | + } |
| 194 | + return $result === self::EXEC_DONE; |
184 | 195 | } catch (ProcessRuntimeException $error) { |
185 | 196 | $job = $this->queue->serializer->unserialize($message); |
186 | 197 | return $this->queue->handleError($id, $job, $ttr, $attempt, $error); |
187 | 198 | } |
188 | | - |
189 | | - return $process->isSuccessful(); |
190 | 199 | } |
191 | 200 | } |
0 commit comments