https://optimalbits.github.io/bull/
https://github.com/OptimalBits/bull?tab=readme-ov-file#documentation
https://xknow.net/introduce-bull-queue/
nestjs: 可以用nestjs直接封装好的注入、注解方法
bull:自己new出来存放起来自己提供使用也没有问题
bull底层用redis,会维护一个长连接
/**
Gets or creates a new Queue with the given name.
The Queue keeps 6 data structures:
- wait (list)
- active (list)
- delayed (zset)
- priority (zset)
- completed (zset)
- failed (zset)
--> priorities -- > completed
/ | /
job -> wait -> active
\\ ^ \\
v | -- > failed
delayed
*/
Delayed jobs are jobs that cannot be executed until a certain time in ms has passed since they were added to the queue. The mechanism is simple, a delayedTimestamp variable holds the next known timestamp that is on the delayed set (or MAX_TIMEOUT_MS if none).
When the current job has finalized the variable is checked, if no delayed job has to be executed yet a setTimeout is set so that a delayed job is processed after timing out.
延迟作业是指自添加到队列以来,在经过以毫秒为单位的特定时间之前无法执行的作业。该机制很简单,delayedTimestamp变量保存延迟集上的下一个已知时间戳(如果没有,则保存MAX_TIMEOUT_MS)。
当当前作业完成时,检查变量,如果还没有延迟的作业需要执行,则设置setTimeout,以便在超时后处理延迟的作业。
左侧涂抹掉的事队列名称
右侧是数据
队列是一种用于处理异步任务的重要工具,它允许您将任务添加到队列中,然后按照优先级或顺序依次执行。队列可以在高峰时段做到削峰的事情, 先接收任务, 然后依次处理. 而不会在同一时间因为访问量过大让服务器连接不上. 在官方文档里有队列一文档说的很详细了, 不过真按着来做, 也不一定能配置好. 有些细节, 文档里并没有说的明白. 以下算是那个文档的小补充吧
主要是用了redis的Pub/Sub功能