Forever.js启动并重新启动多个脚本

时间:2022-12-06 09:35:13

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

我的网络应用程序有3个主要的node.js组件:网站,订阅源和作业。

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.js脚本。我不确定这是否正确,还有一个forever.startDaemon所以不确定我应该使用哪一个。

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.

这有效,但问题是我想在发布我的应用程序的新版本时重新启动所有进程。我正在使用git的post-receive hook来第一次运行forever.js,但如果我在每次post-receive上执行此操作,它每次只会产生3个进程。

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:

所以我想如果它们已经运行,我需要一种重启3个进程的方法。我想用forever.list做这个,但文档只说:

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.

最后,我想要做的是在git的post-receive钩子上启动/重启3个node.js进程。

3 个解决方案

#1


12  

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:

然后在你的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).

将这些节点进程包装在单个进程中并不是一个好主意。我现在亲自使用Supervisord而不是永远(supervisord比永远恕我直言更稳定和强大)。

#2


2  

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

像魅力一样工作,我从来没有使用./run.sh获得重复的进程

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

要读取日志,我使用tail -f /path/to/.log

#3


1  

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

是的,这是可能的。您需要使用npm run forever命令来运行脚本。

Add this to your package.json

将其添加到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

您可以使用npm init创建package.json

#1


12  

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:

然后在你的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).

将这些节点进程包装在单个进程中并不是一个好主意。我现在亲自使用Supervisord而不是永远(supervisord比永远恕我直言更稳定和强大)。

#2


2  

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

像魅力一样工作,我从来没有使用./run.sh获得重复的进程

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

要读取日志,我使用tail -f /path/to/.log

#3


1  

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

是的,这是可能的。您需要使用npm run forever命令来运行脚本。

Add this to your package.json

将其添加到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

您可以使用npm init创建package.json