feat: Implement automatic database cleanup feature with configuration options and API support

This commit is contained in:
Arunavo Ray
2025-05-24 18:33:59 +05:30
parent d7ce2a6908
commit 47e1c7b493
12 changed files with 667 additions and 76 deletions

View File

@@ -1,9 +1,11 @@
import { defineMiddleware } from 'astro:middleware';
import { initializeRecovery, hasJobsNeedingRecovery, getRecoveryStatus } from './lib/recovery';
import { startCleanupService } from './lib/cleanup-service';
// Flag to track if recovery has been initialized
let recoveryInitialized = false;
let recoveryAttempted = false;
let cleanupServiceStarted = false;
export const onRequest = defineMiddleware(async (context, next) => {
// Initialize recovery system only once when the server starts
@@ -53,6 +55,18 @@ export const onRequest = defineMiddleware(async (context, next) => {
}
}
// Start cleanup service only once after recovery is complete
if (recoveryInitialized && !cleanupServiceStarted) {
try {
console.log('Starting automatic database cleanup service...');
startCleanupService();
cleanupServiceStarted = true;
} catch (error) {
console.error('Failed to start cleanup service:', error);
// Don't fail the request if cleanup service fails to start
}
}
// Continue with the request
return next();
});