NodeJS npm not working

z9ju0rcb  于 2023-05-28  发布在  Node.js
关注(0)|答案(6)|浏览(274)

我正在使用nest.js框架开发基于节点的应用程序。我正在尝试使用https://www.npmjs.com/package/nest-schedule中提到的nest-schedule编写一个调度程序。
不知何故,当与@Cron或@Schedule一起使用时,代码无法工作。休息一下其他的装修工就可以了。使用与上面链接中提到的相同的代码库。有谁能帮我设置这个并在nodejs中使用确切的cron模式吗

cngwdvgl

cngwdvgl1#

对于当前版本的Nest,您可以使用nestjs/schedule。看看我是如何用nestjs/schedule实现这一点的。
第一步:安装nestjs cli

npm i -g @nestjs/cli

第二步:创建一个新项目

nest new schedule-sample

第3步:安装nestjs调度

npm install --save @nestjs/schedule

第四步:生成一个新的服务来放置你的服务。

nest generate service cron

一旦你安装了这个软件包,就把它添加到app.module中,如下所示:

import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { CronService } from './cron.service';

@Module({
  imports: [
    ScheduleModule.forRoot()
  ],
  providers: [CronService],
})
export class AppModule {}

第五:你可以像下面这样运行它(完整的指令在这里https://docs.nestjs.com/techniques/task-scheduling):

@Cron('*/5 * * * * *')
runEvery10Seconds() {
 console.log('Run it every 5 seconds');
}

下面是完整的示例(cron.service.ts)。

import { Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Cron, Interval } from '@nestjs/schedule';

@Injectable()
export class CronService {

    private readonly logger = new Logger(CronService.name);

    @Cron('*/5 * * * * *')
    runEvery10Seconds() {
        this.logger.debug('Run it every 5 seconds');
    }

    @Cron('10 * * * * *')
    handleCron() {
        this.logger.debug('Called when the current second is 10');
    }

    @Interval(10000)
    handleInterval() {
        this.logger.debug('Called every 10 seconds');
    }
}

最后的想法:
调度作业最复杂的方法是使用动态cron作业。为此,您可以使用SchedulerRegistry API从代码中的任何位置通过名称获得对CronJob示例的引用。首先,使用标准构造函数注入SchedulerRegistry:

constructor(private schedulerRegistry: SchedulerRegistry) {}

提示从@nestjs/schedule包导入SchedulerRegistry。然后在类中使用它,如下所示。假设使用以下声明创建了一个cron作业:

@Cron('* * 8 * * *', {
  name: 'notifications',
})
triggerNotifications() {}

使用以下命令访问此作业:

const job = this.schedulerRegistry.getCronJob('notifications');

job.stop();
console.log(job.lastDate());

我已经在以下版本中测试了它(package.json)

"@nestjs/common": "^7.6.15",
"@nestjs/core": "^7.6.15",
"@nestjs/schedule": "^0.4.3",
hmmo2u0o

hmmo2u0o2#

有同样的问题,cron jobs没有启动,用这种方式解决:

async function bootstrap() {

  const app = await NestFactory.createApplicationContext(AppModule);
  
  // add this, if network if network listener is needed :
  await app.listen(<port>)

  // or this if network is not needed :
  await app.init()

}

bootstrap();
7uhlpewt

7uhlpewt3#

您是否已将您的工作服务添加到模块中?
可能您的作业未导入到任何模块中。
https://github.com/nestjs/nest/tree/master/sample/27-scheduling/src/tasks

ef1yzkbh

ef1yzkbh4#

@Cron()@Schedule()装饰器直到v0.3.1github issue)才真正工作。
你能试试latest version吗?
package.json

{
    ...
    "dependencies": {
        "nest-schedule": "^0.3.1"
        ...
    }
    ...
}

scheduler.service.ts

import { Injectable } from '@nestjs/common';
import { Cron, NestSchedule } from 'nest-schedule';

@Injectable()
export class SchedulerService extends NestSchedule {

    // ...

    @Cron('* * * * * *') // Run every second
    scheduledJob() {
        console.info('[Scheduler]: scheduled jobs has been started');

        // ...
    }

    // ...

}

对我来说很好

5m1hhzi4

5m1hhzi45#

我也有同样的问题...做了一些研究后,我发现我需要使用最新版本的@nestjs/common和@nestjs/core,如果使用的话,还需要使用@nestjs/platform-express

xzlaal3s

xzlaal3s6#

注意,装饰器是在声明性定义中使用的。
不工作:

@Cron(CronExpression.EVERY_10_SECONDS)
async task() {
    console.log('[Scheduler]: every 10 seconds');
}

工作:

@Cron(CronExpression.EVERY_10_SECONDS)
task() {
    console.log('[Scheduler]: every 10 seconds');
}

相关问题