Skip to content

Commit

Permalink
DockerContainerStatus check: 'container not found' is not a check error
Browse files Browse the repository at this point in the history
Before this, a non existing container was interpreted as check error
while it's actually a valid result. The check didn't fail, we know for
sure that the container is not running.

see #141
  • Loading branch information
flo-at committed Nov 30, 2023
1 parent 02132dd commit 69c1b10
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/check/docker_container_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,43 @@ pub struct DockerContainerStatus {
containers: Vec<String>,
}

enum ContainerStatus {
NotFound,
Found(bollard::models::ContainerState),
}

impl DockerContainerStatus {
async fn container_state(
docker: &bollard::Docker,
container: &str,
) -> Result<bollard::models::ContainerState> {
docker
async fn container_state(docker: &bollard::Docker, container: &str) -> Result<ContainerStatus> {
let response = docker
.inspect_container(
container,
Some(bollard::container::InspectContainerOptions { size: false }),
)
.await
.await;
if let Err(bollard::errors::Error::DockerResponseServerError {
status_code: 404,
message: _,
}) = response
{
return Ok(ContainerStatus::NotFound);
}
response
.map_err(|x| Error(format!("Docker error: {x}")))
.and_then(|x| {
x.state
.ok_or_else(|| Error(String::from("Could not read container state.")))
})
.map(ContainerStatus::Found)
}

async fn container_running_and_healthy(
docker: &bollard::Docker,
container: &str,
) -> Result<bool> {
let state = Self::container_state(docker, container).await?;
let state = match Self::container_state(docker, container).await? {
ContainerStatus::NotFound => return Ok(false),
ContainerStatus::Found(state) => state,
};
let status = state
.status
.ok_or_else(|| Error(String::from("Could not read container status.")))
Expand Down

0 comments on commit 69c1b10

Please sign in to comment.