Skip to content

Commit 353dc8f

Browse files
committed
yiisoft#10: Separate option that turn off isolate mode of job execute
1 parent 36da080 commit 353dc8f

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

src/Command.php

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,29 @@ abstract class Command extends Controller
2323
*/
2424
public $queue;
2525
/**
26-
* @var boolean
26+
* @var bool verbose mode of a job execute. If enabled, execute result of each job
27+
* will be printed.
2728
*/
2829
public $verbose = false;
29-
30-
public function init()
31-
{
32-
parent::init();
33-
$this->queue->messageHandler = function ($message) {
34-
return $this->handleMessage($message);
35-
};
36-
}
30+
/**
31+
* @var bool isolate mode. It executes a job in a child process.
32+
*/
33+
public $isolate = true;
3734

3835
/**
3936
* @inheritdoc
4037
*/
4138
public function options($actionID)
4239
{
43-
return array_merge(parent::options($actionID), [
44-
'verbose',
45-
]);
40+
$options = parent::options($actionID);
41+
if ($this->useVerboseOption($actionID)) {
42+
$options[] = 'verbose';
43+
}
44+
if ($this->useIsolateOption($actionID)) {
45+
$options[] = 'isolate';
46+
}
47+
48+
return $options;
4649
}
4750

4851
/**
@@ -55,15 +58,41 @@ public function optionAliases()
5558
]);
5659
}
5760

61+
/**
62+
* @param string $actionID
63+
* @return bool
64+
*/
65+
protected function useVerboseOption($actionID)
66+
{
67+
return in_array($actionID, ['exec', 'run', 'listen']);
68+
}
69+
70+
/**
71+
* @param string $actionID
72+
* @return bool
73+
*/
74+
protected function useIsolateOption($actionID)
75+
{
76+
return in_array($actionID, ['run', 'listen']);
77+
}
78+
5879
/**
5980
* @inheritdoc
6081
*/
6182
public function beforeAction($action)
6283
{
63-
if ($this->verbose) {
84+
if ($this->useVerboseOption($action->id) && $this->verbose) {
6485
$this->queue->attachBehavior('verbose', VerboseBehavior::class);
6586
}
6687

88+
if ($this->useIsolateOption($action->id) && $this->isolate) {
89+
$this->queue->messageHandler = function ($message) {
90+
return $this->handleMessage($message);
91+
};
92+
} else {
93+
$this->queue->messageHandler = null;
94+
}
95+
6796
return parent::beforeAction($action);
6897
}
6998

@@ -86,12 +115,17 @@ public function actionExec()
86115
private function handleMessage($message)
87116
{
88117
// Executes child process
89-
$cmd = strtr('{php} {yii} {queue}/exec --verbose={verbose}', [
118+
$cmd = strtr('{php} {yii} {queue}/exec', [
90119
'{php}' => PHP_BINARY,
91120
'{yii}' => $_SERVER['SCRIPT_FILENAME'],
92121
'{queue}' => $this->id,
93-
'{verbose}' => (int) $this->verbose,
94122
]);
123+
foreach ($this->getPassedOptions() as $name) {
124+
if (in_array($name, $this->options('exec'))) {
125+
$cmd .= ' --' . $name . '=' . $this->$name;
126+
}
127+
}
128+
95129
$descriptors = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']];
96130
$process = proc_open($cmd, $descriptors, $pipes);
97131
if (is_resource($process)) {

0 commit comments

Comments
 (0)