@@ -321,6 +321,8 @@ func StopPostgres(ctx context.Context, dockerClient *client.Client, containerID,
321
321
func CheckContainerReadiness (ctx context.Context , dockerClient * client.Client , containerID string ) (err error ) {
322
322
log .Msg ("Check container readiness: " , containerID )
323
323
324
+ var errorRepeats bool
325
+
324
326
for {
325
327
select {
326
328
case <- ctx .Done ():
@@ -344,11 +346,22 @@ func CheckContainerReadiness(ctx context.Context, dockerClient *client.Client, c
344
346
}
345
347
346
348
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.
347
356
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
+ }
351
362
}
363
+
364
+ errorRepeats = true
352
365
}
353
366
}
354
367
}
0 commit comments