Avoid garbage with nodejs app using foreverjs and running on Heroku - node.js

According to Heroku doc, to avoid garbage, you can provide flags to V8 in your Procfile:
web: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 server.js
However, my app uses foreverjs, and I use the following instruction in the Procfile:
web: ./node_modules/.bin/forever -m 5 server.js
Is there a way to provide flags like --max_old_space_size or gc_interval=100 and still use foreverjs?

I'm using pm2 not forever but as it seems that forever as this syntax :
usage: forever [action] [options] SCRIPT [script-options]
I would try to put those in the script-options part :
./node_modules/.bin/forever -m 5 server.js --max_old_space_size=460 --gc_interval=100
Either way, I wouldn't force the timing of the gc...
The max_old_space_size should be in acordance to the RAM you have available for your node process, and in your case, if your node process take more than ~500M of RAM it should trigger a gc call by himself.

Related

Node.js arguments when using forever

I'm currently using forever to run my node.js application in our development environment. What I am currently struggling with is how to pass node.js arguments when using "forever start"
Here is an example where I need to pass a number and a date to node. It's not working so any help would be appreciated.
forever -c 'node 8010 "2014-11-11 12:00:00"' start app.js
According to the documentation, the script arguments come after the call.
https://github.com/nodejitsu/forever
usage: forever [action] [options] SCRIPT [script-options]
forever start app.js 8010 "2014-11-11 12:00:00"
The usage of nconf https://github.com/flatiron/nconf in your project is highly recommended to grab those params.

How to debug custom node server with brunch.io?

I am using brunch.io with a custom server app.coffee.
This is what my brunch-config.coffee entry looks like:
server:
path: 'app.coffee'
port: 3333
base: '/'
I want to use node's default debugger, but when I type debugger anywhere in my app.coffee, script execution doesn't stop. My debugger statement is simply ignored.
How can I make brunch run my server so that debugger statements are not ignored but pause script execution?
Brunch version: 1.7.18
Coffee version: 1.7.1
Node version: 0.10.30
Thanks for your time!
To use the debugger, it would be best to run brunch watch and your node app in separate terminal instances.
Just run brunch watch without the -s/--server option, and then separately run something like:
coffee --nodejs --debug app.coffee

Start server node.js with forever on ubuntu

I been searching alot and no result with same problem as me.
I have a Node.js application and I want to start it with forever start app.js, the process starts but no website found when i try in browser. The process is in the list when I write forever list.
Npm start works fine but I cant use nodejs/node app.js or my_file.js.. It gives no error or something just new command line with no output in terminal.
So anyone know why I cant start the app with nodejs app.js or forever start app.js .. No files works.
Thanks!
In express 4 you should write :
forever ./bin/www
And if you check your package.json file you can see :
"scripts": {
"start": "node ./bin/www"
}
It's the npm start script
Alternatively, you can try using PM2.
It does a great job at keeping your app alive, and has some really useful features such as load balancing, no downtime, and a web interface to monitor your processes.
In addition, I find it dead simple to use.

Starting Node.js using forever with --nouse-idle-notification and other flags

Previously, I started my production node app via:
NODE_ENV=production forever start index.js
However, per the suggestions in this question, I'd like to start node with --nouse-idle-notification. I also found this article about setting --max-old-space-size, etc. Unfortunately, nobody I ask can seem to figure out how to tell if the flag is actually accepted by node, so I'm not sure how to tell if my forever syntax is correct.
Furthermore, I can't get forever to accept both arguments...
Eg, if I use this
NODE_ENV=production forever start --max-old-space-size=8192 --nouse-idle-notification index.js
I get the "forever usage information", as if I had tried to start forever without passing a .js file to run (eg, just typing "forever"). If I put the flags before the "start" command, it seems to start, but again I'm not sure how to tell if the flags were accepted...
Can someone please help me with the correct syntax?
You need to pass -c parameter:
forever start -c "node --max-old-space-size=8192 --nouse-idle-notification" index.js
If you list the processes, you'll see the flags are honoured.
forever list
Unless you really love forever for some other reason, try mon.
It's super easy to pass flags because you can specify the exact command:
mon "node --max-old-space-size=8192 --nouse-idle-notification --expose-gc server.js" -d
It monitors only a node process. If you want to monitor a group of processes like forever does, install mongroup, its a bash script that manages mon.
This will save you some RAM, specially if you're monitoring a lot of node processes (I think forever launches one additional node process for every process you want to monitor).
quick tip: last time I checked, TJ Holowaychuk's branch of mon was not working well under linux (I guess he only tested on Mac), but this one works and its the one I'm using right now. EDIT: Actually 2 days ago the issue was closed and the main branch should now be working.
You could try:
forever start --max-old-space-size=8192 --nouse-idle-notification -c "NODE_ENV=production node" index.js

Can I tell foreman to reload the web app every time a request is made so I can develop decently?

A web app I am writing in JavaScript using node.js. I use Foreman, but I don't want to manually restart the server every time I change my code. Can I tell Foreman to reload the entire web app before handling an HTTP request (i.e. restart the node process)?
Here's an adjusted version of Pendlepants solution. Foreman looks for an .env file to read environment variables. Rather than adding a wrapper, you can just have Foreman switch what command it uses to start things up:
In .env:
WEB=node app.js
In dev.env:
WEB=supervisor app.js
In your Procfile:
web: $WEB
By default, Foreman will read from .env (in Production), but in DEV just run this:
foreman start -e dev.env
You can use rerun for this purpose
You might implement just 2 commands for this:
gem install rerun
rerun foreman start
Then rerun will automatically restart process after any change in your files.
If you use nodemon
, you can do
nodemon --exec "foreman start"
The problem isn't with Foreman so much as it's with how node doesn't reload code on new requests. The solution is to use an npm package like supervisor along with an environment wrapper for Foreman.
First, install supervisor:
npm install -g supervisor
Then, write a wrapper shell script that Foreman can call:
if [ "$NODE_ENV" == "production" ]; then
node /path/to/app.js
else
supervisor /path/to/app.js
fi
Set the wrapper script's permissions to executable by running chmod a+x /path/to/wrapper_script.sh
Lastly, update foreman to use the wrapper script. So in your Procfile:
web: /path/to/wrapper_script.sh
Now when you run Foreman and your node app isn't running in production, it should reload on every request.
I feel like Peter Ehrlich's comment on the original question deserves to be an answer on its own. I think a different Procfile for local/dev is definitely the best solution: https://stackoverflow.com/a/10790514/133720
You don't even need to install anything new if you use node-dev.
Your .env file loaded from Procfile:
NODECMD=node-dev
Your Procfile:
web: $NODECMD app/server.js
Your foreman command
foreman start -e dev.env -p 9786
And in your production env (heroku) set an environment variable:
NODECMD=node

Resources