NodeJS 无法注册cron作业,因为它是在非静态提供程序中定义的- NestJS

s4n0splo  于 2023-06-05  发布在  Node.js
关注(0)|答案(1)|浏览(250)

我正在创建一个工人,将执行数据库操作及时。当我定义我的private readonly validationRuleRepository: ValidationRulesRepository,时,它似乎没有注册cron。
ValidationRulesRepository的作用域也是Scope.DEFAULT

@Injectable({ scope: Scope.DEFAULT })
export class RulesCleanupWorker {
    private readonly logger: Logger = new Logger(RulesCleanupWorker.name);

    public constructor(
        private readonly validationRuleRepository: ValidationRulesRepository,
    ) { }

    @Cron(CronExpression.EVERY_30_SECONDS)
    public async handleCron(): Promise<void> {
        this.logger.debug('Starting to delete version 00 validation rules');
        // await this.validationRuleRepository.deleteVersion00Rules();
        this.logger.debug('ended clean up activity');
    }
}

cronjob不执行,也未注册。我在日志中看到以下警告:

[Nest] 21620  - 05/29/2023, 2:01:22 PM     LOG [RouterExplorer] Mapped {/api/vr-package/:id/copy-rules, POST} (version: 1) route +1ms
[Nest] 21620  - 05/29/2023, 2:01:22 PM    WARN [Scheduler] Cannot register cron job "RulesCleanupWorker@handleCron" because it is defined in a non static provider.
[Nest] 21620  - 05/29/2023, 2:01:22 PM     LOG [NestApplication] Nest application successfully started +0ms
8ehkhllq

8ehkhllq1#

很可能您的ValidationRulesRepository注入了一个REQUEST作用域的提供程序。如果不是仓库,那么可能是TypeORM模块的配置。如果最终将REQUEST范围内的提供程序注入到scope: Scope.DEFAULT或其依赖项之一,则无论是否指定scope: Scope.DEFAULT,都不会改变该范围

相关问题