production
2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/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=$(sed "s/.*/& /" tmp/pids/thin.*.pid 2>/dev/null | tr -d '\n')
# 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