graceful db stop and db deletion fixes

This commit is contained in:
ayntk-ai
2024-08-09 22:15:45 +02:00
parent c566152f37
commit 16a5c601e3
3 changed files with 49 additions and 19 deletions

View File

@@ -10,25 +10,65 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql; use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql; use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis; use App\Models\StandaloneRedis;
use Illuminate\Support\Facades\Process;
use Lorisleiva\Actions\Concerns\AsAction; use Lorisleiva\Actions\Concerns\AsAction;
use App\Actions\Server\CleanupDocker;
class StopDatabase class StopDatabase
{ {
use AsAction; use AsAction;
public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $database) public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $database, bool $isDeleteOperation = false)
{ {
$server = $database->destination->server; $server = $database->destination->server;
if (! $server->isFunctional()) { if (!$server->isFunctional()) {
return 'Server is not functional'; return 'Server is not functional';
} }
instant_remote_process(command: ["docker stop --time=30 $database->uuid"], server: $server, throwError: false); $this->stopContainer($database, $database->uuid, 300);
instant_remote_process(command: ["docker rm $database->uuid"], server: $server, throwError: false); if (!$isDeleteOperation) {
instant_remote_process(command: ["docker rm -f $database->uuid"], server: $server, throwError: false); $this->deleteConnectedNetworks($database->uuid, $server); //Probably not needed as DBs do not have a network normally
CleanupDocker::run($server, true);
}
if ($database->is_public) { if ($database->is_public) {
StopDatabaseProxy::run($database); StopDatabaseProxy::run($database);
} }
return 'Database stopped successfully';
}
private function stopContainer($database, string $containerName, int $timeout = 300): void
{
$server = $database->destination->server;
$process = Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
$startTime = time();
while ($process->running()) {
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);
}
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);
} }
} }

View File

@@ -31,4 +31,4 @@ class StopService
return $e->getMessage(); return $e->getMessage();
} }
} }
} }

View File

@@ -56,17 +56,7 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
case 'standalone-dragonfly': case 'standalone-dragonfly':
case 'standalone-clickhouse': case 'standalone-clickhouse':
$persistentStorages = $this->resource?->persistentStorages()?->get(); $persistentStorages = $this->resource?->persistentStorages()?->get();
StopDatabase::run($this->resource); StopDatabase::run($this->resource, true);
// TODO
// DBs do not have a network normally?
//if ($this->deleteConnectedNetworks) {
// $this->resource?->delete_connected_networks($this->resource->uuid);
// }
// }
// $server = data_get($this->resource, 'server');
// if ($this->deleteImages && $server) {
// CleanupDocker::run($server, true);
// }
break; break;
case 'service': case 'service':
StopService::run($this->resource, true); StopService::run($this->resource, true);
@@ -80,10 +70,10 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
if ($this->deleteConfigurations) { if ($this->deleteConfigurations) {
$this->resource?->delete_configurations(); $this->resource?->delete_configurations();
} }
$server = data_get($this->resource, 'server'); $server = data_get($this->resource, 'server');
if ($this->deleteImages && $server) { if ($this->deleteImages && $server) {
CleanupDocker::run($server, true); CleanupDocker::run($server, true); //this is run for DBs but it does not work for DBs
} }
if ($this->deleteConnectedNetworks) { if ($this->deleteConnectedNetworks) {