gpt4 book ai didi

node.js - 如何使用 Bull 和 Node.js 通过后台作业处理函数?

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:01 26 4
gpt4 key购买 nike

我想知道如何通过 Queue/Bull 在我的函数“myCustomMethod”中运行代码。这是正确的方法吗?

./models/Sport.js

export async function myCustomMethod(type, req)
{
console.log("This method should be executed via the Queue / Bull");
let computationResult = true;
return computationResult;
}

cronCustomFile.js

import { myCustomMethod } from './models/Sport.js';

cron.schedule('*/5 * * * * *', () =>
{
var battleRoyaleQueue = new Queue('battle_royale_queue');
console.log('Checking live events every 5 seconds');
battleRoyaleQueue.process(function (job, done)
{
try
{
console.log('Processing via battle_royale_queue');
myCustomMethod('live-events');
done();
}
catch (err)
{
console.log(err.message);
}
});
return true;
});

公牛版本“牛”:“^3.6.0”

其他信息

看起来作业没有被添加到队列中或没有被处理。 enter image description here

引用

https://github.com/OptimalBits/bull

最佳答案

在您的代码中,您正在创建一个工作线程来每 5 秒处理一个作业,但您并没有创建要处理的作业。您应该创建一次工作线程,然后创建一个添加到队列并重复处理的作业。

config/bull.js

import Queue from 'bull';
import { myCustomMethod } from '../models/Sport.js';

var battleRoyaleQueue = new Queue('battle_royale_queue');
battleRoyaleQueue.process(function(job, done) {
const { type } = job.data
try {
console.log('Processing via battle_royale_queue');
myCustomMethod(type);
done();
} catch (err) {
console.log(err.message);
}
});

recurringJob.js

import battleRoyaleQueue from '../config/queues.js';

const beginReccuringJob = (type) => {
battleRoyaleQueue.add({type: type}, { repeat: { cron: '*/5 * * * * *' } });
};
beginReccuringJob('live-events');

关于node.js - 如何使用 Bull 和 Node.js 通过后台作业处理函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com