| 184 | const WATCHDOG_MAX_RESTARTS = 3; |
| 185 | |
| 186 | const probe = async (): Promise<void> => { |
| 187 | const sql = postgres(`postgres://postgres:postgres@127.0.0.1:${PORT}/postgres`, { |
| 188 | max: 1, |
| 189 | idle_timeout: 0, |
| 190 | connect_timeout: 5, |
| 191 | fetch_types: false, |
| 192 | prepare: false, |
| 193 | onnotice: () => undefined, |
| 194 | }); |
| 195 | try { |
| 196 | // connect_timeout only bounds the handshake; race the query too so a |
| 197 | // post-startup wedge cannot hang the watchdog itself. |
| 198 | await Promise.race([ |
| 199 | sql.unsafe("select 1"), |
| 200 | sleep(10_000).then(() => { |
| 201 | throw new Error("probe query timed out after 10s"); |
| 202 | }), |
| 203 | ]); |
| 204 | } finally { |
| 205 | await sql.end({ timeout: 5 }).catch(() => {}); |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | const watchdog = async () => { |
| 210 | let consecutiveFailures = 0; |