generated from tailored/router-template
temp
This commit is contained in:
55
src/task/test/worker.ts
Normal file
55
src/task/test/worker.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { redis } from '@/modules/redis.ts';
|
||||
import { Queue, Worker } from 'bullmq';
|
||||
import { clamp } from 'lodash-es';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
export const queue = new Queue('TEST_QUEUE');
|
||||
export const sleep = (ms: number) => {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
};
|
||||
export const worker = new Worker(
|
||||
'TEST_QUEUE',
|
||||
async (job) => {
|
||||
const startTime = Date.now();
|
||||
console.log('job', job.name, job.data);
|
||||
await sleep(1000);
|
||||
const endTime = Date.now();
|
||||
const duration = endTime - startTime;
|
||||
job.updateProgress(50);
|
||||
const isRetry = job.attemptsMade > 0;
|
||||
console.log('job attemptsMade', job.attemptsMade, 'isRetry', isRetry);
|
||||
if (job.data.index === 1 && !isRetry) {
|
||||
// set fail
|
||||
throw new Error('test error');
|
||||
}
|
||||
return {
|
||||
startTime: startTime,
|
||||
endTime: endTime,
|
||||
duration: duration,
|
||||
};
|
||||
},
|
||||
{
|
||||
connection: redis,
|
||||
limiter: {
|
||||
max: 1,
|
||||
duration: 10000,
|
||||
},
|
||||
},
|
||||
);
|
||||
worker.on('completed', async (job) => {
|
||||
console.log('job completed', job.name, job.id, job.returnvalue);
|
||||
});
|
||||
worker.on('failed', async (job) => {
|
||||
console.log('job failed', job.name, job.id, job.failedReason, await job.getState());
|
||||
});
|
||||
|
||||
queue.pause();
|
||||
await queue.drain();
|
||||
const arr = Array.from({ length: 2 }, (_, i) => i + 1);
|
||||
for (const i of arr) {
|
||||
queue.add('job' + i, { name: 'test' + i, index: i }, { jobId: 'job' + i, removeOnFail: true, removeOnComplete: true, attempts: 3 });
|
||||
}
|
||||
const job = await queue.getJob('job1');
|
||||
console.log('job', job);
|
||||
await sleep(4000)
|
||||
queue.resume();
|
||||
Reference in New Issue
Block a user