diff --git a/src/task/utils/time.ts b/src/task/utils/time.ts new file mode 100644 index 0000000..5c25ca8 --- /dev/null +++ b/src/task/utils/time.ts @@ -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; +}; diff --git a/src/task/worker.ts b/src/task/worker.ts index f88e94f..86b4798 100644 --- a/src/task/worker.ts +++ b/src/task/worker.ts @@ -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); diff --git a/src/test/get-time-duration.ts b/src/test/get-time-duration.ts new file mode 100644 index 0000000..3110240 --- /dev/null +++ b/src/test/get-time-duration.ts @@ -0,0 +1,3 @@ +import { getTimeDuration } from '@/task/utils/time.ts'; + +console.log('getTimeDuration', getTimeDuration());