Skip to content

Commit 55e7138

Browse files
committed
refactor(error): improve database connection health check
1 parent 44fb10c commit 55e7138

5 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/app.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ async function main() {
2828
});
2929

3030
process.on('unhandledRejection', (reason) => {
31-
console.error('Unhandled Rejection:', reason);
31+
logger.error(`${reason}:`, 'Unhandled Rejection');
3232
});
3333

34-
process.on('uncaughtException', async (err) => {
35-
console.error('Uncaught Exception:', err);
34+
process.on('uncaughtException', async (err: any) => {
35+
logger.error(`${err}:`, 'Uncaught Exception');
3636
await shutdown(botInstance, 1);
3737
});
3838
}
@@ -41,7 +41,7 @@ async function shutdown(botInstance: CopBot, exitCode = 0) {
4141
try {
4242
const db = ServiceProvider.getInstance();
4343
await db.close();
44-
logger.info('Database closed.');
44+
logger.warn('Database closed.');
4545
await botInstance.stop();
4646
} catch (err) {
4747
console.error('Error during shutdown:', err);

src/bot/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,19 @@ export class CopBot {
6262
}
6363
});
6464
app.get('/health', async (_, res) => {
65-
const isHealthy = await ServiceProvider.getInstance().healthCheck();
66-
res.status(isHealthy ? 200 : 500).send(isHealthy ? 'OK' : 'Database not reachable');
65+
try {
66+
const isHealthy = await ServiceProvider.getInstance().healthCheck();
67+
if (isHealthy) {
68+
res.status(200).send('Database is healthy');
69+
logger.info('Database connection is healthy.');
70+
} else {
71+
res.status(500).send('Database not reachable');
72+
logger.error('Database not reachable.');
73+
}
74+
} catch (error: any) {
75+
res.status(500).send('Database connection error');
76+
logger.error(`Database health check failed: ${error.message}`);
77+
}
6778
});
6879
const port = process.env.PORT || 3000;
6980
app.listen(port, async () => {

src/database/ConnectionPool.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export class ConnectionPool {
7272
return new Pool({
7373
connectionString,
7474
ssl: this._isProduction === 'production' ? { rejectUnauthorized: false } : false,
75+
connectionTimeoutMillis: 500,
76+
max: 10,
77+
idleTimeoutMillis: 500,
7578
});
7679
}
7780
}

src/database/models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { User } from '../../types/database/TablesTypes';
33
import { DatabaseService } from '../service/Database';
44
export class UserService {
55
private _db: DatabaseService;
6-
constructor(private _client: PoolClient) {
6+
constructor(_client: PoolClient) {
77
this._db = new DatabaseService(_client);
88
}
99
async create(user: Omit<User, 'id'>): Promise<User> {

src/service/database/ServiceProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class ServiceProvider {
5858
async healthCheck(): Promise<boolean> {
5959
try {
6060
const client = await this.getPoolClint();
61+
await client.query('SELECT NOW()');
6162
client.release();
6263
console.log('Database is healthy.');
6364
return true;

0 commit comments

Comments
 (0)