PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://github.com/shimondoodkin/nodejs-autorestart
Markdown | 162 lines | 118 code | 44 blank | 0 comment | 0 complexity | e5ad4425c2c57322cd1cce79f560781d MD5 | raw file
  1. # What is node.js auto restart
  2. It is a set of examples and a module with common problems solved.
  3. It allows easy development and stable production.
  4. - Run on boot:
  5. To make your program run automaticaly on boot with Upsrart(you need to configure a .sh file and a .conf file)
  6. - Auto restart:
  7. autoexit module to watch all .js files if they have been changed and to restart nodejs.
  8. - A description of how to make your program crash proof from javascript errors. (segfaults still crash)
  9. - Auto reload a module:
  10. makes sense for fast development of slow loading app,
  11. "http://github.com/shimondoodkin/node-hot-reload":http://github.com/shimondoodkin/node-hot-reload.
  12. ## How to use nodejs auto restart:
  13. Copy `nodejs.sh` and `autoexit.js` to root folder of your application
  14. for example to `/var/www`. Copying of `autoexit.js` is optional and it can be included from deps folder
  15. ### Add to your script at top:
  16. require.paths.unshift(__dirname); //make local paths accessible
  17. ### And to your script at end:
  18. // exit if any js file or template file is changed.
  19. // it is ok because this script encapsualated in a batch while(true);
  20. // so it runs again after it exits.
  21. var autoexit_watch=require('autoexit').watch;
  22. //
  23. var on_autoexit=function (filename) { } // if it returns false it means to ignore exit this time;
  24. autoexit_watch(__dirname,".js", on_autoexit);
  25. //autoexit_watch(__dirname+"/templates",".html", on_autoexit);
  26. ### You might want to use: `try-catch` that will make your applicaiton not crash on errors
  27. try
  28. {
  29. //your code
  30. }
  31. catch(e)
  32. {
  33. console.log(e.stack)
  34. }
  35. Also after serving one sucsessful request (application is fully loaded) I add an on error error handler:
  36. if(!app_loaded)
  37. {
  38. process.on('uncaughtException', function (err) {
  39. console.log('Caught exception: ' + err.stack);
  40. });
  41. app_loaded=true;
  42. }
  43. As i like it, I want it to crash on load errors and exit the application but not on application errors.
  44. ### Example:
  45. require.paths.unshift(__dirname); //make local paths accessible
  46. var http = require('http');
  47. var app_loaded=false;
  48. http.createServer(function (req, res) {
  49. if(!app_loaded)
  50. {
  51. process.on('uncaughtException', function (err) {
  52. console.log('Caught exception: ' + err.stack);
  53. });
  54. app_loaded=true;
  55. }
  56. try
  57. {
  58. res.writeHead(200, {'Content-Type': 'text/plain'});
  59. res.end('Hello World\n');
  60. }
  61. catch(e)
  62. {
  63. console.log(e.stack);
  64. }
  65. }).listen(8124);
  66. console.log('Server running at http://127.0.0.1:8124/');
  67. // exit if any js file or template file is changed.
  68. // it is ok because this script encapsualated in a batch while(true);
  69. // so it runs again after it exits.
  70. var autoexit_watch=require('autoexit').watch;
  71. //
  72. var on_autoexit=function () { console.log('bye bye'); }
  73. autoexit_watch(__dirname,".js", on_autoexit);
  74. //autoexit_watch(__dirname+"/templates",".html", on_autoexit);
  75. ### Edit nodejs.sh
  76. Edit `nodejs.sh` to match to your server.js filename.
  77. ### To launch nodejs you type
  78. cd /var/www
  79. ./nodejs.sh
  80. ### To make it work with upstart - make it run on boot
  81. Copy `nodejs.conf` to `/etc/init/`
  82. and modify it to point to nodejs.sh
  83. Upstart is originated in Ubuntu linux. if your linux does not have upstart. An option for you migh be to install upstart.
  84. ### To use upstart you type :
  85. [command] + [init filename without conf extention]
  86. start nodejs
  87. stop nodejs
  88. restrt nodejs
  89. ### When i start to develop I connect to the server with ssh and run:
  90. stop nodejs
  91. cd /var/www
  92. ./nodejs.sh
  93. Then I will start to see application output and errors on the screen
  94. If I want to stop the server I press `Control + C`
  95. and the script stops.
  96. note: the output will not bet visible if logging is configured in node.sh. the output will go to log.
  97. ### Nginx?
  98. Yes I also use Nginx as front. (but it is not required). I use it
  99. to let me in the future to change and integrate different servers seemlessly.
  100. It is basicly: nginx<->nodejs as an upstream.
  101. also i have added php-cgi to nginx to use +Rock mongo+ - a mongodb db editor.
  102. also i've added a log.php , log file viewer so don't even need ssh.
  103. ### Multi Process
  104. you can put nginx or haproxy as a front and create several .conf files for the upstart.
  105. in each you modify the execution line to contain port number ex.:
  106. in the nodejs1.conf
  107. exec sudo -u www-data /bin/bash /var/www/nodejs-mongodb-app/nodejs.sh 8001
  108. in the nodejs2.conf
  109. exec sudo -u www-data /bin/bash /var/www/nodejs-mongodb-app/nodejs.sh 8002
  110. and make port as an argument to your server.js.
  111. //http.createServer(server_handler_function).listen(process.argv[2]||8001);
  112. //see http://nodejs.org/api.html#process-argv-58
  113. //process.argv.forEach(function (val, index, array) {
  114. // console.log(index + ': ' + val);
  115. //});
  116. To achive best performance. it was found by testing (during development of twisted and nginx) that the number of processors should much the number of cores not more not lest.
  117. ### the idea behind the architecture
  118. the idea is to add an extra level of fail-safety by using a stable system shell script to restart node instead of node itself.