Skip to content

Commit 4ef94a1

Browse files
szachovyCopilot
andcommitted
Handle transient Docker API errors in wait_until_healthy()
Wrap health check polling in try/except to catch APIError, KeyError, and IndexError. These transient errors occur during Docker Swarm stabilization and should be treated as not-healthy-yet rather than crashing the deployment. Closes #43 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0ab8ce7 commit 4ef94a1

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626

2727
* Fixed `run_mysql_server()` not instantiating `MySQLServer` class. (#94)
2828
* Disabled MD060 markdownlint rule to fix table column style false positives in documentation. (#94)
29+
* Handled transient Docker API errors in `wait_until_healthy()` retry loop. (#43)
2930

3031
## 1.0 - 2024-10-13
3132

src/container.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,16 @@ def wait_until_healthy(self, cls: typing.Type[ContainerInstance]) -> str:
198198
cls.run() # type: ignore[call-arg]
199199
time.sleep(cls.healthcheck_start_period)
200200
for _ in range(cls.healthcheck_retries):
201-
if self.container == "superset":
202-
if self.client.api.tasks(filters={"service": "superset"})[0]["Status"]["State"] == "running":
203-
return f"{self.get_logs()}\nContainer {self.container} is healthy"
204-
else:
205-
if self.client.containers.get(self.container).attrs["State"]["Health"]["Status"] == "healthy":
206-
return f"{self.get_logs()}\nContainer {self.container} is healthy"
201+
try:
202+
if self.container == "superset":
203+
tasks = self.client.api.tasks(filters={"service": "superset"})
204+
if tasks and tasks[0]["Status"]["State"] == "running":
205+
return f"{self.get_logs()}\nContainer {self.container} is healthy"
206+
else:
207+
if self.client.containers.get(self.container).attrs["State"]["Health"]["Status"] == "healthy":
208+
return f"{self.get_logs()}\nContainer {self.container} is healthy"
209+
except (docker.errors.APIError, KeyError, IndexError):
210+
pass
207211
time.sleep(cls.healthcheck_interval)
208212
return f"{self.get_logs()}\nTimeout while waiting for {self.container} healthcheck to be healthy"
209213

0 commit comments

Comments
 (0)