Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor(service-management): enhance container stopping logic by implementing parallel processing and removing deprecated methods

Andras Bacsai200b5cd4fbca
diff --git a/app/Actions/Service/StopService.php b/app/Actions/Service/StopService.php
index 9638db57f5..4debb63c47 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 e89ba3257e..47a588554b 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)
{
+ if (empty($containers)) {
+ return;
+ }
+ $containerNames = [];
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);
+ $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 13e3a89c0e..c3c8f32153 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');