Skip to content

Commit

Permalink
repo: fix runaway pg-pool client, add grateful crash handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ar committed Aug 13, 2024
1 parent 40af2d5 commit 1158dce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
16 changes: 4 additions & 12 deletions desci-repo/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
// import * as pg from 'pg';

const pg = await import('pg').then((value) => value.default);
const { Pool } = pg;

// console.log('DB', process.env.DATABASE_URL);

export const pool = new Pool({
connectionString: process.env.DATABASE_URL,
options: '-c search_path=public',
});

// pool.on('error', (err, client) => {
// console.error('Unexpected error on idle client', err, client);
// // process.exit(-1);
// });

export const client = await pool.connect();

// console.log('DB CLIENT', client.)
pool.on('error', (err, client) => {
// This is fine, client is booted out of the pool already when this happens
console.warn('[db::pool] Unexpected error on idle client', err, client);
});

export const findNodeByUuid = async (uuid: string) => {
try {
Expand Down
27 changes: 24 additions & 3 deletions desci-repo/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { pino } from 'pino';
import { fileURLToPath } from 'url';
import path from 'path';
import { pool } from './db/index.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -67,6 +68,26 @@ function omitBuffer(array) {
});
}

process.on('uncaughtException', (err) => {
logger.fatal(err, 'uncaught exception');
});
type RejectionPayload = {
reason: unknown,
promise: Promise<unknown>,
};

const shutdownNicely = async (
err: Error | RejectionPayload,
kind: string
): Promise<void> => {
await pool.end();
logger.fatal(err, kind);
process.exit(1);
};

process.on(
'uncaughtException',
e => shutdownNicely(e, 'uncaughtException')
);

process.on(
'unhandledRejection',
(reason, promise) => shutdownNicely({ reason, promise }, 'unhandledRejection')
);

0 comments on commit 1158dce

Please sign in to comment.