mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-04 10:42:23 +00:00
Status checks now queue a delayed check for each application, preview, service part, or database that goes from up to exited. After 2 minutes it sends the "Resource status changes" notification if the resource is still stopped, no deployment runs, and Coolify did not stop it. Each resource sends at most one notification in 15 minutes. Closes #9493
77 lines
2.8 KiB
PHP
77 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Database;
|
|
|
|
use App\Actions\Server\CleanupDocker;
|
|
use App\Events\ServiceStatusChanged;
|
|
use App\Jobs\NotifyResourceStoppedJob;
|
|
use App\Models\BaseModel;
|
|
use App\Models\StandaloneClickhouse;
|
|
use App\Models\StandaloneDragonfly;
|
|
use App\Models\StandaloneKeydb;
|
|
use App\Models\StandaloneMariadb;
|
|
use App\Models\StandaloneMongodb;
|
|
use App\Models\StandaloneMysql;
|
|
use App\Models\StandalonePostgresql;
|
|
use App\Models\StandaloneRedis;
|
|
use App\Models\StandaloneSqlite;
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
class StopDatabase
|
|
{
|
|
use AsAction;
|
|
|
|
public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|StandaloneSqlite $database, bool $dockerCleanup = true, bool $resetRestartCount = true, bool $removeContainer = true, bool $keepAnonymousDataVolume = true): string
|
|
{
|
|
try {
|
|
$server = $database->destination->server;
|
|
if (! $server->isFunctional()) {
|
|
return 'Server is not functional';
|
|
}
|
|
|
|
// A removed container loses the link to the unnamed volume that holds the current data.
|
|
if ($removeContainer && $keepAnonymousDataVolume && $database instanceof StandaloneClickhouse && $database->anonymousDataVolume() !== null) {
|
|
$removeContainer = false;
|
|
}
|
|
|
|
NotifyResourceStoppedJob::expectStop($database);
|
|
$this->stopContainer($database, $database->uuid, 30, $removeContainer);
|
|
|
|
// Reset restart tracking when database is manually stopped
|
|
$database->update(['status' => 'exited']);
|
|
if ($resetRestartCount) {
|
|
$database->update([
|
|
'restart_count' => 0,
|
|
'last_restart_at' => null,
|
|
'last_restart_type' => null,
|
|
]);
|
|
}
|
|
|
|
if ($dockerCleanup) {
|
|
CleanupDocker::dispatchAfterStop($server);
|
|
}
|
|
|
|
if ($database->is_public) {
|
|
StopDatabaseProxy::run($database);
|
|
}
|
|
|
|
return 'Database stopped successfully';
|
|
} catch (\Exception $e) {
|
|
return 'Database stop failed: '.$e->getMessage();
|
|
} finally {
|
|
ServiceStatusChanged::dispatch($database->environment->project->team->id);
|
|
}
|
|
|
|
}
|
|
|
|
private function stopContainer(BaseModel $database, string $containerName, int $timeout = 30, bool $removeContainer = true): void
|
|
{
|
|
$server = $database->destination->server;
|
|
$commands = [dockerStopCommand($timeout, $containerName, $server)];
|
|
if ($removeContainer) {
|
|
$commands[] = "docker rm -f $containerName";
|
|
}
|
|
instant_remote_process(command: $commands, server: $server, throwError: false);
|
|
}
|
|
}
|