✨ feat(消息管理): 每小时执行改成每天上午9点执行消息处理
This commit is contained in:
parent
924da331c7
commit
a4cafe58f9
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue