Skip to content

Commit 5cf0aba

Browse files
Merge pull request #4 from netlogix/feature/NEOS-49-supervisorctl-groupactions
Add Start, Stop and Update Actions for supervisorctl @related: NEOS-49
2 parents f3f7fd2 + 791d76a commit 5cf0aba

3 files changed

Lines changed: 91 additions & 24 deletions

File tree

Classes/Command/SupervisorCommandController.php

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,17 @@ class SupervisorCommandController extends CommandController
2424
*/
2525
protected $providerClassNames = [];
2626

27+
/**
28+
* Creates supervisor configuration
29+
*/
2730
public function createCommand(): void
2831
{
2932
Files::createDirectoryRecursively(self::CONFIG_PATH);
3033
Files::emptyDirectoryRecursively(self::CONFIG_PATH);
3134

32-
$factory = new Model\Factory();
33-
34-
foreach ($this->getProviders() as $provider) {
35-
assert($provider instanceof Provider);
36-
foreach ($provider->getPrograms() as $program) {
37-
assert($program instanceof Model\Program);
38-
$factory->registerProgram($program);
39-
}
40-
}
41-
4235
$renderer = new Renderer\Renderer();
4336

44-
foreach ($factory->getGroups() as $group) {
37+
foreach ($this->getGroups() as $group) {
4538

4639
assert($group instanceof Model\Group);
4740
file_put_contents(
@@ -56,7 +49,74 @@ public function createCommand(): void
5649
);
5750
}
5851
}
52+
}
5953

54+
/**
55+
* Starts all programs of configured groups
56+
*/
57+
public function startGroupsCommand(): void
58+
{
59+
$this->runSupervisorCommand('start', ...$this->getGroups());
60+
}
61+
62+
/**
63+
* Stops all programs of configured groups
64+
*/
65+
public function stopGroupsCommand(): void
66+
{
67+
$this->runSupervisorCommand('stop', ...$this->getGroups());
68+
}
69+
70+
/**
71+
* Reloads config and starts/stops programs accordingly
72+
*/
73+
public function updateGroupsCommand(): void
74+
{
75+
$this->runSupervisorCommand('update', ...$this->getGroups());
76+
}
77+
78+
protected function runSupervisorCommand(string $action, Model\Group ...$groups): bool
79+
{
80+
$output = [];
81+
$groupArgs = implode(' ', array_map(function($i){return $i->getName();}, $groups));
82+
$command = sprintf('supervisorctl %s %s%s 2>&1',escapeshellarg($action), escapeshellarg($groupArgs), $action !== 'update' ? ':' : '');
83+
84+
exec($command, $output, $result);
85+
$output = implode("\n", $output);
86+
if ($result !== 0) {
87+
if (!empty($output)) {
88+
$exceptionMessage = $output;
89+
} else {
90+
$exceptionMessage = sprintf('Execution of supervisorctl failed with exit code %d without any further output.', $result);
91+
}
92+
$this->outputLine($exceptionMessage);
93+
}
94+
95+
return $result === 0;
96+
}
97+
98+
protected function bootstrapFactory(): Model\Factory
99+
{
100+
$factory = new Model\Factory();
101+
102+
foreach ($this->getProviders() as $provider) {
103+
assert($provider instanceof Provider);
104+
foreach ($provider->getPrograms() as $program) {
105+
assert($program instanceof Model\Program);
106+
$factory->registerProgram($program);
107+
}
108+
}
109+
110+
return $factory;
111+
}
112+
113+
/**
114+
* @return array<Model\Group>
115+
*/
116+
protected function getGroups(): array
117+
{
118+
$factory = $this->bootstrapFactory();
119+
return $factory->getGroups();
60120
}
61121

62122
/**

Classes/ConfigurationBased/Provider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function initialize(): void
5757

5858
unset($programTemplate['name']);
5959
unset($programTemplate['command']);
60-
unset($programTemplate['group']);
60+
unset($programTemplate['groupName']);
6161

6262
$groups[$groupName] = $groups[$groupName] ?? [];
6363
$groups[$groupName][$name] = $name;

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Netlogix.Supervisor
33

44
This package creates supervisor configuration files via flow CLI.
55

6-
Configuration files are named "group-something.conf" when they contain a
7-
supervisor group configuration or "program-something.conf" when they contain
8-
a supervisor program configuration. Both are created at
6+
Configuration files are named "group-something.conf" when they contain a
7+
supervisor group configuration or "program-something.conf" when they contain
8+
a supervisor program configuration. Both are created at
99
Configuration/Supervisor within your application.
1010

1111

@@ -20,19 +20,26 @@ Supervisor allows to "include" other files and even allows for globbing.
2020
files=/etc/supervisor/conf.d/*.conf /var/www/*/Configuration/Supervisor/group.conf /var/www/*/Configuration/Supervisor/program*.conf
2121
```
2222

23-
Creating supervisor files dynamically is intended to be a compile time step,
23+
Creating supervisor files dynamically is intended to be a compile time step,
2424
preferably on deployment.
2525

2626

2727
Available configuration options:
2828
--------------------------------
2929

30-
Program settings get passed to the supervisor configuration file without
30+
Program settings get passed to the supervisor configuration file without
3131
validation, so every current and future configuration option is available.
3232

3333
See: http://supervisord.org/configuration.html#program-x-section-settings
3434

35-
35+
Available flow commands:
36+
-----------------------
37+
```
38+
./flow supervisor:create Creates supervisor configuration
39+
./flow supervisor:startgroups Starts all programs of configured groups
40+
./flow supervisor:stopgroups Stops all programs of configured groups
41+
./flow supervisor:updategroups Reloads config and starts/stops programs accordingly
42+
```
3643
Create supervisor settings via YAML:
3744
------------------------------------
3845

@@ -97,14 +104,14 @@ class Provider implements \Netlogix\Supervisor\Provider
97104
Configure group settings:
98105
-------------------------
99106

100-
Groups are created on demand as soon as a program assigns itself to the
107+
Groups are created on demand as soon as a program assigns itself to the
101108
group. This means groups don't need to be configured beforehand or even at all.
102109

103-
If no further configuration is presented, the supervisor group name is
110+
If no further configuration is presented, the supervisor group name is
104111
generated from the "groupName" configuration option of a program.
105112

106-
Groups can override their names. This menas several programs can e.g. refer
107-
to the same group "default" but the group can be renamed if different
113+
Groups can override their names. This menas several programs can e.g. refer
114+
to the same group "default" but the group can be renamed if different
108115
applications share a common supervisor daemon.
109116

110117
This setting also provides a priority setting.
@@ -123,7 +130,7 @@ Netlogix:
123130
Renamed identifiers:
124131
--------------------
125132

126-
Both, group names and program names need to be cleaned to comply with
133+
Both, group names and program names need to be cleaned to comply with
127134
supervisors naming scheme. This is done automatically, so even if the
128-
group name is configured as `%FLOW_PATH_ROOT%` it will be transformed to
135+
group name is configured as `%FLOW_PATH_ROOT%` it will be transformed to
129136
`var-www-document-root`.

0 commit comments

Comments
 (0)