npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@inest/nestjs-schedule

v0.0.9

Published

基于Nestjs + bullMQ + redis实现的分布式任务调度模块

Downloads

47

Readme

@inest/nestjs-schedule

基于Nestjs + bullMQ + redis实现的分布式任务调度模块

说明

默认配置项:防止任务调度中产生大量的日志数据,影响redis性能。 可根据实际情况在初始化SchedulerModule模块时自行配置

const DEFAULT_JOB_OPTIONS = {
  removeOnComplete: {
    age: 3600, // 1小时
    count: 10, // 保存近10条
  },
  removeOnFail: {
    age: 7 * 24 * 3600, // 7天
    count: 100, // 保存近100条
  },
  stackTraceLimit: 500,
};

安装

npm i @inest/nestjs-schedule

uarn add @inest/nestjs-schedule

pnpm i @inest/nestjs-schedule

使用方法

1. 导入模块

1.1. 静态导入模块

SchedulerModule.forRoot({
  connection: {
    host: 'xxx.xxx.xxx.xxx',
    port: 6379,
    password: 'xxxxxx',
    db: 0,
  },
  prefix: 'applicationName', // 任务名前缀 
} as JobOptionsWrapper);

1.2. 异步导入模块

需要通过读取配置文件的方式获取redis配置时,使用此方式

SchedulerModule.forRootAsync({
  inject: [ConfigService],
  useFactory: (cs: ConfigService) => {
    // 调用cs服务,获取配置文件中的配置
    return {
      connection: {
        host: 'xxx.xxx.xxx.xxx',
        port: 6379,
        password: 'xxxxxx',
        db: 0,
      },
      prefix: 'applicationName', // 任务名前缀 
    } as JobOptionsWrapper;
  },
})

2. 分布式任务调度器的使用

2.1 cron表达式

import { Injectable } from '@nestjs/common';

@Injectable()
export class TaskService {
  // 每5秒中调用一次
  @Schedule('*/5 * * * * ?')
  task() {
    console.log('helle world');
  }
}

2.2 间隔调用

import { Injectable } from '@nestjs/common';

@Injectable()
export class TaskService {
  // 每5秒中调用一次
  @Schedule(5000) // 5000 ms
  task() {
    console.log('helle world');
  }
}

2.3 特定条件下调用

import { Injectable } from '@nestjs/common';

@Injectable()
export class TaskService {
  // 每5秒中调用一次
  @Schedule('*/5 * * * * ?', () => {
    // 伪代码 应对各类特定环境才执行的场景
    // return getIp() === '118.88.88.88';
    // return getOS() === 'Linux';
    // return getApplication() === 'taskApp';
    // ...
    return true;
  })
  task() {
    console.log('helle world');
  }
}

更多参数配置,请参考bull文档

3. 本地(单机)任务调度器的使用

import { Injectable } from '@nestjs/common';

@Injectable()
export class TaskService {
  @LocalSchedule({
    cronTime: '* * * * * *',
    /**
     * 特定条件下调用,同上面的场景
     */
    validator() {
      // 伪代码 应对各类特定环境才执行的场景
      // return getIp() === '118.88.88.88';
      // return getOS() === 'Linux';
      // return getApplication() === 'taskApp';
      // ...
      return true;
    },
  })
  localTask() {
    console.log('helle world');
  }
}

更多参数请参考@nestjs/schedule文档