1 | <?php |
||
7 | class RabbitMqSupervisor |
||
8 | { |
||
9 | /** |
||
10 | * @var Supervisor |
||
11 | */ |
||
12 | private $supervisor; |
||
13 | |||
14 | /** |
||
15 | * @var EngineInterface |
||
16 | */ |
||
17 | private $templating; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $config; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $application; |
||
28 | |||
29 | /** |
||
30 | * @param Supervisor $supervisor |
||
31 | * @param EngineInterface $templating |
||
32 | * @param array $config |
||
33 | * @param string $application |
||
34 | */ |
||
35 | 1 | public function __construct(Supervisor $supervisor, EngineInterface $templating, array $config, $application) |
|
42 | |||
43 | /** |
||
44 | * Generate all supervisor worker configuration files |
||
45 | */ |
||
46 | public function generate() |
||
47 | { |
||
48 | // create directory structure |
||
49 | foreach (['worker', 'conf.d', 'logs'] as $directory) { |
||
50 | $path = sprintf('%s/%s/%s', $this->config['path'], $this->application, $directory); |
||
51 | |||
52 | if (!is_dir($path)) { |
||
53 | mkdir($path, 0755, true); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | // get absolute (root)path |
||
58 | if (!$path = realpath(sprintf('%s/%s', $this->config['path'], $this->application))) { |
||
59 | throw new \RuntimeException(sprintf( |
||
60 | 'path "%s/%s" does not exist.', |
||
61 | $this->config['path'], |
||
62 | $this->application |
||
63 | )); |
||
64 | } |
||
65 | |||
66 | // create the supervisord.conf configuration file |
||
67 | file_put_contents( |
||
68 | sprintf('%s/%s', $path, 'supervisord.conf'), |
||
69 | $this->templating->render( |
||
70 | 'RabbitMqManagerBundle:Supervisor:supervisor.conf.twig', [ |
||
71 | 'path' => $path, |
||
72 | ] |
||
73 | ) |
||
74 | ); |
||
75 | |||
76 | // remove old configuration files |
||
77 | $this->cleanDir(sprintf('%s/%s', $path, 'conf.d')); |
||
78 | $this->cleanDir(sprintf('%s/%s', $path, 'worker')); |
||
79 | |||
80 | foreach (['consumers', 'rpc_servers'] as $type) { |
||
81 | foreach ($this->config[$type] as $name => $consumer) { |
||
82 | if (!isset($consumer['worker']['queue']['routing'])) { |
||
83 | $this->writeConfig( |
||
84 | sprintf('%s_%s', $type, $name), |
||
85 | $path, |
||
86 | $consumer |
||
87 | ); |
||
88 | |||
89 | continue; |
||
90 | } |
||
91 | |||
92 | foreach ($consumer['worker']['queue']['routing'] as $index => $route) { |
||
93 | $this->writeConfig( |
||
94 | sprintf('%s_%s_%s', $type, $name, $index), |
||
95 | $path, |
||
96 | $consumer, |
||
97 | $route |
||
98 | ); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $name |
||
106 | * @param string $path |
||
107 | * @param array $consumer |
||
108 | * @param null $route |
||
109 | */ |
||
110 | private function writeConfig($name, $path, array $consumer, $route = null) |
||
146 | |||
147 | /** |
||
148 | * Stop supervisord and all processes |
||
149 | */ |
||
150 | public function stop() |
||
154 | |||
155 | /** |
||
156 | * Start supervisord and all processes |
||
157 | */ |
||
158 | 1 | public function start() |
|
163 | |||
164 | /** |
||
165 | * Send -HUP to supervisord to gracefully restart all processes |
||
166 | */ |
||
167 | public function hup() |
||
171 | |||
172 | /** |
||
173 | * Send kill signal to supervisord |
||
174 | * |
||
175 | * @param string $signal |
||
176 | * @param bool $waitForProcessToDisappear |
||
177 | */ |
||
178 | public function kill($signal = '', $waitForProcessToDisappear = false) |
||
195 | |||
196 | /** |
||
197 | * Wait for supervisord process to disappear |
||
198 | */ |
||
199 | public function wait() |
||
208 | |||
209 | /** |
||
210 | * Check if a process with the given pid is running |
||
211 | * |
||
212 | * @param int $pid |
||
213 | * @return bool |
||
214 | */ |
||
215 | private function isProcessRunning($pid) { |
||
225 | |||
226 | /** |
||
227 | * Determines the supervisord process id |
||
228 | * |
||
229 | * @return null|int |
||
230 | */ |
||
231 | private function getSupervisorPid() { |
||
242 | |||
243 | /** |
||
244 | * @param string $path |
||
245 | */ |
||
246 | private function cleanDir($path) |
||
262 | } |
||
263 |