How to run Cron Job in Node.js application that uses cluster module? -


i'm using node-cron module scheduling tasks in node.js application. want run application in several processes using core cluster module.

running application in several processes ends in scheduled tasks execution in each process (e.g. if task send email email sent multiple times).

what best practices/possible ways of running cron job along cluster module? should create separate process handle cron job , not accept requests. if yes, how can in right way?

after research ended "distributed locks using redis" solution. there node module that: node-redis-warlock.

hope answer useful else.

update. minimal sample code:

var warlock = require('node-redis-warlock'),     redis = require('redis');  // establish redis client redis = redis.createclient();  // , pass warlock var warlock = new warlock(redis);  function executeonce (key, callback) {     warlock.lock(key, 20000, function(err, unlock){         if (err) {             // went wrong , weren't able set lock             return;         }          if (typeof unlock === 'function') {             settimeout(function() {                 callback(unlock);             }, 1000);         }     }); }  // executes call once executeonce('every-three-hours-lock', function(unlock) {     // here stuff should done once...                 unlock();           }); 

update 2. more detailed example:

const cronjob = require('cron').cronjob; const warlock = require('node-redis-warlock'); const redis = require('redis').createclient(); const warlock = new warlock(redis); const async = require('async');  function executeonce (key, callback) {     warlock.lock(key, 20000, function(err, unlock) {         if (err) {             // went wrong , weren't able set lock             return;         }          if (typeof unlock === 'function') {             settimeout(function() {                 callback(unlock);             }, 1000);         }     }); }  function everyminutejobtasks (unlock) {     async.parallel([         sendemailnotifications,         updatesomething,         // etc...     ],     (err) => {         if (err) {             logger.error(err);         }          unlock();     }); }  let everyminutejob = new cronjob({     crontime: '*/1 * * * *',     ontick: function () {         executeonce('every-minute-lock', everyminutejobtasks);     },     start: true,     runoninit: true });  /* actual tasks */ let sendemailnotifications = function(done) {     // stuff here     // call done() when finished or call done(err) if error occurred }  let updatesomething = function(done) {     // stuff here     // call done() when finished or call done(err) if error occurred }  // etc... 

Comments

  1. Your Blog is amazing. If you want to know that HOW TO SETUP A CRON JOB then,
    Click here for more information:
    HOW TO SETUP A CRON JOB

    ReplyDelete

Post a Comment

Popular posts from this blog

swift - How to change text of a button with a segmented controller? -

python - How to create jsonb index using GIN on SQLAlchemy? -

PHP DOM loadHTML() method unusual warning -