production
3.13 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/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
}
app_server_start() {
ruby -S bundle exec unicorn_rails \
--config-file lib/noosfero/unicorn.rb \
--env "$RAILS_ENV" \
--daemonize
}
app_server_stop() {
# see unicorn_rails(1)
kill -s QUIT $(cat tmp/pids/unicorn.pid)
}
app_server_restart() {
# see unicorn_rails(1) and "Signal handling" in unicorn documentation
kill -s USR2 $(cat tmp/pids/unicorn.pid)
sleep 5
kill -s QUIT $(cat tmp/pids/unicorn.pid.oldbin)
}
do_start() {
bundle exec rake db:migrate SCHEMA=/dev/null
clear_cache
environments_loop start
app_server_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.
app_server_stop ||
stop_via_pid_file tmp/pids/unicorn.pid
environments_loop stop ||
stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid
}
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
clear_cache
app_server_restart
environments_loop start
}
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
./script/whenever $action $env
done
else
bundle exec ./script/delayed_job "$action"
bundle exec ./script/feed-updater "$action"
./script/whenever $action
fi
}
do_running() {
pids=$(sed "s/.*/& /" tmp/pids/unicorn.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
;;
*)
# `running` is not listed on purpose since it's not supposed to be a public
# API.
echo "usage: $0 start|stop|restart|run"
exit 1
;;
esac