2023-10-14 12:22:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Actions\Database;
|
|
|
|
|
|
2024-09-18 19:18:47 +00:00
|
|
|
use App\Actions\Server\CleanupDocker;
|
2024-04-10 13:00:46 +00:00
|
|
|
use App\Models\StandaloneClickhouse;
|
|
|
|
|
use App\Models\StandaloneDragonfly;
|
|
|
|
|
use App\Models\StandaloneKeydb;
|
2023-10-24 12:31:28 +00:00
|
|
|
use App\Models\StandaloneMariadb;
|
2023-10-19 11:32:03 +00:00
|
|
|
use App\Models\StandaloneMongodb;
|
2023-10-24 12:31:28 +00:00
|
|
|
use App\Models\StandaloneMysql;
|
2023-10-14 12:22:07 +00:00
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
|
use App\Models\StandaloneRedis;
|
2024-08-09 20:15:45 +00:00
|
|
|
use Illuminate\Support\Facades\Process;
|
2023-10-14 12:22:07 +00:00
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
|
|
|
|
|
class StopDatabase
|
|
|
|
|
{
|
|
|
|
|
use AsAction;
|
|
|
|
|
|
2024-09-04 12:59:44 +00:00
|
|
|
public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $database, bool $isDeleteOperation = false, bool $dockerCleanup = true)
|
2023-10-14 12:22:07 +00:00
|
|
|
{
|
|
|
|
|
$server = $database->destination->server;
|
2024-09-18 19:18:47 +00:00
|
|
|
if (! $server->isFunctional()) {
|
2024-01-30 08:48:51 +00:00
|
|
|
return 'Server is not functional';
|
|
|
|
|
}
|
2024-08-06 11:27:06 +00:00
|
|
|
|
2024-08-09 20:15:45 +00:00
|
|
|
$this->stopContainer($database, $database->uuid, 300);
|
2025-01-07 13:52:08 +00:00
|
|
|
if (! $isDeleteOperation && $dockerCleanup) {
|
|
|
|
|
CleanupDocker::dispatch($server, true);
|
2024-08-09 20:15:45 +00:00
|
|
|
}
|
2024-08-06 11:27:06 +00:00
|
|
|
|
2023-10-14 12:22:07 +00:00
|
|
|
if ($database->is_public) {
|
|
|
|
|
StopDatabaseProxy::run($database);
|
|
|
|
|
}
|
2024-08-09 20:15:45 +00:00
|
|
|
|
|
|
|
|
return 'Database stopped successfully';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function stopContainer($database, string $containerName, int $timeout = 300): void
|
|
|
|
|
{
|
|
|
|
|
$server = $database->destination->server;
|
|
|
|
|
|
2025-01-07 13:52:08 +00:00
|
|
|
$invokedProcess = Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
|
2024-08-09 20:15:45 +00:00
|
|
|
|
|
|
|
|
$startTime = time();
|
2025-01-07 13:52:08 +00:00
|
|
|
while ($invokedProcess->running()) {
|
2024-08-09 20:15:45 +00: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);
|
|
|
|
|
}
|
2023-10-14 12:22:07 +00:00
|
|
|
}
|