Skip to content

Commit 7aa8e56

Browse files
committed
fix(engine): adjust container readiness health checks (#425)
1 parent 8eae481 commit 7aa8e56

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

engine/internal/retrieval/engine/postgres/tools/health/healthcheck.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ func GetConfig(username, dbname string, options ...ContainerOption) *container.H
4646
return healthConfig
4747
}
4848

49-
// OptionRetries allows overwrite retries counter.
49+
// OptionRetries allows overwriting retries counter.
5050
func OptionRetries(retries int) ContainerOption {
5151
return func(h *container.HealthConfig) {
5252
h.Retries = retries
5353
}
5454
}
5555

56-
// OptionInterval allows overwrite a health check interval.
56+
// OptionInterval allows overwriting a health check interval.
5757
func OptionInterval(interval time.Duration) ContainerOption {
5858
return func(h *container.HealthConfig) {
5959
h.Interval = interval
6060
}
6161
}
6262

63-
// OptionTest allows overwrite a health check test command.
63+
// OptionTest allows overwriting a health check test command.
6464
func OptionTest(testCommand string) ContainerOption {
6565
return func(h *container.HealthConfig) {
6666
if testCommand != "" {

engine/internal/retrieval/engine/postgres/tools/tools.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ func StopPostgres(ctx context.Context, dockerClient *client.Client, containerID,
321321
func CheckContainerReadiness(ctx context.Context, dockerClient *client.Client, containerID string) (err error) {
322322
log.Msg("Check container readiness: ", containerID)
323323

324+
var errorRepeats bool
325+
324326
for {
325327
select {
326328
case <-ctx.Done():
@@ -344,11 +346,22 @@ func CheckContainerReadiness(ctx context.Context, dockerClient *client.Client, c
344346
}
345347

346348
if healthCheckLength := len(resp.State.Health.Log); healthCheckLength > 0 {
349+
// Checking exit code 2 and 3 because pg_isready returns
350+
// 0 to the shell if the server is accepting connections normally,
351+
// 1 if the server is rejecting connections (for example during startup),
352+
// 2 if there was no response to the connection attempt, and
353+
// 3 if no attempt was made (for example due to invalid parameters).
354+
// Supposedly, the status 2 will be returned in cases where the server is not running
355+
// and will not start on its own, so there is no reason to wait for all specified retries.
347356
if lastHealthCheck := resp.State.Health.Log[healthCheckLength-1]; lastHealthCheck.ExitCode > 1 {
348-
return &ErrHealthCheck{
349-
ExitCode: lastHealthCheck.ExitCode,
350-
Output: lastHealthCheck.Output,
357+
if errorRepeats {
358+
return &ErrHealthCheck{
359+
ExitCode: lastHealthCheck.ExitCode,
360+
Output: lastHealthCheck.Output,
361+
}
351362
}
363+
364+
errorRepeats = true
352365
}
353366
}
354367
}

0 commit comments

Comments
 (0)