问题描述
我正在尝试在NestJS应用中使用斗牛竞技场.
I'm trying to use bull-arena in a NestJS app.
export class AppModule {
configure(consumer: MiddlewareConsumer) {
const queues = this.createArenaQueues();
const arena = Arena({ queues }, { disableListen: true });
consumer.apply(arena).forRoutes('/system/queues');
}
createArenaQueues() {
return [
{
name: 'Notification_Emailer',
hostId: 'MyAwesomeQueues',
url: 'redis://localhost',
},
];
}
}
这行得通!
但是由于从数据库中加载队列,我需要对createArenaQueues()
使用async/await.
But I need to use async/await for createArenaQueues()
, due to loading queues from db.
export class AppModule {
configure(consumer: MiddlewareConsumer) {
const queues = await this.createArenaQueues(); //await here
const arena = Arena({ queues }, { disableListen: true });
consumer.apply(arena).forRoutes('/system/queues');
}
async createArenaQueues() { // async here
return [
{
name: 'Notification_Emailer',
hostId: 'MyAwesomeQueues',
url: 'redis://localhost',
},
];
}
}
不起作用!
那么,问题是,如何处理呢?
So, the question, how to handle this?
从系统"控制器运行竞技场很酷,但我想不出一种方法.
It wold be cool to run arena from "System" controller, but I couldn't figure out a way to do it.
此外,我尝试将竞技场移至独立的中间件,但不知道我最终应该做些什么:返回类似res.send(arena)
之类的东西?
Also, I tried to move arena to standalone middleware, but dont know, what I should to do in the end: return something like res.send(arena)
or what?
处理此问题的最简单方法是async
配置支持,但未实现.
Easiest way to handle this, is async
configure support, but it not implemented.
推荐答案
您可以创建为您的arenaQueues
异步创建的自定义提供程序:
You can create a custom provider for your arenaQueues
that is created asynchronously:
将自定义提供程序添加到您的AppModule
的提供程序中:
Add the custom provider to your AppModule
's providers:
providers: [
ArenaService,
{
provide: 'arenaQueues',
useFactory: async (arenaService: ArenaService) => arenaService.createArenaQueues(),
inject: [ArenaService],
},
],
,然后将arenaQueues
注入您的AppModule
:
export class AppModule {
constructor(@Inject('arenaQueues') private queues) {}
configure(consumer: MiddlewareConsumer) {
// Now, you can use the asynchronously created this.queues here
const arena = Arena({ queues: this.queues }, { disableListen: true });
consumer.apply(arena).forRoutes('/system/queues');
}
这篇关于NestJS如何使用async/await配置中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!