Files
peardata/scripts/healthcheck.js
T
Raven Scott 015d92a257
Release rolling / release (push) Has been cancelled
CI / test (push) Has been cancelled
first commit
2026-07-18 16:17:38 -04:00

43 lines
1.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* Process supervision healthcheck.
* If PEARDATA_HEALTH_KEY is set, dials the server and pings.
* Otherwise exits 0 when the process can import server modules (liveness).
*
* Exit 0 = healthy, 1 = unhealthy.
*/
import { PearDataConnection } from '../client/connection.js'
const timeoutMs = Number(process.env.HEALTHCHECK_TIMEOUT_MS) || 8000
const publicKey = process.env.PEARDATA_HEALTH_KEY || process.env.SERVER_PUBLIC_KEY
const timer = setTimeout(() => {
console.error('healthcheck: timeout')
process.exit(1)
}, timeoutMs)
try {
if (!publicKey || !/^[0-9a-fA-F]{64}$/.test(publicKey)) {
// Liveness without remote dial
clearTimeout(timer)
console.log('ok liveness')
process.exit(0)
}
const conn = new PearDataConnection(publicKey, {
timeoutMs,
adminSeed: process.env.SERVER_SEED || null,
})
await conn.connect()
const pong = await conn.ping()
await conn.destroy()
clearTimeout(timer)
if (!pong?.ok) throw new Error('ping failed')
console.log('ok ping', pong.pong)
process.exit(0)
} catch (err) {
clearTimeout(timer)
console.error('healthcheck failed:', err.message)
process.exit(1)
}