1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Services; |
4
|
|
|
|
5
|
|
|
use Gaufrette\Filesystem; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
|
8
|
|
|
class SupervisorConfigurationGenerator |
9
|
|
|
{ |
10
|
|
|
private |
11
|
|
|
$overwrite, |
12
|
|
|
$filesystem, |
13
|
|
|
$commandGenerator; |
14
|
|
|
|
15
|
5 |
|
public function __construct(Filesystem $filesystem, CommandGenerator $commandGenerator) |
16
|
|
|
{ |
17
|
5 |
|
$this->filesystem = $filesystem; |
18
|
5 |
|
$this->commandGenerator = $commandGenerator; |
19
|
5 |
|
$this->overwrite = true; |
20
|
5 |
|
} |
21
|
|
|
|
22
|
5 |
|
public function generate(array $workers, $autostart, $autorestart, $appId, $destination, OutputInterface $output) |
23
|
|
|
{ |
24
|
5 |
|
foreach($workers as $worker => $data) |
25
|
|
|
{ |
26
|
5 |
|
$this->generateSupervisorConfigurationFile($worker, $autostart, $autorestart, $appId, $destination, $output); |
27
|
|
|
} |
28
|
5 |
|
} |
29
|
|
|
|
30
|
5 |
|
private function generateSupervisorConfigurationFile($worker, $autostart, $autorestart, $appId, $destination, OutputInterface $output) |
31
|
|
|
{ |
32
|
5 |
|
$configuration = $this->generateSupervisorConfiguration($worker, $autostart, $autorestart, $appId); |
33
|
5 |
|
$filename = $this->buildFilename($appId, $worker); |
34
|
|
|
|
35
|
5 |
|
$this->filesystem->write($filename, $configuration, $this->overwrite); |
36
|
|
|
|
37
|
5 |
|
$message = sprintf('%s', $destination . $filename); |
38
|
5 |
|
if($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) |
39
|
|
|
{ |
40
|
|
|
$message = sprintf("<comment>%s%s</comment>\n%s\n", $destination, $filename, $configuration); |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
$output->writeln($message); |
44
|
5 |
|
} |
45
|
|
|
|
46
|
5 |
|
private function buildFilename($appId, $workerName) |
47
|
|
|
{ |
48
|
5 |
|
return sprintf('%s--%s.conf', $appId, $workerName); |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
private function generateSupervisorConfiguration($worker, $autostart, $autorestart, $appId) |
52
|
|
|
{ |
53
|
5 |
|
$program = sprintf('%s--%s', $appId, $worker); |
54
|
5 |
|
$command = $this->commandGenerator->generate($worker); |
55
|
5 |
|
$autostart = $this->booleanToString($autostart); |
56
|
5 |
|
$autorestart = $this->booleanToString($autorestart); |
57
|
|
|
|
58
|
|
|
return <<<TXT |
59
|
5 |
|
[program:$program] |
60
|
5 |
|
command=$command |
61
|
|
|
directory=/var/www/app |
62
|
|
|
user=www-data |
63
|
5 |
|
autostart=$autostart |
64
|
5 |
|
autorestart=$autorestart |
65
|
|
|
TXT; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
private function booleanToString($value) |
69
|
|
|
{ |
70
|
5 |
|
return $value === true ? 'true' : 'false'; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|