In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要介绍了node项目中如何使用Node Schedule创建定时任务,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
nodejs的后端也是需要定时任务做处理,例如备份,定时发送邮件,结算等操作,所以使用了Node Schedule这个npm帮助我们制定定时任务脚本。
npm install node-schedule
Node Schedule 中的每个定时任务都由一个Job对象表示。可以手动创建,然后执行schedule()方法以应用任务,或使用scheduleJob()如下。
Job对象是`EventEmitter,并发出以下事件:
run每次执行后的事件。
scheduled每次计划运行时的事件。
一canceled,当它在执行之前调用被取消的事件。
一个error当被触发调度作业调用抛出或退出事件拒绝Promise。
(scheduled和canceled事件都接收一个 JavaScript 日期对象作为参数)。 注意的是,任务是第一次立即执行的,因此如果使用scheduleJob()方法创建任务,将错过第一个scheduled 事件触发,但您可以手动查询调用。
Cron格式* * * * * *┬ ┬ ┬ ┬ ┬ ┬│ │ │ │ │ ││ │ │ │ │ └ 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)每分钟的第30秒触发: '30 * * * * *'每小时的1分30秒触发 :'30 1 * * * *'每天的凌晨1点1分30秒触发 :'30 1 1 * * *'每月的1日1点1分30秒触发 :'30 1 1 1 * *'2016年的1月1日1点1分30秒触发 :'30 1 1 1 2016 *'每周1的1点1分30秒触发 :'30 1 1 * * 1'这里是个人对node-schedule封装类来使用增删改查定时任务const schedule = require('node-schedule');exports.Interval = class Interval { constructor({ unit_name, maintain_time, last_alarm }) { this.unit_name = unit_name // 任务名字 this.maintain_time = maintain_time // 定时时间 this.last_alarm = last_alarm || "" // 上一次定时任务名字 } // 生成新的定时任务 async create(callback) { // 终止之前的定时任务 if (this.last_alarm !== "") { this.delete(this.last_alarm) } schedule.scheduleJob(`${this.unit_name}`, `${this.maintain_time}`, callback); } // 删除定时任务 delete() { if (schedule.scheduledJobs[this.unit_name]) { schedule.scheduledJobs[this.unit_name].cancel(); return true } return false } // 找到一个定时任务 findOne(name) { if (schedule.scheduledJobs[name]) { return schedule.scheduledJobs[name] } else { throw new Error("未找到任务名") } } // 查看所有的定时任务 findAll() { return schedule.scheduledJobs }}这里是调用时定时任务Interval实例// 定时任务new Util.Interval({ unit_name: '自动分发任务 0 0 12 * * *', maintain_time: '0 0 12 * * *', last_alarm: '自动分发任务 0 0 12 * * *'}).create(async () => { // 写入你自己想在定时任务触发的时候,想要执行的函数})感谢你能够认真阅读完这篇文章,希望小编分享的"node项目中如何使用Node Schedule创建定时任务"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.