#!/bin/sh set -e if [ -e /etc/default/noosfero ]; then . /etc/default/noosfero fi export RAILS_ENV=production ACTION="$1" if [ -z "$ACTION" ]; then echo "usage: $0 start|stop|restart|run" exit 1 fi clear_cache() { if test -w ./public; then echo "Cleaning cache files" rm -rf ./public/javascripts/cache* rm -rf ./public/stylesheets/cache* elif [ ! -z "$NOOSFERO_DATA_DIR" ] && [ -w "$NOOSFERO_DATA_DIR" ]; then echo "Cleaning cache files" rm -rf "$NOOSFERO_DATA_DIR"/cache/* fi } do_start() { bundle exec rake db:migrate SCHEMA=/dev/null clear_cache environments_loop start bundle exec whenever --write-crontab --set 'environment=production' ruby -S bundle exec thin -C config/thin.yml start } do_stop() { # During Debian upgrades, it is possible that rails is not available (e.g. # Lenny -> Squeeze), so the programs below might fail. If they do, we fall # back to stopping the daemons by manually reading their PID files, killing # them and wiping the PID files. ruby -S bundle exec thin -C config/thin.yml stop || stop_via_pid_file tmp/pids/thin.*.pid environments_loop stop || stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid bundle exec whenever --clear-crontab } do_restart() { bundle exec rake db:migrate SCHEMA=/dev/null environments_loop stop || stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid environments_loop start clear_cache ruby -S bundle exec thin -C config/thin.yml restart --onebyone } stop_via_pid_file() { for pidfile in $@; do if [ -e "$pidfile" ]; then pid=$(cat $pidfile) echo "Sentign TERM signal to stop $pid ..." kill -TERM "$pid" rm -f $pidfile fi done } environments_loop() { action="$1" environments=$(find ./config/environments -name "*_${RAILS_ENV}.rb") if [ "$environments" ]; then for environment in $environments; do env=$(basename $environment | cut -d. -f1) RAILS_ENV=$env bundle exec ./script/delayed_job -i $env "$action" RAILS_ENV=$env bundle exec ./script/feed-updater "$action" -i $env done else bundle exec ./script/delayed_job "$action" bundle exec ./script/feed-updater "$action" fi } do_running() { pids=$(cat tmp/pids/thin.*.pid 2>/dev/null || true) # passes if any of $pids exist, fails otherwise kill -0 $pids > /dev/null 2>&1 } case "$ACTION" in start|stop) do_$ACTION ;; run) do_start echo "=> Running in production mode. Hit ctrl-C to stop." trap do_stop INT TERM tail -n 0 -f log/production.log || true ;; restart) do_restart ;; running) do_running ;; *) echo "usage: $0 start|stop|restart|run" exit 1 ;; esac