A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://www.benjiegillam.com/2011/08/node-js-clean-restart-and-faster-development-with-nodemon/ below:

Node.JS: Clean Restart and Faster Development With Nodemon

If you’re using nodemon to automatically restart your node/coffee server process when the source code changes (and if you’re not, why not?), you may find (as I did) that it can kill the script quite abruptly. If you want to do something when the code shuts down (close connections, flush to database/disk/redis/memcached, etc) then you can do so by intercepting the SIGUSR2 signal that nodemon uses to kill your script, and then setting up these actions. Once these actions are complete, you can exit but nodemon will see that as a crash and stop restarting. So you then have to kill your app again with SIGUSR2. Here’s the solution I use in CoffeeScript (click Toggle for JavaScript):

.coffee.jsCleanly restart on nodemon SIGUSR2 (.coffee)
1
2
3
4
5
6
7
8
9
10
11
12
# Alias for setTimeout that reorders parameters to look neater in CoffeeScript
delay = (ms, cb) -> setTimeout cb, ms
# Handle SIGUSR2, but only once
process.once 'SIGUSR2', ->
  console.log 'Doing shutdown tasks...'
  delay 5000, ->
    console.log 'All done, exiting'
    # Kill ourself with the SIGUSR2 signal again
    process.kill process.pid, 'SIGUSR2'
# This next line just to prevent the app exiting in case you want to use this as a demo
delay 99999999, ->
  console.log 'App exiting naturally'

By the way, to install nodemon globally: sudo npm install -g nodemon


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4