New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: non-blocking version of getNextJob #516
Labels
Comments
drainDelay 0 means block for ever as per Redis semantics. It would be quite easy though to add an opts parameter with "block: boolean". |
// bullMQUtils.ts
import { delay, Job, Queue, Worker } from "bullmq";
/**
// 调用手动队列处理函数
const token = nanoid();
const { queue: manuallyQueue, worker } = processManualQueue(
"manuallyQueue",
{
connection: connection,
concurrency: 5,
delayTime: 3000,
successCallback: async (data) => {
if (data.video == 3) return false;
return true;
},
},
token
);
*/
export function processManualQueue(queueName, options, token) {
const { connection, concurrency, delayTime, successCallback } = options;
const queue = new Queue(queueName, { connection });
const worker = new Worker(queueName, null, {
connection,
concurrency: concurrency || 5,
});
workerRun();
async function workerRun() {
let job;
while (true) {
await delay(delayTime || 3000);
let jobData = null;
let jobId;
let success;
if (job) {
success = await successCallback(job.data);
// console.log("处理手动作业", job.data, "状态", success);
if (success) {
[jobData, jobId] = await job.moveToCompleted(
"some return value",
token
);
} else {
await job.moveToFailed(new Error("some error message"), token);
}
if (jobData) {
job = Job.fromJSON(worker, jobData, jobId);
} else {
job = null;
}
} else {
if (!job) {
job = await worker.getNextJob(token);
}
}
}
}
return { queue, worker };
} // 调用手动队列处理函数
const token = nanoid();
const { queue: manuallyQueue, worker } = processManualQueue(
"manuallyQueue",
{
connection: connection,
concurrency: 5,
delayTime: 3000,
successCallback: async (data) => {
if (data.video == 3) return false;
return true;
},
},
token
); // 手动作业, 失败重试
async manuallyQueue() {
// console.log('触发了-test_queue_1');
// manuallyQueue.add("jobName", { video: 1 });
// manuallyQueue.add("jobName", { video: 2 });
for (let i = 1; i < 6; i++) {
manuallyQueue.add(
"jobName",
{ video: i },
{
attempts: 3, // 重试3次
backoff: {
type: "exponential",
delay: 3000, // 1秒后重试
},
}
);
}
}, |
thewilkybarkid
added a commit
to PREreview/coar-notify
that referenced
this issue
Apr 8, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looking for ways to compose workers in a more elaborate way, e.g. to add priority, or throttling, or round-robin balancing between workers, e.g.:
When trying this out, the blocking behavior of
worker.getNextJob
makes this use-case impossible. It would be helpful if there was a non-blocking version ofgetNextJob
that just returns immediately. Perhaps when settingdrainDelay
to0
?The text was updated successfully, but these errors were encountered: