|
1 | 1 | import Dockerode from "dockerode"; |
2 | 2 | import { AbstractWaitStrategy } from "./wait-strategy"; |
3 | | -import { log } from "../common"; |
| 3 | +import { IntervalRetry, log } from "../common"; |
4 | 4 | import { getContainerRuntimeClient } from "../container-runtime"; |
5 | 5 |
|
6 | 6 | export class HealthCheckWaitStrategy extends AbstractWaitStrategy { |
7 | 7 | public async waitUntilReady(container: Dockerode.Container): Promise<void> { |
8 | 8 | log.debug(`Waiting for health check...`, { containerId: container.id }); |
9 | | - |
10 | 9 | const client = await getContainerRuntimeClient(); |
11 | | - const containerEvents = await client.container.events(container, ["health_status"]); |
12 | 10 |
|
13 | | - return new Promise((resolve, reject) => { |
14 | | - const timeout = setTimeout(() => { |
15 | | - const message = `Health check not healthy after ${this.startupTimeout}ms`; |
| 11 | + const status = await new IntervalRetry<string | undefined, Error>(100).retryUntil( |
| 12 | + async () => (await client.container.inspect(container)).State.Health?.Status, |
| 13 | + (healthCheckStatus) => healthCheckStatus === "healthy" || healthCheckStatus === "unhealthy", |
| 14 | + () => { |
| 15 | + const timeout = this.startupTimeout; |
| 16 | + const message = `Health check not healthy after ${timeout}ms`; |
16 | 17 | log.error(message, { containerId: container.id }); |
17 | | - containerEvents.destroy(); |
18 | | - reject(new Error(message)); |
19 | | - }, this.startupTimeout); |
20 | | - |
21 | | - const onTerminalState = () => { |
22 | | - clearTimeout(timeout); |
23 | | - containerEvents.destroy(); |
24 | | - log.debug(`Health check wait strategy complete`, { containerId: container.id }); |
25 | | - }; |
26 | | - |
27 | | - containerEvents.on("data", (data) => { |
28 | | - const parsedData = JSON.parse(data); |
| 18 | + throw new Error(message); |
| 19 | + }, |
| 20 | + this.startupTimeout |
| 21 | + ); |
29 | 22 |
|
30 | | - const status = |
31 | | - parsedData.status.split(":").length === 2 |
32 | | - ? parsedData.status.split(":")[1].trim() // Docker |
33 | | - : parsedData.HealthStatus; // Podman |
| 23 | + if (status !== "healthy") { |
| 24 | + const message = `Health check failed: ${status}`; |
| 25 | + log.error(message, { containerId: container.id }); |
| 26 | + throw new Error(message); |
| 27 | + } |
34 | 28 |
|
35 | | - if (status === "healthy") { |
36 | | - resolve(); |
37 | | - onTerminalState(); |
38 | | - } else if (status === "unhealthy") { |
39 | | - const message = `Health check failed: ${status}`; |
40 | | - log.error(message, { containerId: container.id }); |
41 | | - reject(new Error(message)); |
42 | | - onTerminalState(); |
43 | | - } |
44 | | - }); |
45 | | - }); |
| 29 | + log.debug(`Health check wait strategy complete`, { containerId: container.id }); |
46 | 30 | } |
47 | 31 | } |
0 commit comments