I found that I cannot add elements to a list using lpush when using this component in the Nestjs project.
我在nestjs项目中使用该组件时发现无法使用 lpush向列表中添加元素。
I implemented a message queue using a list in Nestjs
我在nestjs中使用list实现了一个消息队列
async onApplicationBootstrap() {
this.logger.log("应用生命周期钩子 onApplicationBootstrap");
this.cacheRefreshHandler();
}
async cacheRefreshHandler() {
while (true) {
let res = null
try {
res = await this.redis.brpop('bff_msg_queue', 0)
this.logger.log('>>>>>>>>收到消息:', JSON.stringify(res))
//消息处理任务
if (res !== null && res[1]) {
//.code
}
} catch (err) {
console.log('brpop 出错,重新brpop', err)
continue
}
}
}
Finally used in the project's service
最后在项目的service中使用
import Redis from 'ioredis';
import { InjectRedis } from '@nestjs-modules/ioredis';
@Resolver('Article')
export class ArticleResolver {
private logger = new Logger(ArticleResolver.name);
constructor(
@InjectRedis() private readonly redis: Redis,
) { }
async method(tid){
// The execution of the following line of code is blocked and cannot return
await this.redis.lpush("bff_msg_queue", JSON.stringify({ "event": "update_article_info", "tid": tid }));
}
}
When I call the method() method, the execution keeps waiting for lpush to complete, and the program does not continue to execute.
当我调用 method() 方法的时候,执行一直在等待lpush执行完成,程序不在往下执行。
Just a quick hunch, but could it be because brpop is blocking, and then the same client cannot do the lpush? Also, cacheRefreshHandler (and the blocking) is not awaited in onApplicationBootstrap.
I found that I cannot add elements to a list using lpush when using this component in the Nestjs project. 我在nestjs项目中使用该组件时发现无法使用 lpush向列表中添加元素。 I implemented a message queue using a list in Nestjs 我在nestjs中使用list实现了一个消息队列
项目启动时:
Finally used in the project's service 最后在项目的service中使用
When I call the method() method, the execution keeps waiting for lpush to complete, and the program does not continue to execute. 当我调用 method() 方法的时候,执行一直在等待lpush执行完成,程序不在往下执行。