Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forever.js starting and restarting multiple scripts

My web app has 3 main node.js components: website, feeds and jobs.

To start these I am using forever:

//forever.js

var forever = require('forever');

function start(name){

  forever.start( ['coffee', name + '.coffee'], { /* log options */ } )

};

start('website');
start('feeds');
start('jobs');

What I first noticed is that if I run script it wont run it as a daemon. ( Which is most likely normal )

node forever.js

So what I did next was run the forever.js script with forever. I am not sure if this is correct, there is also a forever.startDaemon so not sure which one I should use.

forever start forever.js

This works but the problem is that I would like to restart all the processes when a new version of my app is published. I am using git's post-receive hook to run the forever.js the first time but if I do this on each post-recieve it will just spawn 3 processes each time.

So I guess I need a way to restart 3 processes if they are already running. I thought to do this with forever.list but the documentation only say:

forever.list (format, callback)

Returns a list of metadata objects about each process that is being run using 
forever. This method is synchronous and will return the list of metadata as such.
Only processes which have invoked forever.startServer() will be available from
forever.list()

First of all I am not sure what format means and second it expects a callback but then it says its synchronous. Which is a little confusing and I am not sure how to use list.

In the end all I want to do is start/restart 3 node.js processes on git's post-receive hook.

like image 293
Pickels Avatar asked Apr 08 '12 17:04

Pickels


3 Answers

I think the best way to do this is:

forever start website.js
forever start feeds.js
forever start jobs.js

and then in your git post-receive hook:

forever restart website.js
forever restart feeds.js
forever restart jobs.js

Wrapping these node processes inside a single process is not a good idea. I now personally use Supervisord with monit instead of forever (supervisord is more stable & powerful than forever IMHO).

like image 112
FGRibreau Avatar answered Nov 12 '22 11:11

FGRibreau


I do it like this:

#!/bin/sh

# Make sure we're in the right place
DIR=$(cd $(dirname "$0"); pwd)
cd $DIR
echo "[ I am $USER and I changed PWD to $DIR ]"

forever restart --spinSleepTime=2000 api_daemon.js || (forever start --spinSleepTime=2000 api_daemon.js && forever list)

Works like a charm, I never get duplicate processes using ./run.sh

To read logs, I use tail -f /path/to/.log

like image 44
flatroze Avatar answered Nov 12 '22 12:11

flatroze


Yes, it's possible. You need to use npm run forever command to run a script.

Add this to your package.json

"scripts": {
    "forever" : "forever start api/api-server.js && forever start www/www-server.js && forever start upload/upload-server.js && forever start static/static-server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
}

You can create package.json using npm init

like image 43
Uday Hiwarale Avatar answered Nov 12 '22 11:11

Uday Hiwarale