refactor(application): enhance application stopping logic to support multiple servers
This commit is contained in:
		@@ -15,9 +15,12 @@ class StopApplication
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function handle(Application $application, bool $previewDeployments = false, bool $dockerCleanup = true)
 | 
					    public function handle(Application $application, bool $previewDeployments = false, bool $dockerCleanup = true)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        ray('StopApplication');
 | 
					        $servers = collect([$application->destination->server]);
 | 
				
			||||||
 | 
					        if ($application?->additional_servers?->count() > 0) {
 | 
				
			||||||
 | 
					            $servers = $servers->merge($application->additional_servers);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        foreach ($servers as $server) {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
            $server = $application->destination->server;
 | 
					 | 
				
			||||||
                if (! $server->isFunctional()) {
 | 
					                if (! $server->isFunctional()) {
 | 
				
			||||||
                    return 'Server is not functional';
 | 
					                    return 'Server is not functional';
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
@@ -28,8 +31,18 @@ class StopApplication
 | 
				
			|||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $containersToStop = $application->getContainersToStop($previewDeployments);
 | 
					                $containers = $previewDeployments
 | 
				
			||||||
            $application->stopContainers($containersToStop, $server);
 | 
					                    ? getCurrentApplicationContainerStatus($server, $application->id, includePullrequests: true)
 | 
				
			||||||
 | 
					                    : getCurrentApplicationContainerStatus($server, $application->id, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $containersToStop = $containers->pluck('Names')->toArray();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                foreach ($containersToStop as $containerName) {
 | 
				
			||||||
 | 
					                    instant_remote_process(command: [
 | 
				
			||||||
 | 
					                        "docker stop --time=30 $containerName",
 | 
				
			||||||
 | 
					                        "docker rm -f $containerName",
 | 
				
			||||||
 | 
					                    ], server: $server, throwError: false);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if ($application->build_pack === 'dockercompose') {
 | 
					                if ($application->build_pack === 'dockercompose') {
 | 
				
			||||||
                    $application->deleteConnectedNetworks();
 | 
					                    $application->deleteConnectedNetworks();
 | 
				
			||||||
@@ -40,8 +53,8 @@ class StopApplication
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            } catch (\Exception $e) {
 | 
					            } catch (\Exception $e) {
 | 
				
			||||||
                return $e->getMessage();
 | 
					                return $e->getMessage();
 | 
				
			||||||
        } finally {
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        ServiceStatusChanged::dispatch($application->environment->project->team->id);
 | 
					        ServiceStatusChanged::dispatch($application->environment->project->team->id);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,7 +25,10 @@ class StopApplicationOneServer
 | 
				
			|||||||
                    $containerName = data_get($container, 'Names');
 | 
					                    $containerName = data_get($container, 'Names');
 | 
				
			||||||
                    if ($containerName) {
 | 
					                    if ($containerName) {
 | 
				
			||||||
                        instant_remote_process(
 | 
					                        instant_remote_process(
 | 
				
			||||||
                            ["docker rm -f {$containerName}"],
 | 
					                            [
 | 
				
			||||||
 | 
					                                "docker stop --time=30 $containerName",
 | 
				
			||||||
 | 
					                                "docker rm -f $containerName",
 | 
				
			||||||
 | 
					                            ],
 | 
				
			||||||
                            $server
 | 
					                            $server
 | 
				
			||||||
                        );
 | 
					                        );
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,6 +23,7 @@ class Configuration extends Component
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return [
 | 
					        return [
 | 
				
			||||||
            "echo-private:team.{$teamId},ServiceChecked" => '$refresh',
 | 
					            "echo-private:team.{$teamId},ServiceChecked" => '$refresh',
 | 
				
			||||||
 | 
					            "echo-private:team.{$teamId},ServiceStatusChanged" => '$refresh',
 | 
				
			||||||
            'buildPackUpdated' => '$refresh',
 | 
					            'buildPackUpdated' => '$refresh',
 | 
				
			||||||
            'refresh' => '$refresh',
 | 
					            'refresh' => '$refresh',
 | 
				
			||||||
        ];
 | 
					        ];
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,6 +26,7 @@ class Destination extends Component
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return [
 | 
					        return [
 | 
				
			||||||
            "echo-private:team.{$teamId},ApplicationStatusChanged" => 'loadData',
 | 
					            "echo-private:team.{$teamId},ApplicationStatusChanged" => 'loadData',
 | 
				
			||||||
 | 
					            "echo-private:team.{$teamId},ServiceStatusChanged" => 'mount',
 | 
				
			||||||
            'refresh' => 'mount',
 | 
					            'refresh' => 'mount',
 | 
				
			||||||
        ];
 | 
					        ];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -259,25 +259,15 @@ class Application extends BaseModel
 | 
				
			|||||||
        return Application::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
 | 
					        return Application::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getContainersToStop(bool $previewDeployments = false): array
 | 
					    public function getContainersToStop(Server $server, bool $previewDeployments = false): array
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $containers = $previewDeployments
 | 
					        $containers = $previewDeployments
 | 
				
			||||||
            ? getCurrentApplicationContainerStatus($this->destination->server, $this->id, includePullrequests: true)
 | 
					            ? getCurrentApplicationContainerStatus($server, $this->id, includePullrequests: true)
 | 
				
			||||||
            : getCurrentApplicationContainerStatus($this->destination->server, $this->id, 0);
 | 
					            : getCurrentApplicationContainerStatus($server, $this->id, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $containers->pluck('Names')->toArray();
 | 
					        return $containers->pluck('Names')->toArray();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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