Skip to content

Commit e449dd9

Browse files
Revert HealthCheckWaitStrategy poll (#856)
1 parent 1930c36 commit e449dd9

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed
Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
import Dockerode from "dockerode";
22
import { AbstractWaitStrategy } from "./wait-strategy";
3-
import { log } from "../common";
3+
import { IntervalRetry, log } from "../common";
44
import { getContainerRuntimeClient } from "../container-runtime";
55

66
export class HealthCheckWaitStrategy extends AbstractWaitStrategy {
77
public async waitUntilReady(container: Dockerode.Container): Promise<void> {
88
log.debug(`Waiting for health check...`, { containerId: container.id });
9-
109
const client = await getContainerRuntimeClient();
11-
const containerEvents = await client.container.events(container, ["health_status"]);
1210

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`;
1617
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+
);
2922

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+
}
3428

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 });
4630
}
4731
}

0 commit comments

Comments
 (0)