1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\Supervisor; |
4
|
|
|
|
5
|
|
|
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Exception\Supervisor\SupervisorAlreadyRunningException; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
|
8
|
|
|
class Supervisor implements SupervisorInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $path; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $config |
17
|
|
|
*/ |
18
|
8 |
|
public function __construct(array $config) |
19
|
|
|
{ |
20
|
8 |
|
$this->path = $config['path']; |
21
|
8 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
6 |
|
public function isRunning() |
27
|
|
|
{ |
28
|
6 |
|
$result = $this->execute('status')->getOutput(); |
29
|
|
|
|
30
|
6 |
|
return !(false !== strpos($result, 'sock no such file') || false !== strpos($result, 'refused connection')); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function start() |
37
|
|
|
{ |
38
|
|
|
if ($this->isRunning()) { |
39
|
|
|
throw new SupervisorAlreadyRunningException('supervisor is already running.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$process = new Process( |
43
|
|
|
sprintf( |
44
|
|
|
'supervisord%1$s%2$s', |
45
|
|
|
sprintf(' --configuration=%s/%s', $this->path, 'supervisord.conf'), |
46
|
|
|
sprintf(' --identifier=%s', sha1($this->path)) |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$process->setWorkingDirectory($this->path); |
51
|
|
|
$process->run(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
2 |
|
public function stop() |
58
|
|
|
{ |
59
|
2 |
|
if (!$this->isRunning()) { |
60
|
1 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$this->execute('shutdown'); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
2 |
|
public function reload() |
70
|
|
|
{ |
71
|
2 |
|
if (!$this->isRunning()) { |
72
|
1 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$this->execute('reread'); |
76
|
1 |
|
$this->execute('reload'); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
1 |
|
public function restart() |
83
|
|
|
{ |
84
|
1 |
|
$this->execute('restart'); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
1 |
|
public function getProcessId() { |
91
|
|
|
|
92
|
1 |
|
return (int) $this->execute('pid')->getOutput(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $cmd supervisorctl command |
97
|
|
|
* |
98
|
|
|
* @return Process |
99
|
|
|
*/ |
100
|
|
|
public function execute($cmd) |
101
|
|
|
{ |
102
|
|
|
$process = new Process( |
103
|
|
|
sprintf( |
104
|
|
|
'supervisorctl%1$s %2$s', |
105
|
|
|
sprintf(' --configuration=%s/%s', $this->path, 'supervisord.conf'), |
106
|
|
|
$cmd |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$process->setWorkingDirectory($this->path); |
111
|
|
|
$process->run(); |
112
|
|
|
$process->wait(); |
113
|
|
|
|
114
|
|
|
return $process; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|