refactor(service-management): enhance container stopping logic by implementing parallel processing and removing deprecated methods
This commit is contained in:
parent
aa0e32a20d
commit
200b5cd4fb
3 changed files with 45 additions and 33 deletions
|
|
@ -21,8 +21,19 @@ public function handle(Service $service, bool $isDeleteOperation = false, bool $
|
||||||
return 'Server is not functional';
|
return 'Server is not functional';
|
||||||
}
|
}
|
||||||
|
|
||||||
$containersToStop = $service->getContainersToStop();
|
$containersToStop = [];
|
||||||
$service->stopContainers($containersToStop, $server);
|
$applications = $service->applications()->get();
|
||||||
|
foreach ($applications as $application) {
|
||||||
|
$containersToStop[] = "{$application->name}-{$service->uuid}";
|
||||||
|
}
|
||||||
|
$dbs = $service->databases()->get();
|
||||||
|
foreach ($dbs as $db) {
|
||||||
|
$containersToStop[] = "{$db->name}-{$service->uuid}";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($containersToStop)) {
|
||||||
|
$this->stopContainersInParallel($containersToStop, $server);
|
||||||
|
}
|
||||||
|
|
||||||
if ($isDeleteOperation) {
|
if ($isDeleteOperation) {
|
||||||
$service->deleteConnectedNetworks();
|
$service->deleteConnectedNetworks();
|
||||||
|
|
@ -36,4 +47,18 @@ public function handle(Service $service, bool $isDeleteOperation = false, bool $
|
||||||
ServiceStatusChanged::dispatch($service->environment->project->team->id);
|
ServiceStatusChanged::dispatch($service->environment->project->team->id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function stopContainersInParallel(array $containersToStop, $server): void
|
||||||
|
{
|
||||||
|
$timeout = count($containersToStop) > 5 ? 10 : 30;
|
||||||
|
$commands = [];
|
||||||
|
$containerList = implode(' ', $containersToStop);
|
||||||
|
$commands[] = "docker stop --time=$timeout $containerList";
|
||||||
|
$commands[] = "docker rm -f $containerList";
|
||||||
|
instant_remote_process(
|
||||||
|
command: $commands,
|
||||||
|
server: $server,
|
||||||
|
throwError: false
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -239,12 +239,24 @@ public function delete(int $pull_request_id)
|
||||||
|
|
||||||
private function stopContainers(array $containers, $server, int $timeout = 30)
|
private function stopContainers(array $containers, $server, int $timeout = 30)
|
||||||
{
|
{
|
||||||
foreach ($containers as $container) {
|
if (empty($containers)) {
|
||||||
$containerName = str_replace('/', '', $container['Names']);
|
return;
|
||||||
instant_remote_process(command: [
|
|
||||||
"docker stop --time=$timeout $containerName",
|
|
||||||
"docker rm -f $containerName",
|
|
||||||
], server: $server, throwError: false);
|
|
||||||
}
|
}
|
||||||
|
$containerNames = [];
|
||||||
|
foreach ($containers as $container) {
|
||||||
|
$containerNames[] = str_replace('/', '', $container['Names']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$containerList = implode(' ', $containerNames);
|
||||||
|
$commands = [
|
||||||
|
"docker stop --time=$timeout $containerList",
|
||||||
|
"docker rm -f $containerList",
|
||||||
|
];
|
||||||
|
|
||||||
|
instant_remote_process(
|
||||||
|
command: $commands,
|
||||||
|
server: $server,
|
||||||
|
throwError: false
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -141,31 +141,6 @@ public static function ownedByCurrentTeam()
|
||||||
return Service::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
|
return Service::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getContainersToStop(): array
|
|
||||||
{
|
|
||||||
$containersToStop = [];
|
|
||||||
$applications = $this->applications()->get();
|
|
||||||
foreach ($applications as $application) {
|
|
||||||
$containersToStop[] = "{$application->name}-{$this->uuid}";
|
|
||||||
}
|
|
||||||
$dbs = $this->databases()->get();
|
|
||||||
foreach ($dbs as $db) {
|
|
||||||
$containersToStop[] = "{$db->name}-{$this->uuid}";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $containersToStop;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stopContainers(array $containerNames, $server, int $timeout = 30)
|
|
||||||
{
|
|
||||||
foreach ($containerNames as $containerName) {
|
|
||||||
instant_remote_process(command: [
|
|
||||||
"docker stop --time=$timeout $containerName",
|
|
||||||
"docker rm -f $containerName",
|
|
||||||
], server: $server, throwError: false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function deleteConfigurations()
|
public function deleteConfigurations()
|
||||||
{
|
{
|
||||||
$server = data_get($this, 'destination.server');
|
$server = data_get($this, 'destination.server');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue