Fixed Bug Server
## Summary This branch contains several bug fixes and performance improvements, primarily focusing on: - Database connection management - MQTT client stability - Logging optimization - API enhancements ## Detailed Changes ### Fixed Issues 1. **Database Connection Management** - Removed from user-validate API route to prevent connection pool exhaustion - Added proper connection handling in global Prisma setup - Reduced logging verbosity in production environments 2. **MQTT Client Improvements** - Enhanced MQTT client initialization with proper error handling - Added reconnection logic with configurable intervals - Implemented cleanup functions to prevent memory leaks - Added separate initialization logic for server and client-side code 3. **Logging Optimization** - Removed excessive logging in middleware that was causing high CPU usage - Configured appropriate log levels for development and production 4. **Component Stability** - Added safety checks in text editor component to prevent MQTT operations on the server side - Improved MQTT publishing logic with client availability checks ### New Files - - Utility functions for safe database operations ### Modified Files 1. - Removed problematic call 2. - Configured different logging levels for dev/prod - Removed process listeners that were causing disconnections - Exported prisma instance separately 3. - Removed excessive logging statements 4. - Enhanced initialization with error handling - Added reconnection and timeout configurations 5. - Added proper cleanup functions - Improved connection handling 6. - Added MQTT client availability checks - Prevented server-side MQTT operations ### Performance Improvements - Reduced database connection overhead - Optimized MQTT connection handling - Eliminated unnecessary logging in production - Better memory management with proper cleanup functions ### No Issue
This commit is contained in:
@@ -9,30 +9,21 @@ declare global {
|
||||
let prisma: PrismaClient;
|
||||
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
prisma = new PrismaClient();
|
||||
prisma = new PrismaClient({
|
||||
// Reduce logging in production to improve performance
|
||||
log: ['error', 'warn'],
|
||||
});
|
||||
} else {
|
||||
if (!global.prisma) {
|
||||
global.prisma = new PrismaClient();
|
||||
global.prisma = new PrismaClient({
|
||||
log: ['error', 'warn', 'info', 'query'], // More verbose logging in development
|
||||
});
|
||||
}
|
||||
prisma = global.prisma;
|
||||
}
|
||||
|
||||
// Tambahkan listener hanya jika belum ditambahkan sebelumnya
|
||||
if (!global.prismaListenersAdded) {
|
||||
// Handle uncaught errors
|
||||
process.on("uncaughtException", async (error) => {
|
||||
console.error("Uncaught Exception:", error);
|
||||
await prisma.$disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Handle unhandled promise rejections
|
||||
process.on("unhandledRejection", async (error) => {
|
||||
console.error("Unhandled Rejection:", error);
|
||||
await prisma.$disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Handle graceful shutdown
|
||||
process.on("SIGINT", async () => {
|
||||
console.log("Received SIGINT signal. Closing database connections...");
|
||||
@@ -51,3 +42,4 @@ if (!global.prismaListenersAdded) {
|
||||
}
|
||||
|
||||
export default prisma;
|
||||
export { prisma };
|
||||
|
||||
24
src/lib/prismaUtils.ts
Normal file
24
src/lib/prismaUtils.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { prisma } from './prisma';
|
||||
|
||||
/**
|
||||
* Utility function to safely execute Prisma operations
|
||||
* This prevents improper disconnection of the Prisma client
|
||||
* which was causing high CPU usage and connection pool issues
|
||||
*/
|
||||
export async function executeDbOperation<T>(
|
||||
operation: () => Promise<T>,
|
||||
errorMessage: string = "Database operation failed"
|
||||
): Promise<{ success: boolean; data?: T; error?: string }> {
|
||||
try {
|
||||
const data = await operation();
|
||||
return { success: true, data };
|
||||
} catch (error) {
|
||||
console.error(errorMessage, error);
|
||||
return { success: false, error: (error as Error).message };
|
||||
}
|
||||
// Note: We intentionally do NOT call prisma.$disconnect() here
|
||||
// Prisma manages connection pooling automatically and disconnecting
|
||||
// on each request causes performance issues
|
||||
}
|
||||
|
||||
export { prisma };
|
||||
Reference in New Issue
Block a user