This commit is contained in:
2025-05-01 03:59:37 +08:00
parent 26ca4c21c8
commit e58adbc46b
45 changed files with 7460 additions and 87 deletions

52
src/task/worker.ts Normal file
View File

@@ -0,0 +1,52 @@
import { redis } from '@/modules/redis.ts';
import { Queue, Worker } from 'bullmq';
import { clamp } from 'lodash-es';
import { nanoid } from 'nanoid';
const XHS_QUEUE_NAME = 'XHS_QUEUE';
export const queue = new Queue(XHS_QUEUE_NAME);
export const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const worker = new Worker(
XHS_QUEUE_NAME,
async (job) => {
const startTime = Date.now();
console.log('job', job.name, job.data);
await sleep(1000);
const endTime = Date.now();
const duration = endTime - startTime;
return {
startTime: startTime,
endTime: endTime,
duration: duration,
};
},
{ connection: redis, concurrency: 1 },
);
worker.on('completed', async (job) => {
console.log('job completed', job.name, job.id, job.returnvalue);
const duration = job.returnvalue.duration || 0;
const maxNextTime = 20 * 1000; // 5 minutes
const nextTime = clamp(maxNextTime - duration, 0, maxNextTime);
const hasJobs = await queue.getJobCounts('waiting', 'wait', 'delayed');
console.log('hasJobs', nextTime, 'joblenght', hasJobs);
if (hasJobs.delayed + hasJobs.wait > 0) {
console.log('======has jobs, no need to add new job');
} else {
const id = nanoid();
queue.add(
'repeact-call-job' + id,
{},
{
delay: nextTime,
removeOnComplete: true,
removeOnFail: {
age: 24 * 3600, // keep up to 24 hours
},
jobId: id,
},
);
}
});