@@ -23,26 +23,29 @@ abstract class Command extends Controller
23
23
*/
24
24
public $ queue ;
25
25
/**
26
- * @var boolean
26
+ * @var bool verbose mode of a job execute. If enabled, execute result of each job
27
+ * will be printed.
27
28
*/
28
29
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 ;
37
34
38
35
/**
39
36
* @inheritdoc
40
37
*/
41
38
public function options ($ actionID )
42
39
{
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 ;
46
49
}
47
50
48
51
/**
@@ -55,15 +58,41 @@ public function optionAliases()
55
58
]);
56
59
}
57
60
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
+
58
79
/**
59
80
* @inheritdoc
60
81
*/
61
82
public function beforeAction ($ action )
62
83
{
63
- if ($ this ->verbose ) {
84
+ if ($ this ->useVerboseOption ( $ action -> id ) && $ this -> verbose ) {
64
85
$ this ->queue ->attachBehavior ('verbose ' , VerboseBehavior::class);
65
86
}
66
87
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
+
67
96
return parent ::beforeAction ($ action );
68
97
}
69
98
@@ -86,12 +115,17 @@ public function actionExec()
86
115
private function handleMessage ($ message )
87
116
{
88
117
// Executes child process
89
- $ cmd = strtr ('{php} {yii} {queue}/exec --verbose={verbose} ' , [
118
+ $ cmd = strtr ('{php} {yii} {queue}/exec ' , [
90
119
'{php} ' => PHP_BINARY ,
91
120
'{yii} ' => $ _SERVER ['SCRIPT_FILENAME ' ],
92
121
'{queue} ' => $ this ->id ,
93
- '{verbose} ' => (int ) $ this ->verbose ,
94
122
]);
123
+ foreach ($ this ->getPassedOptions () as $ name ) {
124
+ if (in_array ($ name , $ this ->options ('exec ' ))) {
125
+ $ cmd .= ' -- ' . $ name . '= ' . $ this ->$ name ;
126
+ }
127
+ }
128
+
95
129
$ descriptors = [['pipe ' , 'r ' ], ['pipe ' , 'w ' ], ['pipe ' , 'w ' ]];
96
130
$ process = proc_open ($ cmd , $ descriptors , $ pipes );
97
131
if (is_resource ($ process )) {
0 commit comments