Completed
Push — master ( 7d482b...96809c )
by Danny
03:09
created

Supervisor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 109
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isRunning() 0 6 2
A start() 0 17 2
A stop() 0 8 2
A reload() 0 9 2
A restart() 0 4 1
A getProcessId() 0 4 1
A execute() 0 16 1
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
    public function __construct(array $config)
19
    {
20
        $this->path = $config['path'];
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function isRunning()
27
    {
28
        $result = $this->execute('status')->getOutput();
29
30
        return !(strpos($result, 'sock no such file') || 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
    public function stop()
58
    {
59
        if (!$this->isRunning()) {
60
            return;
61
        }
62
63
        $this->execute('shutdown');
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function reload()
70
    {
71
        if (!$this->isRunning()) {
72
            return;
73
        }
74
75
        $this->execute('reread');
76
        $this->execute('reload');
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function restart()
83
    {
84
        $this->execute('restart');
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function getProcessId() {
91
92
        return (int) $this->execute('pid');
93
    }
94
95
    /**
96
     * @param string $cmd supervisorctl command
97
     *
98
     * @return Process
99
     */
100
    private 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