将 BullMQ 与 NestJs 一起使用,是否可以将配置变量用作作业名称的一部分?

Using BullMQ with NestJs, is it possible to use a config variable as part of a Job name?

我正在尝试在 nestjs/bull 模块的 @Process() 装饰器中使用环境变量值,如下所示。我应该如何提供 'STAGE' 变量作为作业名称的一部分?

import { Process, Processor } from '@nestjs/bull';
import { Inject } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Job } from 'bull';

@Processor('main')
export class MqListener {

  constructor(
    @Inject(ConfigService) private configService: ConfigService<SuperRootConfig>,
  ) { }

  // The reference to configService is not actually allowed here:
  @Process(`testjobs:${this.configService.get('STAGE')}`)
  
  handleTestMessage(job: Job) {
    console.log("Message received: ", job.data)
  }
}

已编辑 Micael 和 Jay 的回答(如下):

Micael Levi 回答了最初的问题:您不能使用 NestJS ConfigModule 将您的配置放入内存变量中。但是,bootstrap 函数中的 运行 dotenv.config() 也不起作用;如果您尝试从方法装饰器中访问内存变量,您会得到未定义的值。为了解决这个问题,Jay McDoniel 指出您必须在导入 AppModule 之前导入文件。所以这有效:

// main.ts
import { NestFactory } from '@nestjs/core';
require('dotenv').config()
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT || 4500);
}
bootstrap();

由于 decorator evaluation 的工作方式,您不能在该上下文中使用 this。那时,没有为 MqListener class 创建实例,因此,使用 this.configService 没有意义。

您需要直接访问 process.env.。因此将在该文件中调用 dotenv(或读取和解析您的点环境文件的库)。