Skip to content

Instantly share code, notes, and snippets.

@ipoval
Created July 26, 2015 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipoval/764ed65d4ed16aa309bc to your computer and use it in GitHub Desktop.
Save ipoval/764ed65d4ed16aa309bc to your computer and use it in GitHub Desktop.
start rails unicorn web server in foreground
#!/usr/bin/env bash
# Run unicorn in the foreground and customize how it runs.
#
# Usage: ./script/start_rails_fg.bash
# ./script/start_rails_fg.bash --help
# supports these env variables: UNICORN_PID
# UNICORN_LISTEN
# UNICORN_STDERR_PATH
# UNICORN_STDOUT_PATH
# UNICORN_TIMEOUT
# UNICORN_WORKERS
#
function wf_fg_usage() {
cat <<END
Usage: ./script/start_rails_fg.bash
supported env vars:
UNICORN_PID=/opt/workfeed/tmp/pids/unicorn.pid;
UNICORN_LISTEN=/tmp/workfeed.unicorn.sock;
UNICORN_STDERR_PATH=false;
UNICORN_STDOUT_PATH=false;
UNICORN_TIMEOUT=200000;
UNICORN_WORKERS=1;
END
} >&2
[[ $1 == '--help' ]] && { wf_fg_usage; exit 0; };
[[ $1 ]] && { wf_fg_usage; exit 1; };
function wf_fg_stop_bg_unicorn() {
printf "%s\n" "stopping the unicorns"
sudo stop workfeed 2> /dev/null;
}
function wf_fg_set_env_vars() {
export UNICORN_PID=${UNICORN_PID:-'/opt/workfeed/tmp/pids/unicorn.pid'}
export UNICORN_LISTEN=${UNICORN_LISTEN:-'/tmp/workfeed.unicorn.sock'}
export UNICORN_STDERR_PATH=${UNICORN_STDERR_PATH:-false}
export UNICORN_STDOUT_PATH=${UNICORN_STDOUT_PATH:-false}
export UNICORN_TIMEOUT=${UNICORN_TIMEOUT:-200000} # long timeout to be able to run with debugger
export UNICORN_WORKERS=${UNICORN_WORKERS:-1}
}
function wf_fg_start_rails() {
declare -i rails_load_env_seconds=15; # measured with `time bundle exec rails runner 'puts Rails.version'`
printf "%s: %d\n" "starting rails in foreground, wait seconds" $rails_load_env_seconds
declare -r local_unicorn_config='/opt/workfeed/config/unicorn.rb'
[[ ! -f $local_unicorn_config ]] || \
{ time bundle exec unicorn --config-file "${local_unicorn_config}"; }
}
wf_fg_stop_bg_unicorn;
wf_fg_set_env_vars;
wf_fg_start_rails;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment