我将MikroORM与BullMQ工作人员结合使用。当MikroORM与Express应用程序一起使用时,它需要一个RequestContext,以便为每个请求维护唯一的身份映射。我怀疑在使用同一个BullMQ工作者处理多个作业时也需要同样的条件。
还有其他人成功地组合了这两个库吗?员工启动新作业时是否可以自动分叉实体管理器?
发布于 2021-12-03 13:54:07
对于这种情况,您可以使用@UseRequestContext()装饰器。它基本上就像在添加上下文的中间件之后执行该方法一样。注意,要使用它,this.orm需要是MikroORM实例。
@Injectable()
export class MyService {
constructor(private readonly orm: MikroORM) { }
@UseRequestContext()
async doSomething() {
// this will be executed in a separate context
}
}https://mikro-orm.io/docs/usage-with-nestjs/#request-scoped-handlers-in-queues
( docs的这一部分是关于nestjs的,但与相同的解决方案是完全相同的问题)
或者,您可以显式地使用RequestContext.createAsync():
await RequestContext.createAsync(orm.em, async () => {
// orm.em here will use the contextual fork created just for this handler
});https://stackoverflow.com/questions/70209851
复制相似问题