2023-10-14 14:22:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Actions\Database;
|
|
|
|
|
|
2024-09-18 21:18:47 +02:00
|
|
|
use App\Actions\Server\CleanupDocker;
|
2024-04-10 15:00:46 +02:00
|
|
|
use App\Models\StandaloneClickhouse;
|
|
|
|
|
use App\Models\StandaloneDragonfly;
|
|
|
|
|
use App\Models\StandaloneKeydb;
|
2023-10-24 14:31:28 +02:00
|
|
|
use App\Models\StandaloneMariadb;
|
2023-10-19 13:32:03 +02:00
|
|
|
use App\Models\StandaloneMongodb;
|
2023-10-24 14:31:28 +02:00
|
|
|
use App\Models\StandaloneMysql;
|
2023-10-14 14:22:07 +02:00
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
|
use App\Models\StandaloneRedis;
|
2024-08-09 22:15:45 +02:00
|
|
|
use Illuminate\Support\Facades\Process;
|
2023-10-14 14:22:07 +02:00
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
|
|
|
|
|
class StopDatabase
|
|
|
|
|
{
|
|
|
|
|
use AsAction;
|
|
|
|
|
|
2024-09-04 14:59:44 +02:00
|
|
|
public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $database, bool $isDeleteOperation = false, bool $dockerCleanup = true)
|
2023-10-14 14:22:07 +02:00
|
|
|
{
|
|
|
|
|
$server = $database->destination->server;
|
2024-09-18 21:18:47 +02:00
|
|
|
if (! $server->isFunctional()) {
|
2024-01-30 09:48:51 +01:00
|
|
|
return 'Server is not functional';
|
|
|
|
|
}
|
2024-08-06 13:27:06 +02:00
|
|
|
|
2024-08-09 22:15:45 +02:00
|
|
|
$this->stopContainer($database, $database->uuid, 300);
|
2025-01-07 15:31:43 +01:00
|
|
|
if (! $isDeleteOperation) {
|
|
|
|
|
if ($dockerCleanup) {
|
|
|
|
|
CleanupDocker::dispatch($server, true);
|
|
|
|
|
}
|
2024-08-09 22:15:45 +02:00
|
|
|
}
|
2024-08-06 13:27:06 +02:00
|
|
|
|
2023-10-14 14:22:07 +02:00
|
|
|
if ($database->is_public) {
|
|
|
|
|
StopDatabaseProxy::run($database);
|
|
|
|
|
}
|
2024-08-09 22:15:45 +02:00
|
|
|
|
|
|
|
|
return 'Database stopped successfully';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function stopContainer($database, string $containerName, int $timeout = 300): void
|
|
|
|
|
{
|
|
|
|
|
$server = $database->destination->server;
|
|
|
|
|
|
2025-01-07 15:31:43 +01:00
|
|
|
$process = Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
|
2024-08-09 22:15:45 +02:00
|
|
|
|
|
|
|
|
$startTime = time();
|
2025-01-07 15:31:43 +01:00
|
|
|
while ($process->running()) {
|
2024-08-09 22:15:45 +02:00
|
|
|
if (time() - $startTime >= $timeout) {
|
|
|
|
|
$this->forceStopContainer($containerName, $server);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
usleep(100000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->removeContainer($containerName, $server);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function forceStopContainer(string $containerName, $server): void
|
|
|
|
|
{
|
|
|
|
|
instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function removeContainer(string $containerName, $server): void
|
|
|
|
|
{
|
|
|
|
|
instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false);
|
|
|
|
|
}
|
2025-01-07 15:31:43 +01:00
|
|
|
|
|
|
|
|
private function deleteConnectedNetworks($uuid, $server)
|
|
|
|
|
{
|
|
|
|
|
instant_remote_process(["docker network disconnect {$uuid} coolify-proxy"], $server, false);
|
|
|
|
|
instant_remote_process(["docker network rm {$uuid}"], $server, false);
|
|
|
|
|
}
|
2023-10-14 14:22:07 +02:00
|
|
|
}
|