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
|
|
|
* @author Mickael Perraud <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Mkk\GearmanBundle\Command; |
16
|
|
|
|
17
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
23
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Gearman Job Execute Command class |
27
|
|
|
*/ |
28
|
|
|
class GearmanJobExecuteCommand extends ContainerAwareCommand |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var QuestionHelper |
33
|
|
|
* |
34
|
|
|
* Question |
35
|
|
|
*/ |
36
|
|
|
protected $question; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Console Command configuration |
40
|
|
|
*/ |
41
|
16 |
|
protected function configure() |
42
|
|
|
{ |
43
|
|
|
$this |
44
|
16 |
|
->setName('gearman:job:execute') |
45
|
16 |
|
->setDescription('Execute one single job') |
46
|
16 |
|
->addArgument( |
47
|
16 |
|
'job', |
48
|
16 |
|
InputArgument::REQUIRED, |
49
|
16 |
|
'job to execute' |
50
|
|
|
) |
51
|
16 |
|
->addOption( |
52
|
16 |
|
'no-description', |
53
|
16 |
|
null, |
54
|
16 |
|
InputOption::VALUE_NONE, |
55
|
16 |
|
'Don\'t print job description' |
56
|
|
|
) |
57
|
16 |
|
->addOption( |
58
|
16 |
|
'iterations', |
59
|
16 |
|
null, |
60
|
16 |
|
InputOption::VALUE_OPTIONAL, |
61
|
16 |
|
'Override configured iterations' |
62
|
|
|
) |
63
|
16 |
|
->addOption( |
64
|
16 |
|
'minimum-execution-time', |
65
|
16 |
|
null, |
66
|
16 |
|
InputOption::VALUE_OPTIONAL, |
67
|
16 |
|
'Override configured minimum execution time' |
68
|
|
|
) |
69
|
16 |
|
->addOption( |
70
|
16 |
|
'timeout', |
71
|
16 |
|
null, |
72
|
16 |
|
InputOption::VALUE_OPTIONAL, |
73
|
16 |
|
'Override configured timeout' |
74
|
|
|
); |
75
|
16 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Executes the current command. |
79
|
|
|
* |
80
|
|
|
* @param InputInterface $input An InputInterface instance |
81
|
|
|
* @param OutputInterface $output An OutputInterface instance |
82
|
|
|
* |
83
|
|
|
* @return integer 0 if everything went fine, or an error code |
|
|
|
|
84
|
|
|
* |
85
|
|
|
* @throws \LogicException When this abstract class is not implemented |
86
|
|
|
*/ |
87
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
88
|
|
|
{ |
89
|
|
|
/** |
90
|
|
|
* @var QuestionHelper $question |
91
|
|
|
*/ |
92
|
3 |
|
$question = $this->getHelperSet()->get('question'); |
93
|
|
|
|
94
|
|
|
if ( |
95
|
3 |
|
!$input->getOption('no-interaction') && |
96
|
2 |
|
!$question->ask( |
97
|
2 |
|
$input, |
98
|
2 |
|
$output, |
99
|
3 |
|
new ConfirmationQuestion('This will execute asked job?') |
100
|
|
|
) |
101
|
|
|
) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
if (!$input->getOption('quiet')) { |
106
|
|
|
|
107
|
2 |
|
$output->writeln(sprintf( |
108
|
2 |
|
'<info>[%s] loading...</info>', |
109
|
2 |
|
date('Y-m-d H:i:s') |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
$job = $input->getArgument('job'); |
114
|
|
|
$jobStructure = $this |
115
|
3 |
|
->getContainer()->get('gearman') |
116
|
3 |
|
->getJob($job); |
117
|
|
|
|
118
|
|
|
if ( |
119
|
3 |
|
!$input->getOption('no-description') && |
120
|
3 |
|
!$input->getOption('quiet') |
121
|
|
|
) { |
122
|
|
|
|
123
|
|
|
$this |
124
|
2 |
|
->getContainer()->get('gearman.describer') |
125
|
2 |
|
->describeJob( |
126
|
2 |
|
$output, |
127
|
2 |
|
$jobStructure |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
if (!$input->getOption('quiet')) { |
132
|
|
|
|
133
|
2 |
|
$output->writeln(sprintf( |
134
|
2 |
|
'<info>[%s] loaded. Ctrl+C to break</info>', |
135
|
2 |
|
date('Y-m-d H:i:s') |
136
|
|
|
)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this |
140
|
3 |
|
->getContainer()->get('gearman.execute') |
141
|
3 |
|
->setOutput($output) |
142
|
3 |
|
->executeJob($job, array( |
143
|
3 |
|
'iterations' => $input->getOption('iterations'), |
144
|
3 |
|
'minimum_execution_time' => $input->getOption('minimum-execution-time'), |
145
|
3 |
|
'timeout' => $input->getOption('timeout') |
146
|
|
|
)); |
147
|
3 |
|
} |
148
|
|
|
} |
149
|
|
|
|
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.