update time fix

This commit is contained in:
熊潇 2025-06-22 19:41:01 +08:00
parent 7bc9a06b7c
commit e982ddb001
3 changed files with 71 additions and 5 deletions

54
src/task/utils/time.ts Normal file
View File

@ -0,0 +1,54 @@
import dayjs from 'dayjs';
/**
*
*
* 03:01 - 09:00 120000 (120)
* 0
* @returns
*/
export const getTimeDuration = () => {
// 根据时间返回,返回需要
const timeRangeList = [
{
start: '03:01',
end: '09:00',
duration: 240 * 1000, // 240s
},
{
start: '09:01',
end: '12:00',
duration: 60 * 1000, // 60s
},
{
start: '12:01',
end: '14:00',
duration: 20 * 1000, // 20s
},
{
start: '14:01',
end: '18:00',
duration: 120 * 1000, // 120s
},
{
start: '18:01',
end: '23:59',
duration: 10 * 1000, // 10s
},
{
start: '00:01',
end: '03:00',
duration: 20 * 1000, // 20s
},
];
const currentTime = Date.now();
const currentHour = dayjs(currentTime).format('HH:mm');
for (const range of timeRangeList) {
if (currentHour >= range.start && currentHour <= range.end) {
return range.duration;
}
}
// 如果没有匹配到默认返回0
return 0;
};

View File

@ -5,6 +5,7 @@ import { nanoid } from 'nanoid';
import { queue, XHS_QUEUE_NAME, taskApp } from './index.ts';
import { addUnreadTask } from './task.ts';
import dayjs from 'dayjs';
import { getTimeDuration } from './utils/time.ts';
export const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
@ -13,7 +14,7 @@ class TimeRecorder {
endTime: number;
duration: number;
updateTime: number;
maxDuration: number = 60 * 1000; // 30s;
maxDuration: number = 40 * 1000; // 30s;
constructor() {
const now = Date.now();
this.startTime = now;
@ -34,8 +35,12 @@ class TimeRecorder {
this.updateTime = Date.now();
return this.updateTime;
}
getClampDuration() {
const duration = Date.now() - this.updateTime;
getClampDuration(random = false) {
let randomDuration = 0;
if (random) {
randomDuration = Math.floor(Math.random() * 5) * 1000; // 随机0-5秒
}
const duration = Date.now() - this.updateTime + randomDuration;
const nextTime = clamp(this.maxDuration - duration, 0, this.maxDuration);
// console.log('getClampDuration', duration, this.maxDuration, 'nextTime', nextTime);
@ -55,6 +60,7 @@ class TimeRecorder {
};
}
}
const timeRecorder = new TimeRecorder();
let errorCount = 0;
export const worker = new Worker(
@ -75,6 +81,9 @@ export const worker = new Worker(
if (errorCount > 3) {
queue.pause();
console.log('error count', errorCount);
if (data.path === 'task' && data.key === 'getUnread') {
process.exit(1);
}
}
throw new Error('job error' + job.name + ' ' + job.id);
}
@ -92,8 +101,8 @@ worker.on('completed', async (job) => {
if (jobCounts.delayed + jobCounts.wait > 0) {
// console.log('======has jobs, no need to add new job');
} else {
const up = timeRecorder.getClampDuration();
const nextTime = up.nextTime;
const up = timeRecorder.getClampDuration(true);
const nextTime = up.nextTime + getTimeDuration();
const unread = await queue.getJob('unread');
if (!unread) {
addUnreadTask(nextTime);

View File

@ -0,0 +1,3 @@
import { getTimeDuration } from '@/task/utils/time.ts';
console.log('getTimeDuration', getTimeDuration());