@@ -321,6 +321,8 @@ func StopPostgres(ctx context.Context, dockerClient *client.Client, containerID,
321321func 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