Rem*_*.co 56 deployment updates node.js
我们目前正在为一个客户开发一个网站(Apache下的TYPO3),该网站由node.js/socket.io应用程序提供支持,该应用程序为CMS提供的内容提供实时更新.
由于这是我们的第一个node.js项目,因此我在"完美设置"方面没有任何最佳实践,因此我花了一些时间研究部署技术.
我仍然有几个问题可以实现一个良好的设置:
易于部署客户.这一点非常重要,因为我们的网站将整合到他们的"实时"TYPO3安装中,该安装服务于大量网站,并且运行在不是由客户管理的服务器上,而是另一个(集中式)组织,它支持呼叫和服务器更改.进程缓慢.
应该很容易更新.如上所述,请求重新启动并进行服务器更改是一个缓慢的过程,因此理想情况下,节点安装应该在收到推送到实时安装的更改时重新启动/更新git.
部署
在普遍的共识似乎是使用forever,当涉及到部署节点的应用程序,防止他们逃跑.我已经测试了forever,并且在npm install forever -g(全局)安装时似乎工作正常.这需要外部协助来全局安装在实时环境中,所以我更喜欢从应用程序的node_modules目录运行它,但是我无法创建一个可靠的包装器来执行此操作.
此外,forever工作正常,但必须手动启动.什么是确保它在服务器启动时启动并保持运行的最佳方法?
init.d脚本?forever状态? 快速开发/重新启动更新
我们目前仍处于项目的开发阶段,每次我对node.js应用程序进行更改时,我都会手动重启node或forever.这有效,但远非理想.有几个较小的npm模块可以检查文件修改并node在检测到的更改时重新启动,例如:
有没有人有这些经验?
更新:你为什么不只使用群集?
该集群模块通过提供类似的功能重载机制,但不与节点0.5+工作.替换它的核心群集模块(节点0.6+)没有所有这些功能,只提供群集.而这反过来又不适合socket.io.至少没有使用Redis(这对我们来说是一个问题,因为我们不能强迫客户使用另一个先决条件服务).
-
显然,我正在尝试找到最稳定的解决方案,它将更新重启器与forever将项目交给客户之前结合起来,我真的希望有人能够生成经过验证的技术组合.
Rem*_*.co 63
结合所有收集的知识(非常感谢Julian Knight的想法)和过去一周测试的方法,我决定采用下面描述的部署解决方案(我认为我很乐意分享以帮助其他人提出类似的问题):
脚本错误的自动重启和脚本更改的自动重新加载是永远处理的 ,因为它还包括一个脚本监视,只要Forever是从node.js脚本中生成的.
为此,我添加了一个server.js以启动app.js我们实际想要运行的脚本:
server.js
var forever = require('forever'),
child = new(forever.Monitor)('app.js', {
'silent': false,
'pidFile': 'pids/app.pid',
'watch': true,
'watchDirectory': '.', // Top-level directory to watch from.
'watchIgnoreDotFiles': true, // whether to ignore dot files
'watchIgnorePatterns': [], // array of glob patterns to ignore, merged with contents of watchDirectory + '/.foreverignore' file
'logFile': 'logs/forever.log', // Path to log output from forever process (when daemonized)
'outFile': 'logs/forever.out', // Path to log output from child stdout
'errFile': 'logs/forever.err'
});
child.start();
forever.startServer(child);
Run Code Online (Sandbox Code Playgroud)
这将监视应用程序目录中的所有文件以进行更改,并在更改后立即重新启动运行的脚本forever.由于日志和pidfile位于应用程序的子目录中,因此必须从文件监视中忽略这些日志,否则脚本将循环重新启动:
.foreverignore
pids/**
logs/**
Run Code Online (Sandbox Code Playgroud)
为了使这一切从系统启动开始,使我们能够轻松控制服务start node-app,stop node-app我们使用Ubuntu的Upstart.我把两个例子(这个和这一个)组合成一个很好地完成工作的例子:
/etc/init/node-app.conf
# This is an upstart (http://upstart.ubuntu.com/) script
# to run the node.js server on system boot and make it
# manageable with commands such as
# 'start node-app' and 'stop node-app'
#
# This script is to be placed in /etc/init to work with upstart.
#
# Internally the 'initctl' command is used to manage:
# initctl help
# initctl status node-app
# initctl reload node-app
# initctl start node-app
description "node.js forever server for node-app"
author "Remco Overdijk <remco@maxserv.nl>"
version "1.0"
expect fork
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env HOME=/home/user/node-app-dir
script
# Not sure why $HOME is needed, but we found that it is:
export HOME=$HOME
chdir $HOME
exec /usr/local/bin/node server.js > logs/node.log &
end script
#post-start script
# # Optionally put a script here that will notifiy you node has (re)started
# # /root/bin/hoptoad.sh "node.js has started!"
#end script
Run Code Online (Sandbox Code Playgroud)
正如Kevin明智地在他的文章中提到的那样以root身份运行节点是不明智的,所以我们将把它改为exec sudo -u www-data /usr/local/bin/node下周转移到新服务器的时候.
因此,forever启动自动node server.js启动upstart,并监视崩溃和文件更改,保持整个设置运行,只要我们想要.
我希望这可以帮助任何人.
因为我的最后一个答案是为了将来!以下是一些其他辅助链接:
似乎还没有一个完美的答案,但有很多人在运行生产节点实例.希望这会指出你正确的方向.
| 归档时间: |
|
| 查看次数: |
22220 次 |
| 最近记录: |