简体   繁体   中英

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. 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.

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.

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.

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

#!/usr/bin/env bash

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

Script assigned to the npm start command

app/start_app.sh

#!/bin/sh

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

AWS Codedeploy error

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

#!/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

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':

$ 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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