Merge pull request #2969 from alexzvn/feat/graceful-shutdown

feat: add graceful shutdown
This commit is contained in:
Andras Bacsai
2024-08-06 12:13:00 +02:00
committed by GitHub

View File

@@ -2028,6 +2028,24 @@ 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.');
} }
/**
* @param integer $timeout in seconds
*/
private function graceful_shutdown_container(string $containerName, int $timeout = 300) {
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]
);
} catch (\Exception $error) {
// report error if needed
}
$this->execute_remote_command(
["docker rm -f $containerName >/dev/null 2>&1", 'hidden' => true, 'ignore_errors' => true]
);
}
private function stop_running_container(bool $force = false) private function stop_running_container(bool $force = false)
{ {
$this->application_deployment_queue->addLogEntry('Removing old containers.'); $this->application_deployment_queue->addLogEntry('Removing old containers.');
@@ -2039,15 +2057,10 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
}); });
} }
$containers->each(function ($container) { $containers->each(function ($container) {
$containerName = data_get($container, 'Names'); $this->graceful_shutdown_container(data_get($container, 'Names'));
$this->execute_remote_command(
["docker rm -f $containerName >/dev/null 2>&1", 'hidden' => true, 'ignore_errors' => true],
);
}); });
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 || isset($this->application->settings->custom_internal_name)) {
$this->execute_remote_command( $this->graceful_shutdown_container($this->container_name);
["docker rm -f $this->container_name >/dev/null 2>&1", 'hidden' => true, 'ignore_errors' => true],
);
} }
} else { } else {
if ($this->application->dockerfile || $this->application->build_pack === 'dockerfile' || $this->application->build_pack === 'dockerimage') { if ($this->application->dockerfile || $this->application->build_pack === 'dockerfile' || $this->application->build_pack === 'dockerimage') {
@@ -2059,9 +2072,7 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
$this->application_deployment_queue->update([ $this->application_deployment_queue->update([
'status' => ApplicationDeploymentStatus::FAILED->value, 'status' => ApplicationDeploymentStatus::FAILED->value,
]); ]);
$this->execute_remote_command( $this->graceful_shutdown_container($this->container_name);
["docker rm -f $this->container_name >/dev/null 2>&1", 'hidden' => true, 'ignore_errors' => true],
);
} }
} }