Files
accounts-manager/src/jobs/staleLockCleanup.ts
Your Name 891ae27689 feat: initial commit with accounts manager project structure
- TypeScript项目基础架构
- API路由和账户管理服务
- 数据库模式和迁移
- 基础配置文件和文档

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-23 01:42:50 +08:00

59 lines
1.6 KiB
TypeScript

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<number> {
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();