{"id":2089,"date":"2022-08-30T15:21:57","date_gmt":"2022-08-30T15:21:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/25\/node-js-how-to-create-smooth-restart-of-launched-app-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:21:57","modified_gmt":"2022-08-30T15:21:57","slug":"node-js-how-to-create-smooth-restart-of-launched-app-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/node-js-how-to-create-smooth-restart-of-launched-app-collection-of-common-programming-errors\/","title":{"rendered":"Node.js: How to create &ldquo;smooth&rdquo; restart of launched app?-Collection of common programming errors"},"content":{"rendered":"<p>I took an approach similar to PartlyCloudy, but just using multiple node threads on the same process, and using the OS as the load balancer instead of a separate one.<\/p>\n<p>So I have code in my &#8220;app.coffee&#8221; code that&#8217;s stolen from:<\/p>\n<p>I only added a &#8220;setTimeout&#8221;. For a period of 5-20 seconds I may have 16 forks on my 8 cores instead of 8, but that&#8217;s better for me than downtime.<\/p>\n<pre><code>var childProcesses, cluster, i, numCPUs, signals, _i;\n\ncluster = require('cluster');\nnumCPUs = require('os').cpus().length;\n\nif (cluster.isMaster) {\n  childProcesses = [];\n  for (i in numCPUs) {\n    childProcesses[i] = cluster.fork();\n  }\n  signals = [\"SIGINT\", \"SIGTERM\", \"SIGQUIT\"];\n  for (i in signals) {\n    process.on(signals[i], function() {\n\n      \/\/ THIS IS WHAT KEEPS IT UP\n      setTimeout(function() {\n        var j;\n        for (j in childProcesses) {\n          childProcesses[j].kill();\n        }\n        process.exit();\n      }, 20000);\n\n    });\n  }\n  cluster.on('exit', function(worker, code, signal) {\n    childProcesses.push(cluster.fork());\n  });\n} else {\n  app.listen(3000);\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-12-25 10:58:08. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I took an approach similar to PartlyCloudy, but just using multiple node threads on the same process, and using the OS as the load balancer instead of a separate one. So I have code in my &#8220;app.coffee&#8221; code that&#8217;s stolen from: I only added a &#8220;setTimeout&#8221;. For a period of 5-20 seconds I may have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2089","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2089"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2089\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}