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 Job Execute Command class |
30
|
|
|
* |
31
|
|
|
* @since 2.3.1 |
32
|
|
|
*/ |
33
|
|
|
class GearmanJobExecuteCommand 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
|
|
|
* @var QuestionHelper |
58
|
|
|
* |
59
|
|
|
* Question |
60
|
|
|
*/ |
61
|
|
|
protected $question; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set gearman client |
65
|
|
|
* |
66
|
|
|
* @param GearmanClient $gearmanClient Gearman client |
67
|
|
|
* |
68
|
|
|
* @return GearmanJobExecuteCommand self Object |
69
|
|
|
*/ |
70
|
8 |
|
public function setGearmanClient(GearmanClient $gearmanClient) |
71
|
|
|
{ |
72
|
8 |
|
$this->gearmanClient = $gearmanClient; |
73
|
|
|
|
74
|
8 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* set Gearman describer |
79
|
|
|
* |
80
|
|
|
* @param GearmanDescriber $gearmanDescriber GearmanDescriber |
81
|
|
|
* |
82
|
|
|
* @return GearmanJobExecuteCommand self Object |
83
|
|
|
*/ |
84
|
8 |
|
public function setGearmanDescriber(GearmanDescriber $gearmanDescriber) |
85
|
|
|
{ |
86
|
8 |
|
$this->gearmanDescriber = $gearmanDescriber; |
87
|
|
|
|
88
|
8 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* set Gearman execute |
93
|
|
|
* |
94
|
|
|
* @param GearmanExecute $gearmanExecute GearmanExecute |
95
|
|
|
* |
96
|
|
|
* @return GearmanJobExecuteCommand self Object |
97
|
|
|
*/ |
98
|
8 |
|
public function setGearmanExecute(GearmanExecute $gearmanExecute) |
99
|
|
|
{ |
100
|
8 |
|
$this->gearmanExecute = $gearmanExecute; |
101
|
|
|
|
102
|
8 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Console Command configuration |
107
|
|
|
*/ |
108
|
8 |
|
protected function configure() |
109
|
|
|
{ |
110
|
|
|
$this |
111
|
8 |
|
->setName('gearman:job:execute') |
112
|
8 |
|
->setDescription('Execute one single job') |
113
|
8 |
|
->addArgument( |
114
|
8 |
|
'job', |
115
|
8 |
|
InputArgument::REQUIRED, |
116
|
8 |
|
'job to execute' |
117
|
|
|
) |
118
|
8 |
|
->addOption( |
119
|
8 |
|
'no-description', |
120
|
8 |
|
null, |
121
|
8 |
|
InputOption::VALUE_NONE, |
122
|
8 |
|
'Don\'t print job description' |
123
|
|
|
) |
124
|
8 |
|
->addOption( |
125
|
8 |
|
'iterations', |
126
|
8 |
|
null, |
127
|
8 |
|
InputOption::VALUE_OPTIONAL, |
128
|
8 |
|
'Override configured iterations' |
129
|
|
|
) |
130
|
8 |
|
->addOption( |
131
|
8 |
|
'minimum-execution-time', |
132
|
8 |
|
null, |
133
|
8 |
|
InputOption::VALUE_OPTIONAL, |
134
|
8 |
|
'Override configured minimum execution time' |
135
|
|
|
) |
136
|
8 |
|
->addOption( |
137
|
8 |
|
'timeout', |
138
|
8 |
|
null, |
139
|
8 |
|
InputOption::VALUE_OPTIONAL, |
140
|
8 |
|
'Override configured timeout' |
141
|
|
|
); |
142
|
8 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Executes the current command. |
146
|
|
|
* |
147
|
|
|
* @param InputInterface $input An InputInterface instance |
148
|
|
|
* @param OutputInterface $output An OutputInterface instance |
149
|
|
|
* |
150
|
|
|
* @return integer 0 if everything went fine, or an error code |
151
|
|
|
* |
152
|
|
|
* @throws \LogicException When this abstract class is not implemented |
153
|
|
|
*/ |
154
|
8 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
155
|
|
|
{ |
156
|
|
|
/** |
157
|
|
|
* @var QuestionHelper $question |
158
|
|
|
*/ |
159
|
8 |
|
$question = $this->getHelperSet()->get('question'); |
160
|
|
|
|
161
|
|
|
if ( |
162
|
8 |
|
!$input->getOption('no-interaction') && |
163
|
4 |
|
!$question->ask( |
164
|
4 |
|
$input, |
165
|
4 |
|
$output, |
166
|
8 |
|
new ConfirmationQuestion('This will execute asked job?') |
167
|
|
|
) |
168
|
|
|
) { |
169
|
2 |
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
6 |
|
if (!$input->getOption('quiet')) { |
173
|
|
|
|
174
|
3 |
|
$output->writeln(sprintf( |
175
|
3 |
|
'<info>[%s] loading...</info>', |
176
|
3 |
|
date('Y-m-d H:i:s') |
177
|
|
|
)); |
178
|
|
|
} |
179
|
|
|
|
180
|
6 |
|
$job = $input->getArgument('job'); |
181
|
|
|
$jobStructure = $this |
182
|
6 |
|
->gearmanClient |
183
|
6 |
|
->getJob($job); |
184
|
|
|
|
185
|
|
|
if ( |
186
|
6 |
|
!$input->getOption('no-description') && |
187
|
6 |
|
!$input->getOption('quiet') |
188
|
|
|
) { |
189
|
|
|
|
190
|
|
|
$this |
191
|
3 |
|
->gearmanDescriber |
192
|
3 |
|
->describeJob( |
193
|
3 |
|
$output, |
194
|
3 |
|
$jobStructure, |
195
|
3 |
|
true |
|
|
|
|
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
6 |
|
if (!$input->getOption('quiet')) { |
200
|
|
|
|
201
|
3 |
|
$output->writeln(sprintf( |
202
|
3 |
|
'<info>[%s] loaded. Ctrl+C to break</info>', |
203
|
3 |
|
date('Y-m-d H:i:s') |
204
|
|
|
)); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this |
208
|
6 |
|
->gearmanExecute |
209
|
6 |
|
->setOutput($output) |
210
|
6 |
|
->executeJob($job, array( |
211
|
6 |
|
'iterations' => $input->getOption('iterations'), |
212
|
6 |
|
'minimum_execution_time' => $input->getOption('minimum-execution-time'), |
213
|
6 |
|
'timeout' => $input->getOption('timeout') |
214
|
|
|
)); |
215
|
6 |
|
} |
216
|
|
|
} |
217
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.