1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\Generator; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Templating\EngineInterface; |
6
|
|
|
|
7
|
|
|
class RabbitMqConfigGenerator implements RabbitMqConfigGeneratorInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var EngineInterface |
11
|
|
|
*/ |
12
|
|
|
private $templating; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $config; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param EngineInterface $templating |
21
|
|
|
* @param array $config |
22
|
|
|
*/ |
23
|
|
|
public function __construct(EngineInterface $templating, array $config) |
24
|
|
|
{ |
25
|
|
|
$this->templating = $templating; |
26
|
|
|
$this->config = $config; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function generate() |
33
|
|
|
{ |
34
|
|
|
if (!is_dir($this->config['path']) && !mkdir($this->config['path'], 0755, true)) { |
35
|
|
|
throw new \RuntimeException(sprintf( |
36
|
|
|
'path "%s" could not be created', |
37
|
|
|
$this->config['path'] |
38
|
|
|
)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// create directory structure |
42
|
|
|
foreach (['worker' => true, 'conf.d' => true, 'logs' => false] as $directory => $cleanup) { |
43
|
|
|
$path = sprintf('%s/%s', $this->config['path'], $directory); |
44
|
|
|
|
45
|
|
|
if (!is_dir($path)) { |
46
|
|
|
mkdir($path, 0755); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($cleanup) { |
50
|
|
|
$this->cleanup($path); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
file_put_contents( |
55
|
|
|
sprintf('%s/%s', $this->config['path'], 'supervisord.conf'), |
56
|
|
|
$this->templating->render( |
57
|
|
|
'RabbitMqManagerBundle:Supervisor:supervisor.conf.twig', [ |
58
|
|
|
'path' => $this->config['path'], |
59
|
|
|
] |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
foreach (['consumers', 'rpc_servers'] as $type) { |
64
|
|
|
foreach ($this->config[$type] as $name => $consumer) { |
65
|
|
|
if (!isset($consumer['worker']['queue']['routing'])) { |
66
|
|
|
$this->writeConfig( |
67
|
|
|
sprintf('%s_%s', $type, $name), |
68
|
|
|
$this->config['path'], |
69
|
|
|
$consumer |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach ($consumer['worker']['queue']['routing'] as $index => $route) { |
76
|
|
|
$this->writeConfig( |
77
|
|
|
sprintf('%s_%s_%s', $type, $name, $index), |
78
|
|
|
$this->config['path'], |
79
|
|
|
$consumer, |
80
|
|
|
$route |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @param string $path |
90
|
|
|
* @param array $consumer |
91
|
|
|
* @param null $route |
92
|
|
|
*/ |
93
|
|
|
private function writeConfig($name, $path, array $consumer, $route = null) |
94
|
|
|
{ |
95
|
|
|
if ('cli-consumer' === $consumer['processor']) { |
96
|
|
|
// write additional cli-consumer config |
97
|
|
|
file_put_contents( |
98
|
|
|
$consumerConfiguration = sprintf('%s/worker/%s.conf', $path, $name), |
99
|
|
|
$this->templating->render('RabbitMqManagerBundle:Supervisor:consumer.conf.twig', [ |
100
|
|
|
'path' => $path, |
101
|
|
|
'routing_key' => $route, |
102
|
|
|
'consumer' => $consumer, |
103
|
|
|
]) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$content = $this->templating->render('RabbitMqManagerBundle:Supervisor/processor:cli-consumer.conf.twig', [ |
107
|
|
|
'path' => $path, |
108
|
|
|
'configuration' => $consumerConfiguration, |
109
|
|
|
'consumer' => $consumer, |
110
|
|
|
]); |
111
|
|
|
} else { |
112
|
|
|
$consumer['command']['arguments'][] = sprintf('--messages=%s', $consumer['messages']); |
113
|
|
|
|
114
|
|
|
if (null !== $route) { |
115
|
|
|
$consumer['command']['arguments'][] = sprintf('--route=%s', $route); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$content = $this->templating->render('RabbitMqManagerBundle:Supervisor/processor:bundle.conf.twig', [ |
119
|
|
|
'path' => $path, |
120
|
|
|
'consumer' => $consumer, |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
file_put_contents( |
125
|
|
|
sprintf('%s/conf.d/%s.conf', $path, $name), |
126
|
|
|
$content |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $path |
132
|
|
|
*/ |
133
|
|
|
private function cleanup($path) |
134
|
|
|
{ |
135
|
|
|
/** @var \SplFileInfo $item */ |
136
|
|
|
foreach (new \DirectoryIterator($path) as $item) { |
137
|
|
|
if ($item->isDir()) { |
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ('conf' !== $item->getExtension()) { |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
unlink($item->getRealPath()); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|