Nodemon + Forever + Port Binding #30

dnunes opened this Issue Jul 19, 2011 · 8 comments


3 participants

dnunes commented Jul 19, 2011

. . Hi there. Currently I have a very specific setup in the machine running the app I'm working on. Sadly I don't have direct access to the server and I need the app to run as smoothly as possible, but I also need a way to update the code. Since I can't access the server to restart the process, I used forever (in Node.JS) to keep it running.
. . As I needed to update the server frequently, I wrapped the "forever.js" script (that runs my "server.js"--that runs a HTTP and Socket servers) in a nodemon process.
. . The thing is: as I change a file, the nodemon process restart the forever.js, but then the forever.js runs an infinite loop of error/respawn with the following error message:

{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EADDRINUSE, Address already in use',
errno: 98,
syscall: 'bind' }

. . If I manually do a Ctrl+C and restart the nodemon command ("nodemon -d 6 forever.js") it start ok again (even when it's in the forever error loop), but as soon as I change a file, my script goes into the "address in use" error loop again. I don't know if nodemon is not actually closing the process and the port bind hangs or something... If you have any tips I would greatly appreciate it.

. . I though about not using the forever.js (running "nodemon -d 6 server.js" rather then "forever.js") because when starting this way the server restarts ok if I save a file, but I really need the forever.js to restart my server automatically if an error occurs because I can't afford the downtime until I manually upload a file again (so that nodemon restart my server).

. . PS: it is worth mentioning that my server restarts ok when an error occurs if I run "node forever.js" directly, without "nodemon", so the problem seems to be using both of them together.

. . Do you have any tips?


remy commented Oct 22, 2011

I appreciate you opened this donkey's ago - but TBH I'd have to play with forever and nodemon myself to see this. My gut says that forever hasn't really exit properly (perhaps correctly so).

Have you tried changing the order of apps, instead of nodemon monitoring forever, switch it around, put forever on nodemon, and run as:

forever nodemon app.js

That way forever ensures nodemon remains up, and nodemon will restart on file changes.

That said, nodemon is designed to wait if there's a crash on the app.js - so it won't restart your app. If that's the case, I think you might need a custom fork of nodemon that doesn't hang on exceptions - and instead just exits fully and only restarts the app.js on file changes.

Yes, the changes you recommended I think are what would need to happen here


remy commented Jan 11, 2012

The only way I can see this working is if nodemon exits on a crash - I could add an option to exit on crash that way forever can reboot the nodemon + script combo - but I'd be wary you might get in a bit of a loop (that said - without nodemon you'd hit the potential loop I guess).

If you confirm - I'll add it.

dnunes commented Jan 13, 2012

. . Remy, with nodemon exiting, we can wrap nodemon on forever and it will work the same way I tried to achieve wrapping the forever on nodemon. Currently, if you wrap nodemon on a forever script, the "forever" part will never run: when you change a file nodemon will reboot the script and when a crash occur nodemon will wait a file change.
. . If you can add this option it would be perfect.

. Thanks.


remy commented Jan 13, 2012

I'm not really clear which way around you mean when you say "on". Can you show me command line examples.

That said, I think what you (ideally) want is this:

nodemon -x forever start server.js

The problem is this: forever forks itself as a daemon process, and the child process exits. Which is fine for nodemon now, as it'll sit and wait until there are file changes. When it picks up a change, forever is restarted - but since the forever child process actual did exit - it's just started (this line of code, the child is null). So forever is started again - which causes multiple versions of forever to run.

However! If you wrap forever around nodemon with the --exitcrash flag it does work:

forever nodemon --exitcrash server.js

Now if any files are changed in the working directory, nodemon restarts the server.js. If server.js crashes (test it by killing the process by PID), nodemon exists and forever restarts everything.

I think that's the right combo.

dnunes commented Jan 13, 2012

. . Sorry I didn't make it clear enough (language barrier). I meant exactly that: wrap nodemon inside a forever process ("forever nodemon script.js"). But for that to work as we are discussing on the thread, nodemon must exit if a crash occur.
. . Is that "exitcrash" flag already available? Nightly build? Git clone? Or is it already on a release?


remy commented Jan 13, 2012

--exitcrash is on the latest release - but actually (as someone pointed out) you can do all this in forever:

forever -w start server.js

I'll test this - and if that's the case I'm likely to remove this feature as it's bespoke to forever.

remy closed this Jan 13, 2012

dnunes commented Jan 16, 2012

. . Yeap. Forever seems to have incorporated this functionality (restarting on file change, while keeping the service running).

. . Thanks for the attention on this subject, guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment