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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lavi-schedule

v1.3.6

Published

自动化任务调度,json方式制定执行内容

Readme

npm install lavi-schedule

##1、Schedule

指定周期调度(默认每秒执行),EventEmitter衍生对象

依赖 node-schedule

  let schedule = new Schedule({scheduleRule: '*/5 * * * * *'}) // 每5秒执行
  
  // 监听事件 process
  schedule.on('process', (self, time, next) => {
    console.log('do something !')
  })
  
  schedule.start()
  
  //每5秒打印一次
  // do something !

Cron-style Scheduling

The cron format consists of:

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ 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)

参考 node-schedule

方法

schedule.runOnce()

执行一次任务,不开启自动调度:

schedule.runOnce()

schedule.start()

开始定时调度任务:

schedule.start()

schedule.stop()

终止定时调度任务:

schedule.stop()

schedule.stopNext()

停止下一次定时调度任务,并终止调度任务:

schedule.stopNext()

schedule.nextInvocation

获取下一次调度任务的时间

schedule.nextInvocation

schedule.sendMessage(type, message)

发送消息,监听message事件可以获取消息

schedule.on('message', (msg) => {
  console.info(msg.message) // {type: SCHEDULE_MESSAGE_TYPE, datetime: Date, message: Object}
})
// print: message info

schedule.sendMessage(SCHEDULE_MESSAGE_TYPE.INFO, 'message info')

SCHEDULE_MESSAGE_TYPE 在 constants.js 进行定义:

const SCHEDULE_MESSAGE_TYPE = {
  INFO: 'INFO',
  WARNING: 'WARNING',
  ERROR: 'ERROR',
  DATA: 'DATA',
  START: 'START',
  END: 'END',
  SUCCESS: 'SUCCESS',
  DEBUG: 'DEBUG'
}

schedule.sendDataMessage(type, message)

发送数据消息消息

schedule.on('message', (msg) => {
  // msg = {type: SCHEDULE_MESSAGE_TYPE.DATA, datetime: Date, message: {dataType: 'json', data: 'json message'}}}
  if(msg.type === SCHEDULE_MESSAGE_TYPE.DATA) {
    if(msg.message.dataType === 'json') 
      console.info(JSON.stringify(msg.message.data)) 
  }
})
// print: {dataType: 'json', data: {data: 'json message'}}

schedule.sendDataMessage('json', {data: 'json message'})

##2、JsonJobSchedule

集成json-rules-engine规则引擎的周期性调度

constructor(rules, facts = [], options)

rules [], json对象数组

facts [], fact对象数组

options {scheduleRule // Cron-style Scheduling, callbackParams // 回调时的回传参数}

let rules = [{
      conditions: {
        any: [{
          all: [{
            fact: 'gameDuration',
            operator: 'equal',
            value: 40
          }, {
            fact: 'personalFoulCount',
            operator: 'greaterThanInclusive',
            value: 5
          }]
        }, {
          all: [{
            fact: 'gameDuration',
            operator: 'equal',
            value: 48
          }, {
            fact: 'personalFoulCount',
            operator: 'greaterThanInclusive',
            value: 6
          }]
        }]
      },
      event: {
        type: 'fouledOut',
        params: {
          message: 'Player has fouled out!'
        }
      }
    }]
    jsonJobSchedule = new JsonJobSchedule(rules, [], {scheduleRule: '*/5 * * * * *', callbackParams: 'callback params'})

    jsonJobSchedule.on('message', (msgObj) => {
      if (msgObj.type === Constants.SCHEDULE_MESSAGE_TYPE.DATA) console.log(msgObj.message.data)
      else console.log(msgObj.message)
    })

    jsonJobSchedule.runFacts = [{
      personalFoulCount: 6,
      gameDuration: 40
    }]
    jsonJobSchedule.start()

##3、JsonJobScheduleRAS

扩展JsonJobSchedule,实现对目标的 Retrieve And Send 模式数据处理,简化实现过程。 Retrieve:从数据源获取数据,继承SourceAdapter实现 Send:向目的发送数据,继承DestAdapter实现

// 每1秒执行,对目标targetId,从SourceAdapter获取数据,写入DestAdapter
jsonJobScheduleRAS = new JsonJobScheduleRAS(new SourceAdapter(), new DestAdapter(), ['targetId']
        , {scheduleRule: '*/1 * * * * *'})
jsonJobScheduleRAS.start()

SourceAdapter

数据源

// 构造数据获取API类
class MyApi {
  retrieveData (targetId) {
    // todo 获取数据
    return ['aaa', 'bbb']
  }
}

// 自定义类继承SourceAdapter
class MySourceAdapter extends SourceAdapter {
  retrieveData (params) {
    return this.options.adapter.retrieveData(params.targetId)
  }
}

let mySourceAdapter = new MySourceAdapter(new MyApi())

constructor (adapter, options)

构造器,传入获取数据的对象,和自定义参数

retrieveData (target, targetParams)

获取数据,可传入获取参数,例如:targetId

DestAdapter

// 构造数据获取API类
class MyApi {
  getTargetParams (target) {
    // todo 获取数据
    return {targetParams: 'some'}
  }
  
  sendData (targetData, targetParams) {
    // todo 发送数据
  }
}

// 自定义类继承SourceAdapter
class MyDestAdapter extends DestAdapter {
  getTargetParams (target) {
    return this.options.adapter.getTargetParams(target)
  }
  
  sendData (targetData, targetParams) {
    return this.options.adapter.sendData(targetData, targetParams)
  }
}

let myDestAdapter = new MyDestAdapter(new MyApi())

constructor (adapter, options)

构造器,传入获取数据的对象,和自定义参数

getTargetParams (target)

获取目标数据参数

sendData (targetData)

发送数据