Skip to content

Commit 4bc0021

Browse files
committed
yiisoft#126: Handles a fatal error of the job execution in isolate mode
1 parent 4fdaf90 commit 4bc0021

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/cli/Command.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace yii\queue\cli;
99

10+
use Symfony\Component\Process\Exception\ProcessFailedException;
1011
use Symfony\Component\Process\Exception\RuntimeException as ProcessRuntimeException;
1112
use Symfony\Component\Process\Process;
1213
use yii\console\Controller;
13-
use yii\console\ExitCode;
1414

1515
/**
1616
* Base Command.
@@ -19,6 +19,15 @@
1919
*/
2020
abstract class Command extends Controller
2121
{
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+
2231
/**
2332
* @var Queue
2433
*/
@@ -46,7 +55,6 @@ abstract class Command extends Controller
4655
*/
4756
public $phpBinary;
4857

49-
5058
/**
5159
* @inheritdoc
5260
*/
@@ -134,10 +142,9 @@ public function beforeAction($action)
134142
public function actionExec($id, $ttr, $attempt, $pid)
135143
{
136144
if ($this->queue->execute($id, file_get_contents('php://stdin'), $ttr, $attempt, $pid)) {
137-
return ExitCode::OK;
145+
return self::EXEC_DONE;
138146
}
139-
140-
return ExitCode::UNSPECIFIED_ERROR;
147+
return self::EXEC_RETRY;
141148
}
142149

143150
/**
@@ -174,18 +181,20 @@ protected function handleMessage($id, $message, $ttr, $attempt)
174181

175182
$process = new Process($cmd, null, null, $message, $ttr);
176183
try {
177-
$process->run(function ($type, $buffer) {
184+
$result = $process->run(function ($type, $buffer) {
178185
if ($type === Process::ERR) {
179186
$this->stderr($buffer);
180187
} else {
181188
$this->stdout($buffer);
182189
}
183190
});
191+
if (!in_array($result, [self::EXEC_DONE, self::EXEC_RETRY])) {
192+
throw new ProcessFailedException($process);
193+
}
194+
return $result === self::EXEC_DONE;
184195
} catch (ProcessRuntimeException $error) {
185196
$job = $this->queue->serializer->unserialize($message);
186197
return $this->queue->handleError($id, $job, $ttr, $attempt, $error);
187198
}
188-
189-
return $process->isSuccessful();
190199
}
191200
}

src/cli/VerboseBehavior.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ public function afterError(ErrorEvent $event)
8989
$this->command->stdout($this->jobTitle($event), Console::FG_GREY);
9090
$this->command->stdout(' - ', Console::FG_YELLOW);
9191
$this->command->stdout('Error', Console::BG_RED);
92-
$duration = number_format(round(microtime(true) - $this->jobStartedAt, 3), 3);
93-
$this->command->stdout(" ($duration s)", Console::FG_YELLOW);
92+
if ($this->jobStartedAt) {
93+
$duration = number_format(round(microtime(true) - $this->jobStartedAt, 3), 3);
94+
$this->command->stdout(" ($duration s)", Console::FG_YELLOW);
95+
}
9496
$this->command->stdout(PHP_EOL);
9597
$this->command->stdout('> ' . get_class($event->error) . ': ', Console::FG_RED);
96-
$this->command->stdout($event->error->getMessage(), Console::FG_GREY);
98+
$message = explode("\n", ltrim($event->error->getMessage()), 2)[0]; // First line
99+
$this->command->stdout($message, Console::FG_GREY);
97100
$this->command->stdout(PHP_EOL);
98101
}
99102

0 commit comments

Comments
 (0)