fs项目---->cron框架的学习(一)
Cron是一种允许您按计划执行某些内容的工具。这通常使用cron语法来完成。我们允许您在计划作业触发时执行函数。我们还允许您使用子进程执行javascript进程外部的作业。此外,这个库超出了基本的cron语法,允许您提供日期对象。这将用作回调的触发器。Cron语法仍然是一种可接受的CronTime格式。虽然这里支持的Cron模式在标准Unix格式上扩展为支持秒数,但如果不使用它,则默认为0,并与Unix行为相匹配。
一、简要的介绍
安装: npm install cron
cron支持的格式:
Asterisk. E.g. * (每unit)
Ranges. E.g. -,5(第1,2,3unit)
Steps. E.g. */2 (每2unit)
cron值的范围,这个可能与标准的有一些差别。
- Seconds: 0-59
- Minutes: 0-59
- Hours: 0-23
- Day of Month: 1-31
- Months: 0-11 (Jan-Dec)
- Day of Week: 0-6 (Sun-Sat)
二、cron的api使用
这里面我们先介绍一下cron里面的api,再详细的说明一下cron的语法所代表的含义。我们以一个简单的例子开头,如下:
const CronJob = require('cron').CronJob;
const job = new CronJob('*/1 * * * * *', function () {
console.log(`You will see this message every second ${new Date()}`);
});
console.log('After job instantiation');
job.start();
打印的结果如下所示,每秒打印一次。
After job instantiation
You will see this message every second Sat Sep :: GMT+ (China Standard Time)
You will see this message every second Sat Sep :: GMT+ (China Standard Time)
You will see this message every second Sat Sep :: GMT+ (China Standard Time)
You will see this message every second Sat Sep :: GMT+ (China Standard Time)
.......
现在我们看一下CronJob对象里面的参数:
constructor(cronTime: string | Date, onTick: () => void, onComplete?: () => void, start?: boolean, timeZone?: string, context?: any, runOnInit?: boolean);
关于对象参数的说明如下:
cronTime- [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS Date object.onTick- [REQUIRED] - The function to fire at the specified time. If anonCompletecallback was provided,onTickwill receive it as an argument.onTickmay callonCompletewhen it has finished its work.onComplete- [OPTIONAL] - A function that will fire when the job is stopped withjob.stop(), and may also be called byonTickat the end of each run.start- [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to calljob.start()in order to start the job (assumingjobis the variable you set the cronjob to). This does not immediately fire youronTickfunction, it just gives you more control over the behavior of your jobs.timeZone- [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at Moment Timezone Website. Probably don't use both.timeZoneandutcOffsettogether or weird things may happen.context- [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to callthis.stop(). However, if you change this you'll have access to the functions and values within your context object.runOnInit- [OPTIONAL] - This will immediately fire youronTickfunction as soon as the requisite initialization has happened. This option is set tofalseby default for backwards compatibility.utcOffset- [OPTIONAL] - This allows you to specify the offset of your timezone rather than using thetimeZoneparam. Probably don't use bothtimeZoneandutcOffsettogether or weird things may happen.unrefTimeout- [OPTIONAL] - If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at timers#timers_timeout_unref from the NodeJS docs
- cronTime为Date类型,而且设置了start=true(不用调用job.start()方法就启动了cron)和
runOnInit=true(启动就执行了onTick函数)
const CronJob = require('cron').CronJob;
const now = new Date();
const param = {
cronTime: new Date(now.getTime() + * ),
onTick: function() {
console.log(`You will see this message after second again ${new Date()}`);
},
start: true,
runOnInit: true
};
const job = new CronJob(param);
console.log('job status ' + job.running);
运行的结果如下:
You will see this message after second again Sat Sep :: GMT+ (China Standard Time)
job status true
You will see this message after second again Sat Sep :: GMT+ (China Standard Time)
- cronTime为cron格式,而且设置了
onComplete函数,stop之后得到lastdate数值。
const CronJob = require('cron').CronJob;
const param = {
cronTime: '*/2 * * * * *',
onTick: function() {
console.log(`You will see this message every two second again ${new Date()}`);
},
onComplete: function() {
console.log(`job status = ${job.running} and date = ${new Date()}`);
}
};
const job = new CronJob(param);
console.log(`job status = ${job.running}`);
job.start();
setTimeout(() => {
job.stop();
console.log(`last execute date is ${job.lastDate()}`)
}, * );
运行的一次结果如下:
job status = undefined
You will see this message every two second again Sat Sep :: GMT+ (China Standard Time)
You will see this message every two second again Sat Sep :: GMT+ (China Standard Time)
You will see this message every two second again Sat Sep :: GMT+ (China Standard Time)
You will see this message every two second again Sat Sep :: GMT+ (China Standard Time)
You will see this message every two second again Sat Sep :: GMT+ (China Standard Time)
job status = false and date = Sat Sep :: GMT+ (China Standard Time)
last execute date is Sat Sep :: GMT+ (China Standard Time)
- 在onTick中使用context(默认是CronJob对象),可以调用stop方法、start、running等
const CronJob = require('cron').CronJob;
const now = new Date(new Date().getTime() + * );
const param = {
cronTime: '* * * * * *',
onTick: function() {
console.log(`${new Date()}`);
if(now < new Date()) {
this.context.stop();
}
}
};
const job = new CronJob(param);
console.log(`now is ${new Date()}`);
job.start();
运行的结果如下:
now is Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time)
三、关于cron格式的例子
我们先举一个完整的例子,后面都是在这基础上做的修改。完整例子代码如下:
- 2 * * * * *:每分钟中的第二秒执行一次
const CronJob = require('cron').CronJob;
const param = {
cronTime: '2 * * * * *',
onTick: function() {
console.log(`${new Date()} and context is ${this.context.running}`);
}
};
const job = new CronJob(param);
console.log(`now is ${new Date()}`);
job.start();
运行的结果如下:
now is Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
......
- 2 34-37 * * * *:每小时中的第34分2秒、第35分2秒、第36分2秒、第37分2秒执行一次
now is Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
......
- 1-30/6 22-23 14-15 * * *:每天的14点或者15点中的22分或者23分中的第1秒、7秒、13秒、19秒、25秒执行一次
now is Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
........
- 1-30/6,33 * * * * *:每分中的第1秒、7秒、13秒、19秒、25秒、33秒执行一次
now is Sat Sep :: GMT+ (China Standard Time)
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
Sat Sep :: GMT+ (China Standard Time) and context is true
......
- */5 * * * * *:每5秒执行一次
now is Sun Sep :: GMT+ (China Standard Time)
Sun Sep :: GMT+ (China Standard Time) and context is true
Sun Sep :: GMT+ (China Standard Time) and context is true
Sun Sep :: GMT+ (China Standard Time) and context is true
......
友情链接
fs项目---->cron框架的学习(一)的更多相关文章
- fs项目---->async/await的学习(一)
2018-07-11号,我来到了fs项目组担任后端开发的角色.这是我来thoughtworks以来首个的正式项目,不管是在技术还是在敏捷的实践中都是受益匪浅.来感受tw特殊的文化的同时,我希望自己能够 ...
- [springmvc+mybatis][关于这两个框架的学习,我想说]
关于学习笔记 在对java web有了一定的了解后,这两个框架没怎么写学习笔记了…毕竟项目驱动型…… 关于学习资料 强烈推荐传智播客的燕青讲解的 让我对这种培训班教育的资料刮目相看(不过还是千万别去这 ...
- jfinal框架教程-学习笔记
jfinal框架教程-学习笔记 JFinal 是基于 Java 语言的极速 WEB + ORM 开发框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restfu ...
- JAVA框架之Hibernate框架的学习步骤
首先介绍一下Java三大框架的关系 以CRM项目即客户关系管理项目示例 hibernate框架的学习路线: 1.学习框架入门,自己搭建框架,完成增删改查的操作 2.学习一级缓存,事物管理和基本查询 3 ...
- java面试项目经验:框架及应用
Java项目经验 Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点:1.掌握项目开发的基本步骤2.具备极强的面向对象的分析与设计技巧3 ...
- 《PHP框架Laravel学习》系列分享专栏
<PHP框架Laravel学习>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201735.html 文章 Laravel教程:l ...
- Mina框架的学习笔记——Android客户端的实现
Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...
- (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码
http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...
- (转)MyBatis框架的学习(三)——Dao层开发方法
http://blog.csdn.net/yerenyuan_pku/article/details/71700957 使用MyBatis开发Dao层,通常有两个方法,即原始Dao开发方法和Mappe ...
随机推荐
- linux命令行下修改系统时间、时区
date查看时间以及时区 图a是est时区,和HONGkong时间查了一个小时. # 保存设置$ sudo mv /etc/localtime /etc/localtime.old # 设置时区 $ ...
- godaddy如何联系客服帮助的技巧和方法
众所周知,Godaddy是世界最大的域名商和空间商,很多人喜欢在那里买域名或者空间,可是,当我们的域名空间出了问题要怎么办呢?今天闪电博客就给大家介绍一些Godaddy客服联系技巧,减少大家等待的时间 ...
- 用delphi制作无界面的activex控件
首先,您要了解: •COM的基本原理 •能被网页调用的非可视ActiveX控件必须是一种至少实现了IOleObject接口的TAutoObject组件 •利用Delphi向导生成的ActiveX控件必 ...
- iOS:NSFileHandle和NSFileManger的使用
一.介绍 利用NSFilehandle类提供的方法,允许更有效地使用文件. 一般而言,处理文件时都要经历以下三个步骤: 1.打开文件,并获取一个NSFileHandle对象,以便在后面的I/O操作中引 ...
- stm32型号解读
ST意法半导体在牵手ARM后可以说是做的非常成功,抓住了从普通MCU到ARM的市场转变的机会.由于ST公司的STM32系列ARM 使用了完善的库开发,作为芯片的应用者不用从底层的寄存器来实现每个功 ...
- chromium 使用 flash
这又是个月经问题,几乎重装一次系统就得在搞一次. 1. chromium 使用的 flash 下载 https://get.adobe.com/cn/flashplayer/otherversions ...
- 微信公众号基础02_获取accessToken和用户信息
上一篇分享了搭建微信公众号server,本文分享一下假设获取access_Token和用户信息.工具还是新浪云SAE 1.获取access_Token 相见开发文档:https://mp.weixin ...
- 分区工具parted的详解及常用分区使用方法【转】
来源:http://blog.51cto.com/zhangmingqian/1068779 分区工具parted的详解及常用分区使用方法 一. parted的用途及说明 概括使用说明 ...
- 在Asp.Net中操作PDF – iTextSharp - 操作图片
iTextSharp支持所有主流的图片格式,比如:jpg, tif, gif, bmp, png和wmf.在iTextSharp中使用Image.GetInstance()方法创建图片有很多种方式,或 ...
- Atitit mybatis快速开发 的sql api接口
Atitit mybatis快速开发 的sql api接口 1.1. sql模式 开发速度大大快与 映射模式1 1.2. MyBatis Mapper1 1.2.1. 代码2 1.2.2. 原理2 1 ...