Resolve production issues by implementing a PostgreSQL backup module and fixing ESM compatibility

Replace `node-cron` with native `setTimeout` in `backupService.ts` to address dynamic require errors in ESM, and introduce a PostgreSQL backup module with administrative interface and automated scheduling.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7c468936-2a88-4686-ac0a-84921a45222d
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/7c468936-2a88-4686-ac0a-84921a45222d/fhCGgjv
This commit is contained in:
michaelschal committed 2025-08-13 21:01:25 +00:00
1 parent fbeb36c04a
commit 1c126fb77a
3 files changed
+233 -12

No files matched your search

+27 -12
View File
@@ -2,7 +2,6 @@ import { exec } from 'child_process';
import { promisify } from 'util';
import fs from 'fs';
import path from 'path';
import cron from 'node-cron';
import { nanoid } from 'nanoid';
import { eq, desc } from "drizzle-orm";
import { db } from "./db";
@@ -187,19 +186,35 @@ export class BackupService {
}
private scheduleAutomaticBackup(): void {
// Schedule backup every night at 2:00 AM
cron.schedule('0 2 * * *', async () => {
try {
console.log('🌙 Starting scheduled automatic backup...');
await this.createBackup('automatic');
} catch (error) {
console.error('❌ Scheduled backup failed:', error);
// Use native setTimeout instead of node-cron for ESM compatibility
const scheduleNextBackup = () => {
const now = new Date();
const next2AM = new Date(now);
next2AM.setHours(2, 0, 0, 0);
// If it's already past 2 AM today, schedule for tomorrow
if (now.getHours() >= 2) {
next2AM.setDate(next2AM.getDate() + 1);
}
}, {
timezone: 'Europe/Paris'
});
const msUntilBackup = next2AM.getTime() - now.getTime();
setTimeout(async () => {
try {
console.log('🔄 Starting automatic backup...');
await this.createBackup('automatic', 'system');
console.log('✅ Automatic backup completed');
} catch (error) {
console.error('❌ Automatic backup failed:', error);
}
// Schedule the next backup (24 hours later)
scheduleNextBackup();
}, msUntilBackup);
};
console.log('⏰ Automatic backup scheduled for 2:00 AM daily');
scheduleNextBackup();
console.log('⏰ Automatic backup scheduled for 2:00 AM daily (native timer)');
}
}