From a4cafe58f95f2e347a7186c02d2e74a330b0501a Mon Sep 17 00:00:00 2001 From: huzhengrong Date: Thu, 23 Oct 2025 20:19:44 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(=E6=B6=88=E6=81=AF=E7=AE=A1?= =?UTF-8?q?=E7=90=86):=20=E6=AF=8F=E5=B0=8F=E6=97=B6=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=94=B9=E6=88=90=E6=AF=8F=E5=A4=A9=E4=B8=8A=E5=8D=889?= =?UTF-8?q?=E7=82=B9=E6=89=A7=E8=A1=8C=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- msgManager.js | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/msgManager.js b/msgManager.js index 3d75e05..7cfbb5e 100644 --- a/msgManager.js +++ b/msgManager.js @@ -35,10 +35,24 @@ class MessageQueue extends EventEmitter { // 处理队列 async startProcessor() { - setInterval(async () => { - // 清除状态 不等于 pending的数据 - console.log("开始处理队列"); + // 计算到明天上午9点的时间间隔 + const now = new Date(); + 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 { + console.log("开始处理队列"); const pendingMessages = this.queue.getPendingMessages(); if (!this.processing && pendingMessages.length > 0) { await this.processQueue(pendingMessages); @@ -46,7 +60,28 @@ class MessageQueue extends EventEmitter { } catch (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) {