|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Supervisor; |
|
4
|
|
|
|
|
5
|
|
|
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Section\SectionCollection; |
|
6
|
|
|
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Process\ProcessBuilderFactoryInterface; |
|
7
|
|
|
use Supervisor\Configuration\Section\Program; |
|
8
|
|
|
|
|
9
|
|
|
class SupervisorConfiguration |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var SupervisorSectionFactoryInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
private $sectionFactory; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var ProcessBuilderFactoryInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $processBuilderFactory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $path; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param SupervisorSectionFactoryInterface $sectionFactory |
|
28
|
|
|
* @param ProcessBuilderFactoryInterface $processBuilderFactory |
|
29
|
|
|
* @param string $path |
|
30
|
|
|
*/ |
|
31
|
5 |
|
public function __construct( |
|
32
|
|
|
SupervisorSectionFactoryInterface $sectionFactory, |
|
33
|
|
|
ProcessBuilderFactoryInterface $processBuilderFactory, |
|
34
|
|
|
$path |
|
35
|
|
|
) { |
|
36
|
5 |
|
$this->sectionFactory = $sectionFactory; |
|
37
|
5 |
|
$this->processBuilderFactory = $processBuilderFactory; |
|
38
|
5 |
|
$this->path = $path; |
|
39
|
5 |
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function generate() |
|
42
|
|
|
{ |
|
43
|
1 |
|
$sections = new SectionCollection(); |
|
44
|
|
|
|
|
45
|
1 |
|
$sections->addSection( |
|
46
|
1 |
|
$this->sectionFactory->createUnixHttpServer( |
|
47
|
|
|
[ |
|
48
|
1 |
|
'file' => sprintf('%s/supervisord.sock', $this->path), |
|
49
|
1 |
|
'chmod' => 700, |
|
50
|
|
|
] |
|
51
|
1 |
|
) |
|
52
|
1 |
|
); |
|
53
|
|
|
|
|
54
|
1 |
|
$sections->addSection( |
|
55
|
1 |
|
$this->sectionFactory->createSupervisord( |
|
56
|
|
|
[ |
|
57
|
1 |
|
'logfile' => sprintf('%s/supervisord.log', $this->path), |
|
58
|
1 |
|
'pidfile' => sprintf('%s/supervisord.pid', $this->path), |
|
59
|
|
|
] |
|
60
|
1 |
|
) |
|
61
|
1 |
|
); |
|
62
|
|
|
|
|
63
|
1 |
|
$sections->addSection( |
|
64
|
1 |
|
$this->sectionFactory->createRpcInterface( |
|
65
|
1 |
|
'supervisor', |
|
66
|
|
|
[ |
|
67
|
1 |
|
'supervisor.rpcinterface_factory' => 'supervisor.rpcinterface:make_main_rpcinterface', |
|
68
|
|
|
] |
|
69
|
1 |
|
) |
|
70
|
1 |
|
); |
|
71
|
|
|
|
|
72
|
1 |
|
$sections->addSection( |
|
73
|
1 |
|
$this->sectionFactory->createSupervisorctl( |
|
74
|
|
|
[ |
|
75
|
1 |
|
'serverurl' => sprintf('unix://%s/supervisord.sock', $this->path), |
|
76
|
|
|
] |
|
77
|
1 |
|
) |
|
78
|
1 |
|
); |
|
79
|
|
|
|
|
80
|
1 |
|
return $sections; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* @param array $properties |
|
86
|
|
|
* |
|
87
|
|
|
* @return Program |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function generateProgram($name, array $properties = []) |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->sectionFactory->createProgram($name, $properties); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $consumer |
|
96
|
|
|
* @param null|string $route |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function getBundleProperties(array $consumer, $route = null) |
|
101
|
|
|
{ |
|
102
|
2 |
|
$processBuilder = $this->processBuilderFactory->create(); |
|
103
|
2 |
|
$processBuilder->setPrefix(['php']); |
|
104
|
2 |
|
$processBuilder->add($consumer['command']['console']); |
|
105
|
|
|
|
|
106
|
2 |
|
foreach ($consumer['command']['arguments'] as $argument) { |
|
107
|
2 |
|
$processBuilder->add($argument); |
|
108
|
2 |
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
$processBuilder->add(sprintf('--messages=%s', $consumer['messages'])); |
|
111
|
|
|
|
|
112
|
2 |
|
if (null !== $route) { |
|
113
|
1 |
|
$processBuilder->add(sprintf('--route=%s', $route)); |
|
114
|
1 |
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
$processBuilder->add($consumer['command']['command']); |
|
117
|
2 |
|
$processBuilder->add($consumer['name']); |
|
118
|
2 |
|
$process = $processBuilder->getProcess(); |
|
119
|
|
|
|
|
120
|
|
|
return [ |
|
121
|
2 |
|
'command' => $process->getCommandLine(), |
|
122
|
2 |
|
'process_name' => '%(program_name)s%(process_num)02d', |
|
123
|
2 |
|
'numprocs' => $consumer['supervisor']['count'], |
|
124
|
2 |
|
'startsecs' => $consumer['supervisor']['startsecs'], |
|
125
|
2 |
|
'autorestart' => $consumer['supervisor']['autorestart'], |
|
126
|
2 |
|
'stopsignal' => $consumer['supervisor']['stopsignal'], |
|
127
|
2 |
|
'stopwaitsecs' => $consumer['supervisor']['stopwaitsecs'], |
|
128
|
2 |
|
]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param array $consumer |
|
133
|
|
|
* @param string $consumerConfiguration |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function getConsumerProperties(array $consumer, $consumerConfiguration) |
|
138
|
|
|
{ |
|
139
|
1 |
|
$processBuilder = $this->processBuilderFactory->create(); |
|
140
|
1 |
|
$processBuilder->setPrefix(['php']); |
|
141
|
1 |
|
$processBuilder->add($consumer['command']['console']); |
|
142
|
|
|
|
|
143
|
1 |
|
foreach ($consumer['command']['arguments'] as $argument) { |
|
144
|
1 |
|
$processBuilder->add($argument); |
|
145
|
1 |
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
$processBuilder->add($consumer['command']['command']); |
|
148
|
1 |
|
$processBuilder->add($consumer['name']); |
|
149
|
|
|
|
|
150
|
1 |
|
$consumerProcessBuilder = $this->processBuilderFactory->create(); |
|
151
|
1 |
|
$consumerProcessBuilder->setPrefix(['rabbitmq-cli-consumer']); |
|
152
|
1 |
|
$consumerProcessBuilder->add('--strict-exit-code'); |
|
153
|
1 |
|
$consumerProcessBuilder->add('--include'); |
|
154
|
1 |
|
$consumerProcessBuilder->add(sprintf('--configuration=%s', $consumerConfiguration)); |
|
155
|
1 |
|
$consumerProcessBuilder->add(sprintf('--executable=%s', $processBuilder->getProcess()->getCommandLine())); |
|
156
|
|
|
|
|
157
|
1 |
|
$process = $consumerProcessBuilder->getProcess(); |
|
158
|
|
|
|
|
159
|
|
|
return [ |
|
160
|
1 |
|
'command' => $process->getCommandLine(), |
|
161
|
1 |
|
'process_name' => '%(program_name)s%(process_num)02d', |
|
162
|
1 |
|
'numprocs' => $consumer['supervisor']['count'], |
|
163
|
1 |
|
'startsecs' => $consumer['supervisor']['startsecs'], |
|
164
|
1 |
|
'autorestart' => $consumer['supervisor']['autorestart'], |
|
165
|
1 |
|
'stopsignal' => $consumer['supervisor']['stopsignal'], |
|
166
|
1 |
|
'stopwaitsecs' => $consumer['supervisor']['stopwaitsecs'], |
|
167
|
1 |
|
]; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|