1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Gearman Bundle for Symfony2 / Symfony3 |
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 Mkk\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
|
|
|
use Mkk\GearmanBundle\Command\Abstracts\AbstractGearmanCommand; |
22
|
|
|
use Mkk\GearmanBundle\Service\GearmanClient; |
23
|
|
|
use Mkk\GearmanBundle\Service\GearmanDescriber; |
24
|
|
|
use Mkk\GearmanBundle\Service\GearmanExecute; |
25
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Gearman Job Execute Command class |
29
|
|
|
*/ |
30
|
|
|
class GearmanJobExecuteCommand extends AbstractGearmanCommand |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var GearmanClient |
34
|
|
|
* |
35
|
|
|
* Gearman client |
36
|
|
|
*/ |
37
|
|
|
protected $gearmanClient; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var GearmanDescriber |
41
|
|
|
* |
42
|
|
|
* GearmanDescriber |
43
|
|
|
*/ |
44
|
|
|
protected $gearmanDescriber; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var GearmanExecute |
48
|
|
|
* |
49
|
|
|
* Gearman execute |
50
|
|
|
*/ |
51
|
|
|
protected $gearmanExecute; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var QuestionHelper |
55
|
|
|
* |
56
|
|
|
* Question |
57
|
|
|
*/ |
58
|
|
|
protected $question; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set gearman client |
62
|
|
|
* |
63
|
|
|
* @param GearmanClient $gearmanClient Gearman client |
64
|
|
|
* |
65
|
|
|
* @return GearmanJobExecuteCommand self Object |
66
|
|
|
*/ |
67
|
8 |
|
public function setGearmanClient(GearmanClient $gearmanClient) |
68
|
|
|
{ |
69
|
8 |
|
$this->gearmanClient = $gearmanClient; |
70
|
|
|
|
71
|
8 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* set Gearman describer |
76
|
|
|
* |
77
|
|
|
* @param GearmanDescriber $gearmanDescriber GearmanDescriber |
78
|
|
|
* |
79
|
|
|
* @return GearmanJobExecuteCommand self Object |
80
|
|
|
*/ |
81
|
8 |
|
public function setGearmanDescriber(GearmanDescriber $gearmanDescriber) |
82
|
|
|
{ |
83
|
8 |
|
$this->gearmanDescriber = $gearmanDescriber; |
84
|
|
|
|
85
|
8 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* set Gearman execute |
90
|
|
|
* |
91
|
|
|
* @param GearmanExecute $gearmanExecute GearmanExecute |
92
|
|
|
* |
93
|
|
|
* @return GearmanJobExecuteCommand self Object |
94
|
|
|
*/ |
95
|
8 |
|
public function setGearmanExecute(GearmanExecute $gearmanExecute) |
96
|
|
|
{ |
97
|
8 |
|
$this->gearmanExecute = $gearmanExecute; |
98
|
|
|
|
99
|
8 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Console Command configuration |
104
|
|
|
*/ |
105
|
8 |
|
protected function configure() |
106
|
|
|
{ |
107
|
|
|
$this |
108
|
8 |
|
->setName('gearman:job:execute') |
109
|
8 |
|
->setDescription('Execute one single job') |
110
|
8 |
|
->addArgument( |
111
|
8 |
|
'job', |
112
|
8 |
|
InputArgument::REQUIRED, |
113
|
8 |
|
'job to execute' |
114
|
|
|
) |
115
|
8 |
|
->addOption( |
116
|
8 |
|
'no-description', |
117
|
8 |
|
null, |
118
|
8 |
|
InputOption::VALUE_NONE, |
119
|
8 |
|
'Don\'t print job description' |
120
|
|
|
) |
121
|
8 |
|
->addOption( |
122
|
8 |
|
'iterations', |
123
|
8 |
|
null, |
124
|
8 |
|
InputOption::VALUE_OPTIONAL, |
125
|
8 |
|
'Override configured iterations' |
126
|
|
|
) |
127
|
8 |
|
->addOption( |
128
|
8 |
|
'minimum-execution-time', |
129
|
8 |
|
null, |
130
|
8 |
|
InputOption::VALUE_OPTIONAL, |
131
|
8 |
|
'Override configured minimum execution time' |
132
|
|
|
) |
133
|
8 |
|
->addOption( |
134
|
8 |
|
'timeout', |
135
|
8 |
|
null, |
136
|
8 |
|
InputOption::VALUE_OPTIONAL, |
137
|
8 |
|
'Override configured timeout' |
138
|
|
|
); |
139
|
8 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Executes the current command. |
143
|
|
|
* |
144
|
|
|
* @param InputInterface $input An InputInterface instance |
145
|
|
|
* @param OutputInterface $output An OutputInterface instance |
146
|
|
|
* |
147
|
|
|
* @return integer 0 if everything went fine, or an error code |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @throws \LogicException When this abstract class is not implemented |
150
|
|
|
*/ |
151
|
8 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
152
|
|
|
{ |
153
|
|
|
/** |
154
|
|
|
* @var QuestionHelper $question |
155
|
|
|
*/ |
156
|
8 |
|
$question = $this->getHelperSet()->get('question'); |
157
|
|
|
|
158
|
|
|
if ( |
159
|
8 |
|
!$input->getOption('no-interaction') && |
160
|
4 |
|
!$question->ask( |
161
|
|
|
$input, |
162
|
|
|
$output, |
163
|
8 |
|
new ConfirmationQuestion('This will execute asked job?') |
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 |
|
$job = $input->getArgument('job'); |
|
|
|
|
178
|
|
|
$jobStructure = $this |
179
|
6 |
|
->gearmanClient |
180
|
6 |
|
->getJob($job); |
181
|
|
|
|
182
|
|
|
if ( |
183
|
6 |
|
!$input->getOption('no-description') && |
184
|
6 |
|
!$input->getOption('quiet') |
185
|
|
|
) { |
186
|
|
|
|
187
|
|
|
$this |
188
|
3 |
|
->gearmanDescriber |
189
|
3 |
|
->describeJob( |
190
|
|
|
$output, |
191
|
|
|
$jobStructure |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
6 |
|
if (!$input->getOption('quiet')) { |
196
|
|
|
|
197
|
3 |
|
$output->writeln(sprintf( |
198
|
3 |
|
'<info>[%s] loaded. Ctrl+C to break</info>', |
199
|
3 |
|
date('Y-m-d H:i:s') |
200
|
|
|
)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this |
204
|
6 |
|
->gearmanExecute |
205
|
6 |
|
->setOutput($output) |
206
|
6 |
|
->executeJob($job, array( |
207
|
6 |
|
'iterations' => $input->getOption('iterations'), |
208
|
6 |
|
'minimum_execution_time' => $input->getOption('minimum-execution-time'), |
209
|
6 |
|
'timeout' => $input->getOption('timeout') |
210
|
|
|
)); |
211
|
6 |
|
} |
212
|
|
|
} |
|
|
|
|
213
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.