feat(消息管理): 每小时执行改成每天上午9点执行消息处理

This commit is contained in:
huzhengrong 2025-10-23 20:19:44 +08:00
parent 924da331c7
commit a4cafe58f9
1 changed files with 39 additions and 4 deletions

View File

@ -35,10 +35,24 @@ class MessageQueue extends EventEmitter {
// 处理队列 // 处理队列
async startProcessor() { async startProcessor() {
setInterval(async () => { // 计算到明天上午9点的时间间隔
// 清除状态 不等于 pending的数据 const now = new Date();
console.log("开始处理队列"); const nextRun = new Date();
nextRun.setHours(9, 0, 0, 0); // 设置为今天的上午9点
// 如果当前时间已经过了今天的9点则设置为明天的9点
if (now > nextRun) {
nextRun.setDate(nextRun.getDate() + 1);
}
const timeUntilNextRun = nextRun - now;
console.log(`消息队列处理器将在 ${nextRun.toString()} 开始执行`);
// 第一次执行使用setTimeout
setTimeout(async () => {
try { try {
console.log("开始处理队列");
const pendingMessages = this.queue.getPendingMessages(); const pendingMessages = this.queue.getPendingMessages();
if (!this.processing && pendingMessages.length > 0) { if (!this.processing && pendingMessages.length > 0) {
await this.processQueue(pendingMessages); await this.processQueue(pendingMessages);
@ -46,7 +60,28 @@ class MessageQueue extends EventEmitter {
} catch (error) { } catch (error) {
console.error(`❌ 获取待处理消息失败:`, error); console.error(`❌ 获取待处理消息失败:`, error);
} }
}, 60 * 60 * 1000); // 1h处理一次
// 之后每天执行一次
this.scheduleDailyProcessing();
}, timeUntilNextRun);
}
// 每天上午9点执行消息处理
scheduleDailyProcessing() {
// 每天间隔24小时执行一次
setInterval(async () => {
try {
console.log("开始处理队列");
const pendingMessages = this.queue.getPendingMessages();
if (!this.processing && pendingMessages.length > 0) {
await this.processQueue(pendingMessages);
}
} catch (error) {
console.error(`❌ 获取待处理消息失败:`, error);
}
}, 24 * 60 * 60 * 1000); // 24小时
console.log("已设置每天上午9点执行消息处理");
} }
async processQueue(pendingMessages) { async processQueue(pendingMessages) {