diff --git a/app/Actions/Service/StopService.php b/app/Actions/Service/StopService.php index 9638db57f..4debb63c4 100644 --- a/app/Actions/Service/StopService.php +++ b/app/Actions/Service/StopService.php @@ -21,8 +21,19 @@ class StopService return 'Server is not functional'; } - $containersToStop = $service->getContainersToStop(); - $service->stopContainers($containersToStop, $server); + $containersToStop = []; + $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) { $service->deleteConnectedNetworks(); @@ -36,4 +47,18 @@ class StopService 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 + ); + } } diff --git a/app/Livewire/Project/Application/Previews.php b/app/Livewire/Project/Application/Previews.php index e89ba3257..47a588554 100644 --- a/app/Livewire/Project/Application/Previews.php +++ b/app/Livewire/Project/Application/Previews.php @@ -239,12 +239,24 @@ class Previews extends Component private function stopContainers(array $containers, $server, int $timeout = 30) { - foreach ($containers as $container) { - $containerName = str_replace('/', '', $container['Names']); - instant_remote_process(command: [ - "docker stop --time=$timeout $containerName", - "docker rm -f $containerName", - ], server: $server, throwError: false); + if (empty($containers)) { + return; } + $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 + ); } } diff --git a/app/Models/Service.php b/app/Models/Service.php index 13e3a89c0..c3c8f3215 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -141,31 +141,6 @@ class Service extends BaseModel 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() { $server = data_get($this, 'destination.server');