From 8d6eb6687f62c1bc1c197d9da2882a230ce7db0f Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Wed, 18 May 2022 12:53:22 +0100 Subject: [PATCH] Handle container not found exception --- .gitlab-ci.yml | 12 ++++++------ dockstat.py | 6 +++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7c0d5fd..0f0d7aa 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,12 +29,12 @@ build-job: # This job runs in the build stage, which runs first. - docker push $CI_REGISTRY_IMAGE - echo "Build complete." -# unit-test-job: # This job runs in the test stage. -# stage: test -# script: -# - echo "Running unit tests..." -# - python3 -munittest discover -# - echo "Testing complete" +unit-test-job: # This job runs in the test stage. + stage: test + script: + - echo "Running unit tests..." + - python3 -munittest discover + - echo "Testing complete" lint-test-job: # This job also runs in the test stage. stage: test # It can run at the same time as unit-test-job (in parallel). diff --git a/dockstat.py b/dockstat.py index 940cc10..e5a3628 100644 --- a/dockstat.py +++ b/dockstat.py @@ -103,7 +103,11 @@ class HTTPHandler(MetricsHandler): # type: ignore[misc] ) for container in self.docker_client.containers.list(all=True): - data = self.docker_api.inspect_container(container.id) + try: + data = self.docker_api.inspect_container(container.id) + except docker.errors.NotFound: + print(f'WARNING: Container {container.id} does not exist. Skipping.') + continue running = bool(data['State']['Running']) started_at = data['State']['StartedAt']