Skip to content

SalakJS/salak-schedule

Repository files navigation

salak-schedule

NPM version build status David deps NPM download

Cron task for salak.

Feature

  • Three mode: single、worker and all.
    • single means the task will be trigger on the only one thread in the only one machine.
    • worker means the task will be trigger on the only one thread in every machine.
    • all means the task will be trigger on every thread
  • Task can trigger service.

Install

npm install --save salak-schedule

Usage

const schedule = require('salak-schedule')

schedule(options, app)

load schedule from dir schedule under every module.

const { Service } = require('salak')

class Task extends Service {
  static timer () {
    return {
      enable: true, // default true
      interval: 1000,
      type: 'all', // single、all、worker, default 'all'
      cron: '* * * * * *', // use `cron-parser`
      cronOptions: {}
    }
  }

  async run () { // for the task logic

  }
}

API

schedule options

  • Store: for single or worker,default redisStore
  • prefix: for single or worker,default 'salakTimer'
  • options: options for Store. default app.redis

app.getSchedules()

app.runSchedule(key)

  • key: ${module}.${taskfilename}

app.closeSchedules()

Write a store

class AStore {
  lock (key, ttl) {}
  unlock (lock) {}
}

module.exports = AStore

License

MIT

Releases

No releases published

Packages

No packages published