|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ivan1986\SupervisorBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\TwigBundle\TwigEngine; |
|
6
|
|
|
use Symfony\Component\Process\Process; |
|
7
|
|
|
|
|
8
|
|
|
class Supervisor |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var TwigEngine */ |
|
11
|
|
|
protected $templating; |
|
12
|
|
|
private $appDir; |
|
13
|
|
|
private $name; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($templating, $appDir, $name) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->templating = $templating; |
|
18
|
|
|
$this->appDir = $appDir; |
|
19
|
|
|
$this->name = $name ? (' -i '.$name) : ''; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Сгенерировать конфиг для программы |
|
24
|
|
|
* |
|
25
|
|
|
* @param $fileName string file in app/supervisor dir |
|
26
|
|
|
* @param $vars array array(name,command,[numprocs]) |
|
27
|
|
|
* @param bool $template string non default template |
|
28
|
|
|
*/ |
|
29
|
|
|
public function genProgrammConf($fileName, $vars, $template = false) |
|
30
|
|
|
{ |
|
31
|
|
|
$template = $template?:'@Supervisor/programm.conf.twig'; |
|
32
|
|
|
$content = $this->templating->render($template, $vars); |
|
|
|
|
|
|
33
|
|
|
file_put_contents($this->appDir.'/supervisor/'.$fileName.'.conf', $content); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Выполняет команду супервизора |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $cmd string supervisorctl command |
|
40
|
|
|
* @return Process Завершенный процесс |
|
41
|
|
|
*/ |
|
42
|
|
|
public function execute($cmd) |
|
43
|
|
|
{ |
|
44
|
|
|
$p = new Process("supervisorctl ".$cmd); |
|
45
|
|
|
$p->setWorkingDirectory($this->appDir); |
|
46
|
|
|
$p->run(); |
|
47
|
|
|
$p->wait(); |
|
48
|
|
|
return $p; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Перечитать конфиг и перезапустить процессы |
|
53
|
|
|
*/ |
|
54
|
|
|
public function reloadAndUpdate() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->execute('reread'); |
|
57
|
|
|
$this->execute('update'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Запустить демона, если он не работает |
|
62
|
|
|
*/ |
|
63
|
|
|
public function run() |
|
64
|
|
|
{ |
|
65
|
|
|
$result = $this->execute('status')->getOutput(); |
|
66
|
|
|
if (strpos($result, 'sock no such file') || strpos($result, 'refused connection')) { |
|
67
|
|
|
$p = new Process('supervisord'.$this->name); |
|
68
|
|
|
$p->setWorkingDirectory($this->appDir); |
|
69
|
|
|
$p->run(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.