内容概要
- 在实际开发项目中,会遇到很多定时任务的工作。比如:定时发送推送通知,定时更新某些数据等等
这是我们第一时间想到的就是可以写个定时器,来完成相应的需求,而在在node.js中可以使用node-schedule来完成定时任务
nestjs是一个node框架当然也可以使用node-schedule来完成定时任务。下面就用示例来说明一下node-schedule的用法.
安装依赖
npm install node-schedule
开启一个定时任务
1 2 3 4 5 6 7 8 9 10
| const schedule = require('node-schedule');
function scheduleTask(){ schedule.scheduleJob('30 * * * * *', ()=>{ console.log('scheduleTask:' + new Date()); }); }
scheduleTask();
|
定时任务中的通配符解释
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
6个占位符从左到右分别代表:秒、分、时、日、月、周几
每分钟的第30秒触发: ‘30 * * * * *’
每小时的1分30秒触发 :’30 1 * * * *’
每天的凌晨1点1分30秒触发 :’30 1 1 * * *’
每月的1日1点1分30秒触发 :’30 1 1 1 * *’
每年的1月1日1点1分30秒触发 :’30 1 1 1 1 *’
每周1的1点1分30秒触发 :’30 1 1 * * 1’
范围触发
1 2 3 4 5 6 7 8 9 10
| const schedule = require('node-schedule');
function scheduleTask(){ schedule.scheduleJob('1-10 * * * * *', function(){ console.log('scheduleTask:' + new Date()); }); }
scheduleTask();
|
递归规则定时器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const schedule = require('node-schedule');
function scheduleTask(){
let rule = new schedule.RecurrenceRule(); rule.second = 0; schedule.scheduleJob(rule, function(){ console.log('scheduleTask:' + new Date()); }); }
scheduleTask();
|
对象文本语法定时器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const schedule = require('node-schedule');
function scheduleTask(){ schedule.scheduleJob({hour: 17, minute: 0, dayOfWeek: 5}, function(){ console.log('scheduleTask:' + new Date()); }); }
scheduleTask();
|
取消定时器
调用定时器对象的cancl方法即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| const schedule = require('node-schedule');
function scheduleCancel(){
let counter = 1; const j = schedule.scheduleJob('* * * * * *', function(){ console.log('定时器触发次数:' + counter); counter++; });
setTimeout(function() { console.log('定时器取消') j.cancel(); }, 5000); }
scheduleCancel();
|
nestjs中使用定时器
这里有这样一个需求,每周五的下午6点定时更新数据库中某些字段;
这里我们就可以实现一个接口用来更新数据库表字段,而关键就在于每周五的下午6点去更新,这时我们就可以启一个定时任务,定时调用接口达到定时更新的目的。
例如:
有接口 http://localhost:3000/photo/updateStatus 定时将数据库中数据删除(不建议删库,一般设置字段表示数据状态,更新该字段为指定值表示删除状态)
在main.ts引入并使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import { Logger } from '@nestjs/common'; import { NestFactory, FastifyAdapter } from '@nestjs/core'; import { AppModule } from './app.module'; import * as cors from 'cors'; import { join } from 'path'; import fly from 'flyio';
const schedule = require('node-schedule');
function scheduleTask() { const timing = schedule.scheduleJob('0 0 0 * * *', () => { Logger.log('定时任务'); fly.get('http://localhost:3000/photo/updateStatus').then(res => { Logger.log(JSON.stringify(res.data)); }).catch(err => { Logger.log(JSON.stringify(err)); }); }); } scheduleTask();
async function bootstrap() { const app = await NestFactory.create(AppModule); app.useStaticAssets(join(__dirname, '..', 'public')); app.use( cors({ credentials: true, }), ); await app.listen(3000); } bootstrap();
|
nest-schedule
nest-schedule是一个基于node-schedule实现的用于nest.js的任务库
…还未尝试使用