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