const Logger = require('../Logger') const { TaskScheduler } = require('./TaskScheduler') class LeaseWatcher { constructor(options = {}) { this.intervalMs = options.intervalMs || 30 * 1000 this.logger = options.logger || new Logger() this.scheduler = options.scheduler || new TaskScheduler() this.timer = null this.running = false } async tick() { if (this.running) { this.logger.warn('[QK][LeaseWatcher] 上一轮巡检尚未结束,跳过本次执行') return } this.running = true try { const count = await this.scheduler.requeueExpiredTasks() if (count > 0) { this.logger.info(`[QK][LeaseWatcher] 已将 ${count} 个失联任务重新入队`) } } catch (err) { this.logger.error(`[QK][LeaseWatcher] 执行失败:${err.stack || err}`) } finally { this.running = false } } start() { if (this.timer) { return this.timer } this.logger.info(`[QK][LeaseWatcher] 已启动,巡检间隔 ${this.intervalMs}ms`) this.tick() this.timer = setInterval(() => this.tick(), this.intervalMs) return this.timer } stop() { if (this.timer) { clearInterval(this.timer) this.timer = null this.logger.info('[QK][LeaseWatcher] 已停止') } } } module.exports = LeaseWatcher