cron[Node.js]中未触发布尔作业



我有多个后台作业处理器,使用如下所示的bull包设置

import { CronJob } from 'cron';
import Queue from 'bull';
let queue = new Queue('workers', {
//  settings: { lockDuration: 60 * 20000 },
defaultJobOptions: {
removeOnComplete: true
},
});

queue
.on('waiting', function(jobId) {
// A Job is waiting to be processed as soon as a worker is idling.
// workerLogger.info(`Job ${jobId} waiting to be processed `);
})
.on('completed', async(job, result) => {
workerLogger.info(`Job ID: ${job.id}, Result: ${result}`);
try {
const jobbed = await queue.getJob(job.id);
if (jobbed) {
await jobbed.remove();
workerLogger.info(`removed completed job ${job.id}`);
}
} catch (error) {
throw new Error(error);
}
})
.on('failed', function(job, err) {
workerLogger.error('job ' + job.id + ' in queue failed... ' + err);
})
.on('error', function(err) {
workerLogger.error('Queue Error... ' + err);
})
.on('stalled', function(job) {
workerLogger.info(
`stalled job, restarting it again! ${job.queue.name} ${JSON.stringify(
job.data,
)} ${job.id} ${job.name}`,
);
});
queue.process('healthCheckPing', concurrency, function(job, done) {
jobs.healthCheckPing(job.data, done);
});
queue.process('test', concurrency, function(job, done) {
jobs.test(job.data, done);
});

我尝试使用cron包运行基于Crontime的作业,但只有一个作业得到处理,请检查下面的示例

const cron = new CronJob({
cronTime: '* * * * *',
onTick: function() {
(() => {
workerLogger.info('Pushing test to queue...');
queue.add('test');
queue.add(
'healthCheckPing',
{
jobName: 'test',
},
);
})();
},
start: true,
timeZone: 'Africa/Lagos',
});

我尝试了很多方法来让它发挥作用,但似乎都不起作用,我会在下面列出一些

  • 运行等待队列.擦除({force:true}(
  • 在单独的实例上运行工作处理器
  • 向作业-队列添加延迟。add('test',{},{delay:500}(
  • 向作业-队列添加优先级。add('test',{},{priority:1}(

在上面的两个作业(测试、healthCheckPing(中,只有一个总是被触发。

也就是说,名为test的作业被处理,而另一个作业没有被处理,或者名为healthCheckPing的作业被执行,而另另一个不是

这是下方的作业处理器功能

const jobs = {};
jobs.test = (_, done) => {
try {
workerLogger.error('test');
done(false, 'ok');
} catch (e) {
done(e);
}
};
jobs.healthCheckPing = async({
jobName
}, done) => {
try {
workerLogger.info('health check pinger');
if (!jobName) throw new Error('uuid not passed');
// jobname is the slug for monitor
// use pingkey to negate between staging and prod monitors
const pingKey = !process.env.NODE_ENV || process.env.NODE_ENV !== 'production' ?
process.env.STAGING_HEALTH_CHECK_KEY :
process.env.PROD_HEALTH_CHECK_KEY;
const url = `https://hc-ping.com/${pingKey}/${jobName}`;
await axios.get(url);
done(false, `pinged ${jobName}!`);
} catch (error) {
done(error);
}
};
export default jobs;

此代码在我的本地机器上运行良好,但仅此问题在生产时发生。节点服务器使用pm2(集群模式,实例=1(运行

问题是因为我在另一个共享相同Redis连接的服务中使用了相同的队列名称,

let queue = new Queue('workers', {
//  settings: { lockDuration: 60 * 20000 },
defaultJobOptions: {
removeOnComplete: true
},
});

工作人员更改为其他人员修复了问题

相关内容

  • 没有找到相关文章

最新更新