Feat/Fix: Proxy stop and restart confirmation

This commit is contained in:
ayntk-ai
2024-09-04 20:41:17 +02:00
parent 505127dae5
commit 3e04a7958e
2 changed files with 58 additions and 18 deletions

View File

@@ -7,6 +7,8 @@ use App\Actions\Proxy\StartProxy;
use App\Events\ProxyStatusChanged; use App\Events\ProxyStatusChanged;
use App\Models\Server; use App\Models\Server;
use Livewire\Component; use Livewire\Component;
use Illuminate\Support\Facades\Process;
use Illuminate\Process\InvokedProcess;
class Deploy extends Component class Deploy extends Component
{ {
@@ -94,21 +96,43 @@ class Deploy extends Component
public function stop(bool $forceStop = true) public function stop(bool $forceStop = true)
{ {
try { try {
if ($this->server->isSwarm()) { $containerName = $this->server->isSwarm() ? 'coolify-proxy_traefik' : 'coolify-proxy';
instant_remote_process([ $timeout = 30;
'docker service rm coolify-proxy_traefik',
], $this->server); $process = $this->stopContainer($containerName, $timeout);
} else {
instant_remote_process([ $startTime = time();
'docker rm -f coolify-proxy', while ($process->running()) {
], $this->server); if (time() - $startTime >= $timeout) {
$this->forceStopContainer($containerName);
break;
}
usleep(100000);
} }
$this->server->proxy->status = 'exited';
$this->server->proxy->force_stop = $forceStop; $this->removeContainer($containerName);
$this->server->save();
$this->dispatch('proxyStatusUpdated');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} finally {
$this->server->proxy->force_stop = $forceStop;
$this->server->proxy->status = 'exited';
$this->server->save();
$this->dispatch('proxyStatusUpdated');
} }
} }
private function stopContainer(string $containerName, int $timeout): InvokedProcess
{
return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
}
private function forceStopContainer(string $containerName)
{
instant_remote_process(["docker kill $containerName"], $this->server, throwError: false);
}
private function removeContainer(string $containerName)
{
instant_remote_process(["docker rm -f $containerName"], $this->server, throwError: false);
}
} }

View File

@@ -24,7 +24,17 @@
</a> </a>
</button> </button>
@endif @endif
<x-modal-confirmation @click="$wire.dispatch('restartEvent')"> <x-modal-confirmation
title="Confirm Proxy Restart?"
buttonTitle="Restart Proxy"
submitAction="restart"
:actions="['This proxy will be stopped and started again.', 'All resources hosted on coolify will be unavailable during the restart.']"
:confirmWithText="false"
:confirmWithPassword="false"
step2ButtonText="Restart Proxy"
:dispatchEvent="true"
dispatchEventType="restartEvent"
>
<x-slot:button-title> <x-slot:button-title>
<svg class="w-5 h-5 dark:text-warning" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <svg class="w-5 h-5 dark:text-warning" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
@@ -35,10 +45,18 @@
</svg> </svg>
Restart Proxy Restart Proxy
</x-slot:button-title> </x-slot:button-title>
This proxy will be stopped and started. It is not reversible. <br>All resources will be unavailable
during the restart. <br>Please think again.
</x-modal-confirmation> </x-modal-confirmation>
<x-modal-confirmation @click="$wire.dispatch('stopEvent')"> <x-modal-confirmation
title="Confirm Proxy Stopping?"
buttonTitle="Stop Proxy"
submitAction="stop(true)"
:actions="['The coolify proxy will be stopped.', 'All resources hosted on coolify will be unavailable.']"
:confirmWithText="false"
:confirmWithPassword="false"
step2ButtonText="Stop Proxy"
:dispatchEvent="true"
dispatchEventType="stopEvent"
>
<x-slot:button-title> <x-slot:button-title>
<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-error" viewBox="0 0 24 24" <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-error" viewBox="0 0 24 24"
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
@@ -51,8 +69,6 @@
</svg> </svg>
Stop Proxy Stop Proxy
</x-slot:button-title> </x-slot:button-title>
This proxy will be stopped. It is not reversible. <br>All resources will be unavailable.
<br>Please think again.
</x-modal-confirmation> </x-modal-confirmation>
</div> </div>
@else @else