add graceful shutdown

This commit is contained in:
Alexzvn
2024-07-29 07:57:13 +00:00
parent 85d080a042
commit 342ef4d367

View File

@@ -2024,6 +2024,26 @@ 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 {
$this->execute_remote_command(
["docker stop --time=$timeout $containerName > /dev/null 2>&1", 'hidden' => true],
["docker rm $containerName > /dev/null 2>&1", 'hidden' => true]
);
return;
} catch (\Exception $error) {
// report error if needed
} finally {
$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.');
@@ -2035,15 +2055,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') {
@@ -2055,9 +2070,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],
);
} }
} }