Skip to content

Commit 5a0edff

Browse files
fix: get back health check for postgres legacy (#3010)
1 parent 780ebf3 commit 5a0edff

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

waku/waku_archive_legacy/driver/postgres_driver/postgres_driver.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import
1515
../../../waku_core,
1616
../../common,
1717
../../driver,
18+
./postgres_healthcheck,
1819
../../../common/databases/db_postgres as waku_postgres
1920

2021
type PostgresDriver* = ref object of ArchiveDriver
@@ -132,6 +133,12 @@ proc new*(
132133
let writeConnPool = PgAsyncPool.new(dbUrl, maxNumConnOnEachPool).valueOr:
133134
return err("error creating write conn pool PgAsyncPool")
134135

136+
if not isNil(onFatalErrorAction):
137+
asyncSpawn checkConnectivity(readConnPool, onFatalErrorAction)
138+
139+
if not isNil(onFatalErrorAction):
140+
asyncSpawn checkConnectivity(writeConnPool, onFatalErrorAction)
141+
135142
let driver = PostgresDriver(writeConnPool: writeConnPool, readConnPool: readConnPool)
136143
return ok(driver)
137144

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{.push raises: [].}
2+
3+
import chronos, chronicles, results
4+
import ../../../common/databases/db_postgres, ../../../common/error_handling
5+
6+
## Simple query to validate that the postgres is working and attending requests
7+
const HealthCheckQuery = "SELECT version();"
8+
const CheckConnectivityInterval = 60.seconds
9+
const MaxNumTrials = 20
10+
const TrialInterval = 1.seconds
11+
12+
proc checkConnectivity*(
13+
connPool: PgAsyncPool, onFatalErrorAction: OnFatalErrorHandler
14+
) {.async.} =
15+
while true:
16+
(await connPool.pgQuery(HealthCheckQuery)).isOkOr:
17+
## The connection failed once. Let's try reconnecting for a while.
18+
## Notice that the 'pgQuery' proc tries to establish a new connection.
19+
20+
block errorBlock:
21+
## Force close all the opened connections. No need to close gracefully.
22+
(await connPool.resetConnPool()).isOkOr:
23+
onFatalErrorAction("checkConnectivity legacy resetConnPool error: " & error)
24+
25+
var numTrial = 0
26+
while numTrial < MaxNumTrials:
27+
let res = await connPool.pgQuery(HealthCheckQuery)
28+
if res.isOk():
29+
## Connection resumed. Let's go back to the normal healthcheck.
30+
break errorBlock
31+
32+
await sleepAsync(TrialInterval)
33+
numTrial.inc()
34+
35+
## The connection couldn't be resumed. Let's inform the upper layers.
36+
onFatalErrorAction("postgres legacy health check error: " & error)
37+
38+
await sleepAsync(CheckConnectivityInterval)

0 commit comments

Comments
 (0)