refactor(Database): streamline container shutdown process and reduce timeout duration
This commit is contained in:
@@ -11,7 +11,6 @@ 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;
|
||||||
|
|
||||||
class StopDatabase
|
class StopDatabase
|
||||||
@@ -39,37 +38,12 @@ class StopDatabase
|
|||||||
return 'Database stopped successfully';
|
return 'Database stopped successfully';
|
||||||
}
|
}
|
||||||
|
|
||||||
private function stopContainer($database, string $containerName, int $timeout = 300): void
|
private function stopContainer($database, string $containerName, int $timeout = 30): void
|
||||||
{
|
{
|
||||||
$server = $database->destination->server;
|
$server = $database->destination->server;
|
||||||
|
instant_remote_process(command: [
|
||||||
$process = Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");
|
"docker stop --time=$timeout $containerName",
|
||||||
|
"docker rm -f $containerName",
|
||||||
$startTime = time();
|
], server: $server, throwError: false);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
|||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Process;
|
|
||||||
use Illuminate\Support\Sleep;
|
use Illuminate\Support\Sleep;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
@@ -2246,24 +2245,16 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
|
|||||||
$this->application_deployment_queue->addLogEntry('Building docker image completed.');
|
$this->application_deployment_queue->addLogEntry('Building docker image completed.');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function graceful_shutdown_container(string $containerName, int $timeout = 300)
|
private function graceful_shutdown_container(string $containerName, int $timeout = 30)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->execute_remote_command(
|
$this->execute_remote_command(
|
||||||
["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true],
|
["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true],
|
||||||
);
|
["docker rm -f $containerName", 'hidden' => true, 'ignore_errors' => true]
|
||||||
} catch (\Exception $error) {
|
);
|
||||||
|
} catch (Exception $error) {
|
||||||
$this->application_deployment_queue->addLogEntry("Error stopping container $containerName: ".$error->getMessage(), 'stderr');
|
$this->application_deployment_queue->addLogEntry("Error stopping container $containerName: ".$error->getMessage(), 'stderr');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->remove_container($containerName);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function remove_container(string $containerName)
|
|
||||||
{
|
|
||||||
$this->execute_remote_command(
|
|
||||||
["docker rm -f $containerName", 'hidden' => true, 'ignore_errors' => true]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function stop_running_container(bool $force = false)
|
private function stop_running_container(bool $force = false)
|
||||||
|
|||||||
Reference in New Issue
Block a user