mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-09 06:22:41 +00:00
22026b7e89
* fix(rln-relay): graceful shutdown with non-zero exit code * fix: missed args * fix: exception str Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> * fix: remove old comment --------- Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>
49 lines
1.5 KiB
Nim
49 lines
1.5 KiB
Nim
when (NimMajor, NimMinor) < (1, 4):
|
|
{.push raises: [Defect].}
|
|
else:
|
|
{.push raises: [].}
|
|
|
|
import
|
|
chronos,
|
|
stew/results
|
|
import
|
|
../../driver,
|
|
../../../common/databases/db_postgres,
|
|
../../../common/error_handling
|
|
|
|
## Simple query to validate that the postgres is working and attending requests
|
|
const HealthCheckQuery = "SELECT version();"
|
|
const CheckConnectivityInterval = 60.seconds
|
|
const MaxNumTrials = 20
|
|
const TrialInterval = 1.seconds
|
|
|
|
proc checkConnectivity*(connPool: PgAsyncPool,
|
|
onFatalErrorAction: OnFatalErrorHandler) {.async.} =
|
|
|
|
while true:
|
|
|
|
(await connPool.pgQuery(HealthCheckQuery)).isOkOr:
|
|
|
|
## The connection failed once. Let's try reconnecting for a while.
|
|
## Notice that the 'exec' proc tries to establish a new connection.
|
|
|
|
block errorBlock:
|
|
## Force close all the opened connections. No need to close gracefully.
|
|
(await connPool.resetConnPool()).isOkOr:
|
|
onFatalErrorAction("checkConnectivity resetConnPool error: " & error)
|
|
|
|
var numTrial = 0
|
|
while numTrial < MaxNumTrials:
|
|
let res = await connPool.pgQuery(HealthCheckQuery)
|
|
if res.isOk():
|
|
## Connection resumed. Let's go back to the normal healthcheck.
|
|
break errorBlock
|
|
|
|
await sleepAsync(TrialInterval)
|
|
numTrial.inc()
|
|
|
|
## The connection couldn't be resumed. Let's inform the upper layers.
|
|
onFatalErrorAction("postgres health check error: " & error)
|
|
|
|
await sleepAsync(CheckConnectivityInterval)
|