refactor(service-management): enhance container stopping logic by implementing parallel processing and removing deprecated methods
This commit is contained in:
@@ -21,8 +21,19 @@ class StopService
|
|||||||
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 @@ class StopService
|
|||||||
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 @@ class Previews extends Component
|
|||||||
|
|
||||||
private function stopContainers(array $containers, $server, int $timeout = 30)
|
private function stopContainers(array $containers, $server, int $timeout = 30)
|
||||||
{
|
{
|
||||||
|
if (empty($containers)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$containerNames = [];
|
||||||
foreach ($containers as $container) {
|
foreach ($containers as $container) {
|
||||||
$containerName = str_replace('/', '', $container['Names']);
|
$containerNames[] = str_replace('/', '', $container['Names']);
|
||||||
instant_remote_process(command: [
|
}
|
||||||
"docker stop --time=$timeout $containerName",
|
|
||||||
"docker rm -f $containerName",
|
$containerList = implode(' ', $containerNames);
|
||||||
], server: $server, throwError: false);
|
$commands = [
|
||||||
}
|
"docker stop --time=$timeout $containerList",
|
||||||
|
"docker rm -f $containerList",
|
||||||
|
];
|
||||||
|
|
||||||
|
instant_remote_process(
|
||||||
|
command: $commands,
|
||||||
|
server: $server,
|
||||||
|
throwError: false
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,31 +141,6 @@ class Service extends BaseModel
|
|||||||
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');
|
||||||
|
|||||||
Reference in New Issue
Block a user