fix: graceful shutdown

This commit is contained in:
Andras Bacsai
2024-08-06 13:05:34 +02:00
parent f4d64e121c
commit a4f107e191
2 changed files with 22 additions and 20 deletions

View File

@@ -1025,7 +1025,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$this->write_deployment_configurations();
$this->server = $this->original_server;
}
if (count($this->application->ports_mappings_array) > 0 || (bool) $this->application->settings->is_consistent_container_name_enabled || isset($this->application->settings->custom_internal_name) || $this->pull_request_id !== 0 || str($this->application->custom_docker_run_options)->contains('--ip') || str($this->application->custom_docker_run_options)->contains('--ip6')) {
if (count($this->application->ports_mappings_array) > 0 || (bool) $this->application->settings->is_consistent_container_name_enabled || str($this->application->settings->custom_internal_name)->isNotEmpty() || $this->pull_request_id !== 0 || str($this->application->custom_docker_run_options)->contains('--ip') || str($this->application->custom_docker_run_options)->contains('--ip6')) {
$this->application_deployment_queue->addLogEntry('----------------------------------------');
if (count($this->application->ports_mappings_array) > 0) {
$this->application_deployment_queue->addLogEntry('Application has ports mapped to the host system, rolling update is not supported.');
@@ -1517,7 +1517,6 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
{
$this->create_workdir();
$ports = $this->application->main_port();
ray('generate_compose_file: ', $ports);
$onlyPort = null;
if (count($ports) > 0) {
$onlyPort = $ports[0];
@@ -2029,27 +2028,32 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
}
/**
* @param integer $timeout in seconds
* @param int $timeout in seconds
*/
private function graceful_shutdown_container(string $containerName, int $timeout = 300) {
private function graceful_shutdown_container(string $containerName, int $timeout = 60)
{
try {
return $this->execute_remote_command(
["docker stop --time=$timeout $containerName > /dev/null 2>&1", 'hidden' => true],
["docker rm $containerName > /dev/null 2>&1", 'hidden' => true]
$this->execute_remote_command(
["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true],
["docker rm $containerName", 'hidden' => true, 'ignore_errors' => true]
);
} catch (\Exception $error) {
// report error if needed
}
$this->execute_remote_command(
["docker rm -f $containerName >/dev/null 2>&1", 'hidden' => true, 'ignore_errors' => true]
["docker rm -f $containerName", 'hidden' => true, 'ignore_errors' => true]
);
}
private function stop_running_container(bool $force = false)
{
$this->application_deployment_queue->addLogEntry('Removing old containers.');
if ($this->newVersionIsHealthy || $force) {
if ($this->application->settings->is_consistent_container_name_enabled || str($this->application->settings->custom_internal_name)->isNotEmpty()) {
$this->graceful_shutdown_container($this->container_name);
} else {
$containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id);
if ($this->pull_request_id === 0) {
$containers = $containers->filter(function ($container) {
@@ -2059,8 +2063,6 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
$containers->each(function ($container) {
$this->graceful_shutdown_container(data_get($container, 'Names'));
});
if ($this->application->settings->is_consistent_container_name_enabled || isset($this->application->settings->custom_internal_name)) {
$this->graceful_shutdown_container($this->container_name);
}
} else {
if ($this->application->dockerfile || $this->application->build_pack === 'dockerfile' || $this->application->build_pack === 'dockerimage') {
@@ -2247,7 +2249,7 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
ray($code);
if ($code !== 69420) {
// 69420 means failed to push the image to the registry, so we don't need to remove the new version as it is the currently running one
if ($this->application->settings->is_consistent_container_name_enabled || isset($this->application->settings->custom_internal_name)) {
if ($this->application->settings->is_consistent_container_name_enabled || str($this->application->settings->custom_internal_name)->isNotEmpty()) {
// do not remove already running container
} else {
$this->application_deployment_queue->addLogEntry('Deployment failed. Removing the new version of your application.', 'stderr');

View File

@@ -91,7 +91,7 @@ class Advanced extends Component
public function saveCustomName()
{
if (isset($this->application->settings->custom_internal_name)) {
if (str($this->application->settings->custom_internal_name)->isNotEmpty()) {
$this->application->settings->custom_internal_name = str($this->application->settings->custom_internal_name)->slug()->value();
} else {
$this->application->settings->custom_internal_name = null;