Skip to content

Commit

Permalink
MySQL Connection SIGINT capture & graceful closure, additional settin…
Browse files Browse the repository at this point in the history
…gs to ensure connection is not closed.

Adding additional emergency settings to ensure that connection is not automatically set to closed after mysql server is exited. Further incorporating graceful shutdown mechanism to ensure that in the rare case that leftover connections remain, they are properly closed. (#168)
  • Loading branch information
siddheshraze authored Jun 26, 2024
1 parent f08735f commit 42ada06
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
36 changes: 35 additions & 1 deletion frontend/components/processors/processormacros.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ const sqlConfig: PoolOptions = {
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
keepAliveInitialDelay: 10000, // 0 by default.
enableKeepAlive: true, // false by default.
};
// database: process.env.AZURE_SQL_SCHEMA!, // better stored in an app setting such as process.env.DB_NAME
export const poolMonitor = new PoolMonitor(sqlConfig);
Expand Down Expand Up @@ -279,4 +281,36 @@ export function buildPaginatedQuery(config: QueryConfig): { query: string, param
queryParams.push(startRow, pageSize); // Ensure these are the last parameters added

return {query, params: queryParams};
}
}

// Function to close all active connections
async function closeConnections() {
console.log('Closing all active connections...');
await poolMonitor.closeAllConnections();
console.log('All connections closed.');
}

// Function to handle graceful shutdown
async function gracefulShutdown() {
console.log('Initiating graceful shutdown...');
try {
await closeConnections();
console.log('Graceful shutdown complete.');
process.exit(0);
} catch (error) {
console.error('Error during graceful shutdown:', error);
process.exit(1);
}
}

// Capture SIGINT signal (triggered by ctrl+c)
process.on('SIGINT', async () => {
console.log('SIGINT signal received.');
await gracefulShutdown();
});

// Capture SIGTERM signal (triggered by process kill)
process.on('SIGTERM', async () => {
console.log('SIGTERM signal received.');
await gracefulShutdown();
});
13 changes: 11 additions & 2 deletions frontend/config/poolmonitor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Pool, PoolConnection, PoolOptions, createPool} from 'mysql2/promise';
import { Pool, PoolConnection, PoolOptions, createPool } from 'mysql2/promise';

export class PoolMonitor {
private pool: Pool;
Expand Down Expand Up @@ -37,7 +37,6 @@ export class PoolMonitor {
});
}


async getConnection(): Promise<PoolConnection> {
try {
console.log('Requesting new connection...');
Expand All @@ -53,4 +52,14 @@ export class PoolMonitor {
getPoolStatus() {
return `active: ${this.activeConnections} | total: ${this.totalConnectionsCreated} | waiting: ${this.waitingForConnection}`;
}

async closeAllConnections(): Promise<void> {
try {
await this.pool.end();
console.log('All connections closed.');
} catch (error) {
console.error('Error closing connections:', error);
throw error;
}
}
}

0 comments on commit 42ada06

Please sign in to comment.