|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Gearman Bundle for Symfony2 |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* Feel free to edit as you please, and have fun. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Marc Morera <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Mmoreram\GearmanBundle\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
|
|
22
|
|
|
use Mmoreram\GearmanBundle\Command\Abstracts\AbstractGearmanCommand; |
|
23
|
|
|
use Mmoreram\GearmanBundle\Service\GearmanClient; |
|
24
|
|
|
use Mmoreram\GearmanBundle\Service\GearmanDescriber; |
|
25
|
|
|
use Mmoreram\GearmanBundle\Service\GearmanExecute; |
|
26
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gearman Worker Execute Command class |
|
30
|
|
|
* |
|
31
|
|
|
* @since 2.3.1 |
|
32
|
|
|
*/ |
|
33
|
|
|
class GearmanWorkerExecuteCommand extends AbstractGearmanCommand |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var GearmanClient |
|
37
|
|
|
* |
|
38
|
|
|
* Gearman client |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $gearmanClient; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var GearmanDescriber |
|
44
|
|
|
* |
|
45
|
|
|
* GearmanDescriber |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $gearmanDescriber; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var GearmanExecute |
|
51
|
|
|
* |
|
52
|
|
|
* Gearman execute |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $gearmanExecute; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set gearman client |
|
58
|
|
|
* |
|
59
|
|
|
* @param GearmanClient $gearmanClient Gearman client |
|
60
|
|
|
* |
|
61
|
|
|
* @return GearmanWorkerExecuteCommand self Object |
|
62
|
|
|
*/ |
|
63
|
8 |
|
public function setGearmanClient(GearmanClient $gearmanClient) |
|
64
|
|
|
{ |
|
65
|
8 |
|
$this->gearmanClient = $gearmanClient; |
|
66
|
|
|
|
|
67
|
8 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* set Gearman describer |
|
72
|
|
|
* |
|
73
|
|
|
* @param GearmanDescriber $gearmanDescriber GearmanDescriber |
|
74
|
|
|
* |
|
75
|
|
|
* @return GearmanWorkerExecuteCommand self Object |
|
76
|
|
|
*/ |
|
77
|
8 |
|
public function setGearmanDescriber(GearmanDescriber $gearmanDescriber) |
|
78
|
|
|
{ |
|
79
|
8 |
|
$this->gearmanDescriber = $gearmanDescriber; |
|
80
|
|
|
|
|
81
|
8 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* set Gearman execute |
|
86
|
|
|
* |
|
87
|
|
|
* @param GearmanExecute $gearmanExecute GearmanExecute |
|
88
|
|
|
* |
|
89
|
|
|
* @return GearmanWorkerExecuteCommand self Object |
|
90
|
|
|
*/ |
|
91
|
8 |
|
public function setGearmanExecute(GearmanExecute $gearmanExecute) |
|
92
|
|
|
{ |
|
93
|
8 |
|
$this->gearmanExecute = $gearmanExecute; |
|
94
|
|
|
|
|
95
|
8 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Console Command configuration |
|
100
|
|
|
*/ |
|
101
|
8 |
|
protected function configure() |
|
102
|
|
|
{ |
|
103
|
8 |
|
parent::configure(); |
|
104
|
|
|
|
|
105
|
|
|
$this |
|
106
|
8 |
|
->setName('gearman:worker:execute') |
|
107
|
8 |
|
->setDescription('Execute one worker with all contained Jobs') |
|
108
|
8 |
|
->addArgument( |
|
109
|
8 |
|
'worker', |
|
110
|
8 |
|
InputArgument::REQUIRED, |
|
111
|
8 |
|
'work to execute' |
|
112
|
|
|
) |
|
113
|
8 |
|
->addOption( |
|
114
|
8 |
|
'no-description', |
|
115
|
8 |
|
null, |
|
116
|
8 |
|
InputOption::VALUE_NONE, |
|
117
|
8 |
|
'Don\'t print worker description' |
|
118
|
|
|
) |
|
119
|
8 |
|
->addOption( |
|
120
|
8 |
|
'iterations', |
|
121
|
8 |
|
null, |
|
122
|
8 |
|
InputOption::VALUE_OPTIONAL, |
|
123
|
8 |
|
'Override configured iterations' |
|
124
|
|
|
) |
|
125
|
8 |
|
->addOption( |
|
126
|
8 |
|
'minimum-execution-time', |
|
127
|
8 |
|
null, |
|
128
|
8 |
|
InputOption::VALUE_OPTIONAL, |
|
129
|
8 |
|
'Override configured minimum execution time' |
|
130
|
|
|
) |
|
131
|
8 |
|
->addOption( |
|
132
|
8 |
|
'timeout', |
|
133
|
8 |
|
null, |
|
134
|
8 |
|
InputOption::VALUE_OPTIONAL, |
|
135
|
8 |
|
'Override configured timeout' |
|
136
|
|
|
); |
|
137
|
8 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Executes the current command. |
|
141
|
|
|
* |
|
142
|
|
|
* @param InputInterface $input An InputInterface instance |
|
143
|
|
|
* @param OutputInterface $output An OutputInterface instance |
|
144
|
|
|
* |
|
145
|
|
|
* @return integer 0 if everything went fine, or an error code |
|
146
|
|
|
* |
|
147
|
|
|
* @throws \LogicException When this abstract class is not implemented |
|
148
|
|
|
*/ |
|
149
|
8 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
150
|
|
|
{ |
|
151
|
|
|
/** |
|
152
|
|
|
* @var QuestionHelper $question |
|
153
|
|
|
*/ |
|
154
|
|
|
$question = $this |
|
155
|
8 |
|
->getHelperSet() |
|
156
|
8 |
|
->get('question'); |
|
157
|
|
|
|
|
158
|
|
|
if ( |
|
159
|
8 |
|
!$input->getOption('no-interaction') && |
|
160
|
4 |
|
!$question->ask( |
|
161
|
4 |
|
$input, |
|
162
|
4 |
|
$output, |
|
163
|
8 |
|
new ConfirmationQuestion('This will execute asked worker with all its jobs?') |
|
164
|
|
|
) |
|
165
|
|
|
) { |
|
166
|
2 |
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
6 |
|
if (!$input->getOption('quiet')) { |
|
170
|
|
|
|
|
171
|
3 |
|
$output->writeln(sprintf( |
|
172
|
3 |
|
'<info>[%s] loading...</info>', |
|
173
|
3 |
|
date('Y-m-d H:i:s') |
|
174
|
|
|
)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
6 |
|
$worker = $input->getArgument('worker'); |
|
178
|
|
|
|
|
179
|
|
|
$workerStructure = $this |
|
180
|
6 |
|
->gearmanClient |
|
181
|
6 |
|
->getWorker($worker); |
|
182
|
|
|
|
|
183
|
|
|
if ( |
|
184
|
6 |
|
!$input->getOption('no-description') && |
|
185
|
6 |
|
!$input->getOption('quiet') |
|
186
|
|
|
) { |
|
187
|
|
|
$this |
|
188
|
3 |
|
->gearmanDescriber |
|
189
|
3 |
|
->describeWorker( |
|
190
|
3 |
|
$output, |
|
191
|
3 |
|
$workerStructure, |
|
192
|
3 |
|
true |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
6 |
|
if (!$input->getOption('quiet')) { |
|
197
|
|
|
|
|
198
|
3 |
|
$output->writeln(sprintf( |
|
199
|
3 |
|
'<info>[%s] loaded. Ctrl+C to break</info>', |
|
200
|
3 |
|
date('Y-m-d H:i:s') |
|
201
|
|
|
)); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$this |
|
205
|
6 |
|
->gearmanExecute |
|
206
|
6 |
|
->setOutput($output) |
|
207
|
6 |
|
->executeWorker($worker, array( |
|
208
|
6 |
|
'iterations' => $input->getOption('iterations'), |
|
209
|
6 |
|
'minimum_execution_time' => $input->getOption('minimum-execution-time'), |
|
210
|
6 |
|
'timeout' => $input->getOption('timeout') |
|
211
|
|
|
)); |
|
212
|
6 |
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|