{"id":68,"date":"2016-01-29T01:51:08","date_gmt":"2016-01-28T20:21:08","guid":{"rendered":"http:\/\/madhurendra.com\/?p=68"},"modified":"2016-01-29T01:51:08","modified_gmt":"2016-01-28T20:21:08","slug":"optimal-way-scheduling-long-job-nodejsjs","status":"publish","type":"post","link":"https:\/\/madhurendra.com\/optimal-way-scheduling-long-job-nodejsjs\/","title":{"rendered":"Optimal way of scheduling long job – nodejs\/js"},"content":{"rendered":"
If you are using nodejs for some backend stuff to schedule some-work, probably you will hate scheduling because of two factors <\/p>\n
You would probably favor cronjobs for scheduling – or may be polling database for regular interval. Loading nodejs script every 5min is a costly job – developers understand, so you can’t follow php\/short interval scheduling. <\/p>\n
First problem can be solved by of setting a recursive call to a function, with time out. Many solutions on web.
\nBut there lies a problem – You can’t cancel the timeout if you needed to after a recursive call. To solve the recursive call problem i wrote few lines of code.<\/p>\n
function setLongTimeout(callback, timeout_ms, updateTimerID) {\r\n \/\/if timout is more than 32bit int\r\n var timer;\r\n if (timeout_ms > 2147483647) {\r\n \/\/now wait until the max wait time passes then call this function again with\r\n timer = setTimeout(function () {\r\n var id = setLongTimeout(callback, (timeout_ms - 2147483647), updateTimerID);\r\n if (typeof updateTimerID ==='function')\r\n updateTimerID(id);\r\n }, 2147483647);\r\n } else {\r\n timer = setTimeout(callback, timeout_ms);\r\n }\r\n if (typeof updateTimerID ==='function')\r\n updateTimerID(timer);\r\n return timer;\r\n}<\/pre>\nAbove code behaves same as `setTimeout` if you don’t need timer object updates, else you can register for that.<\/p>\n
Second problem – Timers are removed when process are restarted. – That shouldn’t be hard – You can create a function to execute atomic functions,
\nNote atomic – atomic because if your setTimeout code will depend on variables state you will need to load those variables from database which will make job harder – better way is to schedule something like – `send email to abc@example.com` instead of `send message to currently loggedin users`. <\/p>\nSolution of second problem really depends on your problem – but if you analyze your scheduled jobs closely you will find that they are really atomic, you made them non-atomic !.<\/p>\n
if you really need that your jobs are never lost from memory better way is to run a stable nodeserver & use IPC. – but that would be practically hard to maintain.<\/p>\n","protected":false},"excerpt":{"rendered":"
If you are using nodejs for some backend stuff to schedule some-work, probably you will hate scheduling because of two factors You can’t set long timeout Timers are removed when process restart You would probably favor cronjobs for scheduling – or may be polling database for regular interval. Loading nodejs script every 5min is a … <\/p>\n