nodejs bull 实现延时队列
nodejs bull 实现延时队列
bull.js
const Queue = require('bull'); const queue = new Queue('nike', { redis: { port: 6379, host: '127.0.0.1', db: 3, password: null }, prefix: 'nike_', defaultJobOptions: { attempts: 1, removeOnComplete: true, backoff: false, delay: 0 }, limiter: { max: 200000, duration: 1000 }, settings: { maxStalledCount: 1, guardInterval: 1, // 重新调度延迟 retryProcessDelay: 500, // delay before processing next job in case of internal error. // drainDelay: 50000 // 空队列时brpoplpush的等待时间 } }); module.exports = queue;
生产者
const queue = require('./bull'); const random = require("random-string") var log4js = require("log4js"); var logger = log4js.getLogger(); logger.level = "info"; queue.on('global:progress', function(jobId, progress) { logger.info(`Job ${jobId} is ${progress * 100}% ready!`); }); queue.on('global:completed', jobId => { logger.info(`Job with id ${jobId} has been completed`); }) const main = async () => { for(let i=0;i<10;i++){ const job = await queue.add({ key: random(10) },{ delay:5000 }); logger.info("生产者:",job.data,await queue.count()) } } main()
消费者
const queue = require('./bull'); var log4js = require("log4js"); var logger = log4js.getLogger(); logger.level = "info"; const main = async () => { queue.process(async (job) => { logger.info('消费者:',job.data); await new Promise(r => setTimeout(r,1000)) return Promise.resolve(); }); } main()
文档:
https://github.com/OptimalBits/bull/tree/develop/docs
api:
https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queueprocess
漫思
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
2023-12-15 SwiftUI 教程
2023-12-15 when(C# 参考)
2023-12-15 where(泛型类型约束)(C# 参考)
2023-12-15 C#where关键字约束
2023-12-15 百度工程师移动开发避坑指南——Swift语言篇
2023-12-15 盘点 swift 中 where 关键字的所有用法,你知道多少?
2023-12-15 C#11的字符串语法