🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@yuo/node-cron

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yuo/node-cron

task scheduler for glede-server

latest
Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
6
200%
Maintainers
2
Weekly downloads
 
Created
Source

Node Cron

下载并使用

npm install @yuo/node-cron

Cron 语法

Cron允许的字段

 # ┌────────────── 秒 (可选)
 # │ ┌──────────── 分
 # │ │ ┌────────── 时
 # │ │ │ ┌──────── 日
 # │ │ │ │ ┌────── 月
 # │ │ │ │ │ ┌──── 周 (0-7), 0和7表示周日
 # │ │ │ │ │ │
 # │ │ │ │ │ │
 # * * * * * *

Cron各字段范围值

fieldvalue
0-59
0-59
0-23
1-31
1-12
0-7 (周日 0, 7 )

使用

使用时间范围

import { schedule } from '@yuo/node-cron';

cron.schedule('1,2,4,5 * * * *', () => {
  console.log('在每小时中的第 1、2、4、5 的分钟整点各触发一次');
});

cron.schedule('1-5 * * * *', () => {
  console.log('在每小时中的第1~5分钟中, 每分钟触发一次');
});

使用时间步进值

import { schedule } from '@yuo/node-cron';

schedule('*/2 * * * *', () => {
  console.log('间隔2分钟执行一次');
});

schedule('10/2 * * * * *', () => {
  console.log('从每分钟的第10秒开始, 间隔2秒钟执行一次');
});

启动任务

import { schedule } from '@yuo/node-cron';

// 默认scheduled: true, 是直接开启的, 不需要手动start
const task1 = schedule('* * * * *', (flag: string) =>  {
  // doSomething
  if (flag === 'manual') {
    // 检测到手动触发后, 注销事务
    task1.stop();
  }
}, { scheduled: false });
task1.start();

task1.now('manual');

// fork子进程启动, 相对于根目录(node启动时的目录)
const task2 = schedule('* * * * *', './schedule-1.ts');

// ./schedule-1.ts, 文件形式的事务无入参
import { yourNBUtil } from 'util';

export default () => {
  let shit = yourNBUtil.doSomething();
};

暂停任务

import { schedule } from '@yuo/node-cron';

const task = schedule('* * * * *', () =>  {
  console.log('will execute every minute until stopped');
});

task.stop();

验证Cron规则

import { validate } from '@yuo/node-cron';

const res1 = validate('59 * * * *'); // valid
const res2 = validate('60 * * * *'); // invalid 0~59

Keywords

cron

FAQs

Package last updated on 30 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts