import { accountService } from '../core/AccountService'; export class StaleLockCleanup { private intervalId: NodeJS.Timeout | null = null; private isRunning = false; start(intervalMinutes: number = 1): void { if (this.isRunning) { console.log('Stale lock cleanup job is already running'); return; } this.isRunning = true; const intervalMs = intervalMinutes * 60 * 1000; console.log(`Starting stale lock cleanup job with ${intervalMinutes} minute(s) interval`); this.intervalId = setInterval(async () => { try { const cleanedCount = await accountService.cleanupStaleLocks(); if (cleanedCount > 0) { console.log(`Stale lock cleanup: Released ${cleanedCount} locked accounts`); } } catch (error) { console.error('Error during stale lock cleanup:', error); } }, intervalMs); this.runOnce(); } stop(): void { if (this.intervalId) { clearInterval(this.intervalId); this.intervalId = null; } this.isRunning = false; console.log('Stale lock cleanup job stopped'); } async runOnce(): Promise { try { const cleanedCount = await accountService.cleanupStaleLocks(); if (cleanedCount > 0) { console.log(`Manual stale lock cleanup: Released ${cleanedCount} locked accounts`); } return cleanedCount; } catch (error) { console.error('Error during manual stale lock cleanup:', error); throw error; } } isJobRunning(): boolean { return this.isRunning; } } export const staleLockCleanup = new StaleLockCleanup();