简体   繁体   English

部署 node.js/express 服务器时 ApplicationStart 上的 AWS Codedeploy 超时

[英]AWS Codedeploy timesout on ApplicationStart when deploying a node.js / express server

I have run into a problem when trying to setup an AWS Codepipline.我在尝试设置 AWS Codepipline 时遇到了问题。 My ApplicationStart script makes a call to start the express server listening on port 60900, but because the express.listen() holds the command line up while it listens the ApplicationStart script times out and my deployment fails.我的 ApplicationStart 脚本调用以启动侦听端口 60900 的快速服务器,但是因为 express.listen() 在侦听 ApplicationStart 脚本时保持命令行状态并且我的部署失败。

I've tried moving it to a background process with an & at the end of the command that starts the server, but I'm still getting the error at the ApplicationStart hook.我尝试在启动服务器的命令末尾使用 & 将它移动到后台进程,但我仍然在 ApplicationStart 挂钩处收到错误。

When I run the my start_server.sh script manually it almost instantly starts the server up and give me back control of the command line.当我手动运行 start_server.sh 脚本时,它几乎立即启动服务器并让我重新控制命令行。

appspec.yml appspec.yml

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/mbmbam.app/
hooks:
  BeforeInstall:
    - location: scripts/stop_server.sh
      timeout: 300
      runas: root
    - location: scripts/remove_previous.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: scripts/change_permissions.sh
      timeout: 300
      runas: root
    - location: scripts/install_app.sh
      timeout: 300
      runas: root
    - location: scripts/install_db.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/start_server.sh
      timeout: 300
      runas: ubuntu

scripts/start_server.sh脚本/start_server.sh

#!/usr/bin/env bash

NODE_ENV=production npm start --prefix /var/www/mbmbam.app/app

Script assigned to the npm start command分配给 npm start 命令的脚本

app/start_app.sh应用程序/start_app.sh

#!/bin/sh

if [ "$NODE_ENV" = "production" ]; then
  node server.js & 
else
  nodemon --ignore './sessions' server.js;
fi

AWS Codedeploy error AWS Codedeploy 错误

LifecycleEvent - ApplicationStart
Script - scripts/start_server.sh
[stdout]
[stdout]> mbmbam-search-app@1.0.0 start /var/www/mbmbam.app/app
[stdout]> ./start_app.sh
[stdout]

Any help would be appreciated.任何帮助,将不胜感激。 I've been stuck on this for a day or so.我已经坚持了一天左右。

I solved it by changing the start_app.sh to我通过将 start_app.sh 更改为

#!/bin/sh

if [ "$NODE_ENV" = "production" ]; then
  node server.js > app.out.log 2> app.err.log < /dev/null & 
else
  nodemon --ignore './sessions' server.js;
fi

Looks like it AWS even listed it in their troubleshooting steps here: https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-long-running-processes看起来 AWS 甚至在此处的故障排除步骤中列出了它: https : //docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-long-running-processes

The issue seems to be node not going cleanly in the backgroud.问题似乎是节点在背景中没有干净利落。

Can you try the following way to start node server in 'app/start_app.sh':您可以尝试以下方式在“app/start_app.sh”中启动节点服务器:

$ nohup node server.js > /dev/null 2>&1 &

Also I would suggest to look at making your node process a service so it is started if the server is rebooted:此外,我建议考虑让您的节点处理服务,以便在服务器重新启动时启动它:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM