83 lines
1.9 KiB
Bash
Executable File
83 lines
1.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# make sure we kill all child processes once done
|
|
trap '{ pkill -P $$; rm -f config/nginx.conf; exit 255; }' EXIT
|
|
|
|
if [ -f bin/nginx ]; then
|
|
nginx=bin/nginx
|
|
else
|
|
which nginx &>/dev/null || { echo "nginx not found" && exit 1; }
|
|
nginx=nginx
|
|
fi
|
|
|
|
psmgr=$tmp_dir/nginx-buildpack-wait
|
|
rm -f $psmgr
|
|
mkfifo $psmgr
|
|
|
|
#Evaluate config to get $PORT
|
|
erb config/nginx.conf.erb > config/nginx.conf
|
|
|
|
n=1
|
|
while getopts :f option ${@:1:2}
|
|
do
|
|
case "${option}"
|
|
in
|
|
f) FORCE=$OPTIND; n=$((n+1));;
|
|
esac
|
|
done
|
|
|
|
#Initialize log directory.
|
|
mkdir -p logs/nginx
|
|
touch logs/nginx/access.log logs/nginx/error.log
|
|
echo 'buildpack=nginx at=logs-initialized'
|
|
|
|
#Start log redirection.
|
|
(
|
|
#Redirect NGINX logs to stdout.
|
|
tail -qF -n 0 logs/nginx/*.log
|
|
echo 'logs' >$psmgr
|
|
) &
|
|
|
|
#Start App Server
|
|
(
|
|
#Take the command passed to this bin and start it.
|
|
#E.g. bin/start-nginx bundle exec unicorn -c config/unicorn.rb
|
|
COMMAND=${@:$n}
|
|
echo "buildpack=nginx at=start-app cmd=$COMMAND"
|
|
$COMMAND
|
|
echo 'app' >$psmgr
|
|
) &
|
|
|
|
if [[ -z "$FORCE" ]]
|
|
then
|
|
FILE="$tmp_dir/app-initialized"
|
|
|
|
#We block on app-initialized so that when NGINX binds to $PORT
|
|
#are app is ready for traffic.
|
|
while [[ ! -f "$FILE" ]]
|
|
do
|
|
echo 'buildpack=nginx at=app-initialization'
|
|
sleep 1
|
|
done
|
|
echo 'buildpack=nginx at=app-initialized'
|
|
fi
|
|
|
|
#Start NGINX
|
|
(
|
|
#We expect nginx to run in foreground.
|
|
#We also expect a socket to be at $tmp_dir/nginx.socket.
|
|
echo 'buildpack=nginx at=nginx-start'
|
|
$nginx -p . -c config/nginx.conf
|
|
echo 'nginx' >$psmgr
|
|
) &
|
|
|
|
#This read will block the process waiting on a msg to be put into the fifo.
|
|
#If any of the processes defined above should exit,
|
|
#a msg will be put into the fifo causing the read operation
|
|
#to un-block. The process putting the msg into the fifo
|
|
#will use it's process name as a msg so that we can print the offending
|
|
#process to stdout.
|
|
read exit_process <$psmgr
|
|
echo "buildpack=nginx at=exit process=$exit_process"
|
|
exit 1
|