Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Latest commit c9ecc2e Nov 20, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
README.md
index.js
package-lock.json
package.json

README.md

Cron-cluster

Build Status

Cron cluster is designed to prevent a job which must be launched only once to be launched many times while an app is scaling accross a cluster with the same task scripts.

Cron cluster is based on node-cron and redis-leader to prevent a cron job to be launched if his process has not been elected as leader on redis.

Requirement

Redis

Install

npm install cron-cluster

Usage

Cron cluster use the same API as the original CronJob.

var redis = require('redis').createClient()
var CronJob = require('cron-cluster')(redis).CronJob

function doCron () {
  var job = new CronJob('* * * * * *', function () {
    // Do some stuff here
  })
  job.start()
}

It is possible to initialize CronJob with an object passed as parameter and run the job only once for all the instances. Cron-cluster is compatible with original cron. More in test/cron-cluster-compatibility-check.js

var redis = require('redis').createClient()
var CronJob = require('cron-cluster')(redis).CronJob

function doCron () {
  var job = new CronJob({
    cronTime: '* * * * * *', 
    onTick: function () {
        // Do some stuff here
    }
  })
  job.start()
}

All you need is to provide a redis client to the cron-cluster module.

Example

Usage with Kue:

var kue = require('kue'),
    queue = kue.createQueue()
var redis = require('redis').createClient()
var CronJob = require('cron-cluster')(redis).CronJob

new CronJob('* * * * * *', function () {
  queue.create('log', {
    text: 'this is a log'
  }).save()
}, null, true)

queue.process('log', function (job, done) {
  console.log(job.data.text)
  done()
})
You can’t perform that action at this time.