Node.js arguments when using forever - node.js

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.

Related

Running forever script from Google Cloud Platform App Engine startup script

I have edited the startup-script variable for one of my instances running on the Google Cloud Platform App Engine. I'd like it to call a forever script to make sure my node app is running. So I added:
cd /opt/bitnami/apps/myapp
forever start --workingDir /opt/bitnami/apps/myapp/ --sourceDir /opt
/bitnami/apps/myapp/ app.js
after the #!/bin/bash line (also tried without the cd as it's not really necessary based on my command). But once the vm is started, running a forever list doesn't list my forever task as having ever started. If I copy and paste that forever command into a gcloud terminal and run, the task shows up fine and my app starts no problem.
Am I not calling this correctly somehow within the bash script?
The simple answer is that GAE does this by default. No need for forever or PM2. There are certain health checks that GAE does on the Docker container holding your app, and if they do not pass the instance is automatically restarted
If you want granular control over these checks (called Legacy Health Checks) you can add this to your app.yaml file:
health_check:
enable_health_check: True
check_interval_sec: 5
timeout_sec: 4
unhealthy_threshold: 2
healthy_threshold: 2
There is also updated mechanisms (called Updated Health Checks) that are still in beta, but can be used instead
The proper way to start your nodejs app on appengine is to specify the "scripts" field in your package.json, as the documentation
Below is an example borrowed from this sample
"scripts": {
"start": "node ./bin/www",
"test": "cd ..; npm run t -- appengine/analytics/test/*.test.js"
},
If you however, are only interested in running a node script, and not interested in the features that come with Google app engine, then you may simply run it on a Google Compute Engine instance.

Forever with npm start and environment variable

I used to start my application using:
DEBUG=chakka ENVIRONMENT=production npm start
How can i start it using forever so i wouldn't have to do it everytime i want to test the application? Thanks!
First you need to know what the application's main script file is. Open up your package.json file and find out what the start script is. If you're using Express it might be app.js. So we'll assume app.js for this example, replace with whatever your file is.
To start the application:
DEBUG=chakka ENVIRONMENT=production forever start app.js
to restart the application after you've made changes:
forever restart app.js

Node.js forever with environment variable

The command I run on my server to start my node app is:
sudo IS_PROD=1 node app.js
I have forever installed but can't seem to pass in the environment variable.
sudo IS_PROD=1 forever node app.js
Doesn't seem to do the trick. I have tried several varieties of this. How do I either execute this command successfully or permanently set the environment variable?
First of all you should skip the node thing in you command, it should not be there, you should not be able to execute that. forever automatically starts your script using nodejs. Instead you should do like this;
sudo IS_PROD=1 forever app.js
Probably you, instead of starting your server in foreground, will want to start your server as a daemon. eg.
sudo IS_PROD=1 forever start app.js
This will create a process in the background that will watch your node app and restart it when it exits. For more information see the readme.
Both of these methods preserves the environment variables, just like when you are just using node.
app.js:
console.log(process.env.IS_PROD);
Using node (v0.8.21)
$ node app.js
undefined
$ IS_PROD=1 node app.js
1
$ sudo IS_PROD=1 node app.js
1
Using forever (v0.10.0)
$ forever app.js
undefined
$ IS_PROD=1 forever app.js
1
$ sudo IS_PROD=1 forever app.js
1
Documentation:
process.env
An object containing the user environment. See environ(7).

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