{"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

    \n
  1. You can’t set long timeout<\/li>\n
  2. Timers are removed when process restart<\/li>\n<\/ol>\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>\n

    Above 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>\n

    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 !.<\/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

    Continue reading<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[13,6,8],"tags":[11],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-javascript","category-server","category-snippets","tag-nodejs","item-wrap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pciGs2-16","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":1,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/68\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}