nestjs/schedule nestjs定时任务

使用

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

@Injectable()
export class TasksService {
  private readonly logger = new Logger(TasksService.name);

  constructor(private readonly exampleService: ExampleService) {}

  @Cron('45 * * * * *')
  handleCron() {
    this.logger.debug('该方法将在45秒标记处每分钟运行一次');
  }

   
  @Interval(10000)
  handleInterval() {
    this.logger.debug('每 x ms执行一次');
  }

  @Timeout(5000)
  handleTimeout() {
    this.logger.debug('延迟 x ms执行一次');
  }

  @Interval(10000)
  sendEmail() {
    this.logger.debug('3');
  }
}

表达式

参考这个:https://wxyaa.notion.site/cron-76a3fcc84b714202bc799e79779c425d?pvs=4

cron是一种规范,不同的包有不同的实现(linux spring node)

Documentation | NestJS - A progressive Node.js framework

* * * * * *
| | | | | |
| | | | | day of week
| | | | months
| | | day of month
| | hours
| minutes
seconds (optional)
@Cron(CronExpression.EVERY_30_SECONDS)
一些常用语法

分布式

分布式系统单个直行,用redis锁一下即可

可以用重复代码实现

也可以基于ts的装饰器注解实现

推荐使用: nestjs-simple-redis-lock

如果使用nestjs 10以上版本,参考package.json overrides使用

overrides 覆盖配置

其它内容

[redisson中的看门狗机制总结](https://www.notion.so/redisson-59cbbd6778e046dc95f2da8d1adc73bd?pvs=21)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值