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\Service; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Implementation of GearmanDescriber |
21
|
|
|
* |
22
|
|
|
* @since 2.3.1 |
23
|
|
|
*/ |
24
|
|
|
class GearmanDescriber |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var KernelInterface |
28
|
|
|
* |
29
|
|
|
* Kernel |
30
|
|
|
*/ |
31
|
|
|
private $kernel; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct method |
35
|
|
|
* |
36
|
|
|
* @param KernelInterface $kernel Kernel |
37
|
|
|
*/ |
38
|
1 |
|
public function __construct(KernelInterface $kernel) |
39
|
|
|
{ |
40
|
1 |
|
$this->kernel = $kernel; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Describe Job. |
45
|
|
|
* |
46
|
|
|
* Given a output object and a Job, dscribe it. |
47
|
|
|
* |
48
|
|
|
* @param OutputInterface $output Output object |
49
|
|
|
* @param array $worker Worker array with Job to describe |
50
|
|
|
*/ |
51
|
|
|
public function describeJob(OutputInterface $output, array $worker) |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Commandline |
55
|
|
|
*/ |
56
|
|
|
$script = $this->kernel->getRootDir() . '/console gearman:job:execute'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* A job descriptions contains its worker description |
60
|
|
|
*/ |
61
|
|
|
$this->describeWorker($output, $worker); |
62
|
|
|
|
63
|
|
|
$job = $worker['job']; |
64
|
|
|
$output->writeln('<info>@job\methodName : ' . $job['methodName'] . '</info>'); |
65
|
|
|
$output->writeln('<info>@job\callableName : ' . $job['realCallableName'] . '</info>'); |
66
|
|
|
|
67
|
|
|
if ($job['jobPrefix']) { |
68
|
|
|
$output->writeln('<info>@job\jobPrefix : ' . $job['jobPrefix'] . '</info>'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Also a complete and clean execution path is given , for supervisord |
73
|
|
|
*/ |
74
|
|
|
$output->writeln('<info>@job\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $job['realCallableName'] . ' --no-interaction</comment>'); |
75
|
|
|
$output->writeln('<info>@job\iterations : ' . $job['iterations'] . '</info>'); |
76
|
|
|
$output->writeln('<info>@job\defaultMethod : ' . $job['defaultMethod'] . '</info>'); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Printed every server is defined for current job |
80
|
|
|
*/ |
81
|
|
|
$output->writeln(''); |
82
|
|
|
$output->writeln('<info>@job\servers :</info>'); |
83
|
|
|
$output->writeln(''); |
84
|
|
|
foreach ($job['servers'] as $name => $server) { |
85
|
|
|
$output->writeln('<comment> ' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Description |
90
|
|
|
*/ |
91
|
|
|
$output->writeln(''); |
92
|
|
|
$output->writeln('<info>@job\description :</info>'); |
93
|
|
|
$output->writeln(''); |
94
|
|
|
$output->writeln('<comment> #' . $job['description'] . '</comment>'); |
95
|
|
|
$output->writeln(''); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Describe Worker. |
100
|
|
|
* |
101
|
|
|
* Given a output object and a Worker, dscribe it. |
102
|
|
|
* |
103
|
|
|
* @param OutputInterface $output Output object |
104
|
|
|
* @param array $worker Worker array with Job to describe |
105
|
|
|
* @param Boolean $tinyJobDescription If true also print job list |
106
|
|
|
*/ |
107
|
|
|
public function describeWorker(OutputInterface $output, array $worker, $tinyJobDescription = false) |
108
|
|
|
{ |
109
|
|
|
/** |
110
|
|
|
* Commandline |
111
|
|
|
*/ |
112
|
|
|
$script = $this->kernel->getRootDir() . '/console gearman:worker:execute'; |
113
|
|
|
|
114
|
|
|
$output->writeln(''); |
115
|
|
|
$output->writeln('<info>@Worker\className : ' . $worker['className'] . '</info>'); |
116
|
|
|
$output->writeln('<info>@Worker\fileName : ' . $worker['fileName'] . '</info>'); |
117
|
|
|
$output->writeln('<info>@Worker\nameSpace : ' . $worker['namespace'] . '</info>'); |
118
|
|
|
$output->writeln('<info>@Worker\callableName: ' . $worker['callableName'] . '</info>'); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Also a complete and clean execution path is given , for supervisord |
122
|
|
|
*/ |
123
|
|
|
$output->writeln('<info>@Worker\supervisord : </info><comment>/usr/bin/php ' . $script.' ' . $worker['callableName'] . ' --no-interaction</comment>'); |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Service value is only explained if defined. Not mandatory |
127
|
|
|
*/ |
128
|
|
|
if (null !== $worker['service']) { |
129
|
|
|
$output->writeln('<info>@Worker\service : ' . $worker['service'] . '</info>'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$output->writeln('<info>@worker\iterations : ' . $worker['iterations'] . '</info>'); |
133
|
|
|
$output->writeln('<info>@Worker\#jobs : ' . count($worker['jobs']) . '</info>'); |
134
|
|
|
|
135
|
|
|
if ($tinyJobDescription) { |
136
|
|
|
$output->writeln('<info>@Worker\jobs</info>'); |
137
|
|
|
$output->writeln(''); |
138
|
|
|
foreach ($worker['jobs'] as $job) { |
139
|
|
|
|
140
|
|
|
if ($job['jobPrefix']) { |
141
|
|
|
|
142
|
|
|
$output->writeln('<comment> # ' . $job['realCallableNameNoPrefix'] . ' with jobPrefix: ' . $job['jobPrefix'] . '</comment>'); |
143
|
|
|
} else { |
144
|
|
|
|
145
|
|
|
$output->writeln('<comment> # ' . $job['realCallableNameNoPrefix'] . ' </comment>'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Printed every server is defined for current job |
153
|
|
|
*/ |
154
|
|
|
$output->writeln(''); |
155
|
|
|
$output->writeln('<info>@worker\servers :</info>'); |
156
|
|
|
$output->writeln(''); |
157
|
|
|
foreach ($worker['servers'] as $name => $server) { |
158
|
|
|
$output->writeln('<comment> #' . $name . ' - ' . $server['host'] . ':' . $server['port'] . '</comment>'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Description |
163
|
|
|
*/ |
164
|
|
|
$output->writeln(''); |
165
|
|
|
$output->writeln('<info>@Worker\description :</info>'); |
166
|
|
|
$output->writeln(''); |
167
|
|
|
$output->writeln('<comment> ' . $worker['description'] . '</comment>'); |
168
|
|
|
$output->writeln(''); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|