use public function
This commit is contained in:
@@ -12,41 +12,28 @@ class StopApplication
|
|||||||
|
|
||||||
public function handle(Application $application, bool $previewDeployments = false)
|
public function handle(Application $application, bool $previewDeployments = false)
|
||||||
{
|
{
|
||||||
if ($application->destination->server->isSwarm()) {
|
try {
|
||||||
instant_remote_process(["docker stack rm {$application->uuid}"], $application->destination->server);
|
$server = $application->destination->server;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$servers = collect([]);
|
|
||||||
$servers->push($application->destination->server);
|
|
||||||
$application->additional_servers->map(function ($server) use ($servers) {
|
|
||||||
$servers->push($server);
|
|
||||||
});
|
|
||||||
foreach ($servers as $server) {
|
|
||||||
if (!$server->isFunctional()) {
|
if (!$server->isFunctional()) {
|
||||||
return 'Server is not functional';
|
return 'Server is not functional';
|
||||||
}
|
}
|
||||||
if ($previewDeployments) {
|
ray('Stopping application: ' . $application->name);
|
||||||
$containers = getCurrentApplicationContainerStatus($server, $application->id, includePullrequests: true);
|
|
||||||
} else {
|
|
||||||
$containers = getCurrentApplicationContainerStatus($server, $application->id, 0);
|
|
||||||
}
|
|
||||||
if ($containers->count() > 0) {
|
|
||||||
foreach ($containers as $container) {
|
|
||||||
$containerName = data_get($container, 'Names');
|
|
||||||
if ($containerName) {
|
|
||||||
instant_remote_process(command: ["docker stop --time=30 $containerName"], server: $server, throwError: false);
|
|
||||||
instant_remote_process(command: ["docker rm $containerName"], server: $server, throwError: false);
|
|
||||||
instant_remote_process(command: ["docker rm -f {$containerName}"], server: $server, throwError: false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($application->build_pack === 'dockercompose') {
|
|
||||||
$uuid = $application->uuid;
|
|
||||||
$application->delete_connected_networks($uuid);
|
|
||||||
|
|
||||||
|
if ($server->isSwarm()) {
|
||||||
|
instant_remote_process(["docker stack rm {$application->uuid}"], $server);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$containersToStop = $application->getContainersToStop($previewDeployments);
|
||||||
|
$application->stopContainers($containersToStop, $server);
|
||||||
|
|
||||||
|
if ($application->build_pack === 'dockercompose') {
|
||||||
|
$application->delete_connected_networks($application->uuid);
|
||||||
CleanupDocker::run($server, true);
|
CleanupDocker::run($server, true);
|
||||||
}
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
ray($e->getMessage());
|
||||||
|
return $e->getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ namespace App\Actions\Service;
|
|||||||
use App\Models\Service;
|
use App\Models\Service;
|
||||||
use App\Actions\Server\CleanupDocker;
|
use App\Actions\Server\CleanupDocker;
|
||||||
use Lorisleiva\Actions\Concerns\AsAction;
|
use Lorisleiva\Actions\Concerns\AsAction;
|
||||||
use Illuminate\Support\Facades\Process;
|
|
||||||
use Illuminate\Process\InvokedProcess;
|
|
||||||
|
|
||||||
class StopService
|
class StopService
|
||||||
{
|
{
|
||||||
@@ -21,9 +19,8 @@ class StopService
|
|||||||
}
|
}
|
||||||
ray('Stopping service: ' . $service->name);
|
ray('Stopping service: ' . $service->name);
|
||||||
|
|
||||||
$containersToStop = $this->getContainersToStop($service);
|
$containersToStop = $service->getContainersToStop();
|
||||||
|
$service->stopContainers($containersToStop, $server);
|
||||||
$this->stopContainers($containersToStop, $server);
|
|
||||||
|
|
||||||
if (!$isDeleteOperation) {
|
if (!$isDeleteOperation) {
|
||||||
$service->delete_connected_networks($service->uuid);
|
$service->delete_connected_networks($service->uuid);
|
||||||
@@ -34,62 +31,4 @@ class StopService
|
|||||||
return $e->getMessage();
|
return $e->getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getContainersToStop(Service $service): array
|
|
||||||
{
|
|
||||||
$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}";
|
|
||||||
}
|
|
||||||
return $containersToStop;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function stopContainers(array $containerNames, $server, int $timeout = 300)
|
|
||||||
{
|
|
||||||
$processes = [];
|
|
||||||
foreach ($containerNames as $containerName) {
|
|
||||||
$processes[$containerName] = $this->stopContainer($containerName, $server, $timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
$startTime = time();
|
|
||||||
while (count($processes) > 0) {
|
|
||||||
$finishedProcesses = array_filter($processes, function ($process) {
|
|
||||||
return !$process->running();
|
|
||||||
});
|
|
||||||
foreach ($finishedProcesses as $containerName => $process) {
|
|
||||||
unset($processes[$containerName]);
|
|
||||||
$this->removeContainer($containerName, $server);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (time() - $startTime >= $timeout) {
|
|
||||||
$this->forceStopRemainingContainers(array_keys($processes), $server);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
usleep(100000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function stopContainer(string $containerName, $server, int $timeout): InvokedProcess
|
|
||||||
{
|
|
||||||
return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
|
|
||||||
}
|
|
||||||
|
|
||||||
private function removeContainer(string $containerName, $server)
|
|
||||||
{
|
|
||||||
instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function forceStopRemainingContainers(array $containerNames, $server)
|
|
||||||
{
|
|
||||||
foreach ($containerNames as $containerName) {
|
|
||||||
instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false);
|
|
||||||
$this->removeContainer($containerName, $server);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user