|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +psmgr=/tmp/nginx-buildpack-wait |
| 4 | +rm -f $psmgr |
| 5 | +mkfifo $psmgr |
| 6 | + |
| 7 | +n=1 |
| 8 | +while getopts :f option ${@:1:2} |
| 9 | +do |
| 10 | + case "${option}" |
| 11 | + in |
| 12 | + f) FORCE=$OPTIND; n=$((n+1));; |
| 13 | + esac |
| 14 | +done |
| 15 | + |
| 16 | +# Initialize log directory. |
| 17 | +mkdir -p logs/nginx |
| 18 | +touch logs/nginx/access.log logs/nginx/error.log |
| 19 | +echo 'buildpack=nginx at=logs-initialized' |
| 20 | + |
| 21 | +# Start log redirection. |
| 22 | +( |
| 23 | + # Redirect nginx logs to stdout. |
| 24 | + tail -qF -n 0 logs/nginx/*.log |
| 25 | + echo 'logs' >$psmgr |
| 26 | +) & |
| 27 | + |
| 28 | +# Start App Server |
| 29 | +( |
| 30 | + # Take the command passed to this bin and start it. |
| 31 | + # E.g. bin/start-nginx bundle exec unicorn -c config/unicorn.rb |
| 32 | + COMMAND=${@:$n} |
| 33 | + echo "buildpack=nginx at=start-app cmd=$COMMAND" |
| 34 | + $COMMAND |
| 35 | + echo 'app' >$psmgr |
| 36 | +) & |
| 37 | + |
| 38 | +if [[ -z "$FORCE" ]] |
| 39 | +then |
| 40 | + FILE="/tmp/app-initialized" |
| 41 | + |
| 42 | + # We block on app-initialized so that when nginx binds to $PORT |
| 43 | + # are app is ready for traffic. |
| 44 | + while [[ ! -f "$FILE" ]] |
| 45 | + do |
| 46 | + echo 'buildpack=nginx at=app-initialization' |
| 47 | + sleep 1 |
| 48 | + done |
| 49 | + echo 'buildpack=nginx at=app-initialized' |
| 50 | +fi |
| 51 | + |
| 52 | +# Start nginx |
| 53 | +( |
| 54 | + # We expect nginx to run in foreground. |
| 55 | + # We also expect a socket to be at /tmp/nginx.socket. |
| 56 | + echo 'buildpack=nginx at=nginx-start' |
| 57 | + /usr/bin/nginx -p . -c config/nginx.conf |
| 58 | + echo 'nginx' >$psmgr |
| 59 | +) & |
| 60 | + |
| 61 | +# This read will block the process waiting on a msg to be put into the fifo. |
| 62 | +# If any of the processes defined above should exit, |
| 63 | +# a msg will be put into the fifo causing the read operation |
| 64 | +# to un-block. The process putting the msg into the fifo |
| 65 | +# will use it's process name as a msg so that we can print the offending |
| 66 | +# process to stdout. |
| 67 | +read exit_process <$psmgr |
| 68 | +echo "buildpack=nginx at=exit process=$exit_process" |
| 69 | +exit 1 |
0 commit comments