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 costly job – developers understand, so you can’t follow php/short interval scheduling.
First problem can be solved by of setting a recursive call to a function, with time out. Many solutions on web.
But 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.
function setLongTimeout(callback, timeout_ms, updateTimerID) { //if timout is more than 32bit int var timer; if (timeout_ms > 2147483647) { //now wait until the max wait time passes then call this function again with timer = setTimeout(function () { var id = setLongTimeout(callback, (timeout_ms - 2147483647), updateTimerID); if (typeof updateTimerID ==='function') updateTimerID(id); }, 2147483647); } else { timer = setTimeout(callback, timeout_ms); } if (typeof updateTimerID ==='function') updateTimerID(timer); return timer; }
Above code behaves same as `setTimeout` if you don’t need timer object updates, else you can register for that.
Second problem – Timers are removed when process are restarted. – That shouldn’t be hard – You can create a function to execute atomic functions,
Note 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 [email protected]` instead of `send message to currently loggedin users`.
Solution 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 !.
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.
Recent Comments