Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ed0bb8

Browse files
Merge branch '4-precise-status-handling' into 'master'
feat: adaptive timeout on pg start, remove verbose debug Closes #4 See merge request postgres-ai/database-lab!92
2 parents b82fadc + 9a99948 commit 2ed0bb8

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

pkg/services/provision/databases/postgres/postgres.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323
)
2424

2525
const (
26-
// waitPostgresTimeout defines timeout to wait Postgres ready.
27-
waitPostgresTimeout = 360
26+
// waitPostgresConnectionTimeout defines timeout to wait for Postgres initial connection.
27+
waitPostgresConnectionTimeout = 120
28+
29+
// waitPostgresStartTimeout defines timeout to wait for Postgres start.
30+
waitPostgresStartTimeout = 360
2831

2932
// checkPostgresStatusPeriod defines period to check Postgres status.
3033
checkPostgresStatusPeriod = 500
@@ -56,9 +59,10 @@ func Start(r runners.Runner, c *resources.AppConfig) error {
5659
return errors.Wrap(err, "failed to run container")
5760
}
5861

59-
// Waiting for server to become ready and promoting if needed.
62+
// Waiting for server to become ready and promote if needed.
6063
first := true
6164
cnt := 0
65+
waitPostgresTimeout := waitPostgresConnectionTimeout
6266

6367
for {
6468
logs, err := docker.GetLogs(r, c, logsMinuteWindow)
@@ -75,9 +79,6 @@ func Start(r runners.Runner, c *resources.AppConfig) error {
7579

7680
out, err := runSimpleSQL("select pg_is_in_recovery()", c)
7781

78-
log.Dbg("sql: out: ", out)
79-
log.Err("sql: err: ", err)
80-
8182
if err == nil {
8283
// Server does not need promotion if it is not in recovery.
8384
if out == "f" || out == "false" {
@@ -88,6 +89,9 @@ func Start(r runners.Runner, c *resources.AppConfig) error {
8889
if out == "t" && first {
8990
log.Dbg("Postgres instance needs promotion.")
9091

92+
// Increase Postgres start timeout for promotion.
93+
waitPostgresTimeout = waitPostgresStartTimeout
94+
9195
first = false
9296

9397
_, err = pgctlPromote(r, c)
@@ -99,16 +103,18 @@ func Start(r runners.Runner, c *resources.AppConfig) error {
99103
return err
100104
}
101105
}
106+
} else {
107+
log.Err("Currently cannot connect to Postgres: ", out, err)
102108
}
103109

104110
cnt++
105111

106-
if cnt > waitPostgresTimeout { // 3 minutes
112+
if cnt > waitPostgresTimeout {
107113
if runnerErr := Stop(r, c); runnerErr != nil {
108114
log.Err(runnerErr)
109115
}
110116

111-
return errors.Wrap(err, "postgres could not be promoted within 3 minutes")
117+
return errors.Wrap(err, "postgres start timeout")
112118
}
113119

114120
time.Sleep(checkPostgresStatusPeriod * time.Millisecond)

0 commit comments

Comments
 (0)