-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler
executable file
·105 lines (88 loc) · 2.18 KB
/
scheduler
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
#!/bin/bash
#
# init script for resque workers
# sample usage:
# ./script/shared/worker start update_staging_db qed_api development
# ==================
# = Configurations =
# ==================
# Print commands and their arguments as they are executed.
# use only for development
# set -x
# ===========
# = Exports =
# ===========
export RAILS_ENV=$3
export RACK_ENV=$3
export QUEUE="$2_scheduler_$3"
# ========================
# = Set global variables =
# ========================
CMD=$1
APPLICATION=$2
if [ "$4" == "--no-env" ]; then
ENVIRONMENT=""
else
ENVIRONMENT="environment"
fi
# Set application root according to rails environment
case $RAILS_ENV in
production)
APPLICATION_ROOT="/var/www/$APPLICATION/current"
;;
beta)
APPLICATION_ROOT="/var/www/$APPLICATION/current"
;;
*)
APPLICATION_ROOT=`pwd`
;;
esac
LOG="$APPLICATION_ROOT/log/resque-scheduler.log"
PID_FILE="/tmp/resque-scheduler."$APPLICATION"_scheduler_"$RAILS_ENV".pid"
# ============
# = Programm =
# ============
function start_worker {
cd $APPLICATION_ROOT
RESQUE_SCHEDULER_MASTER_LOCK_PREFIX=$APPLICATION bundle exec rake $ENVIRONMENT resque:scheduler >> $LOG 2>&1 &
PID=$!
echo 'Waiting for worker to start...' >> $LOG
# We need to wait some undetermined time to load the environment
# only after that we can actually be sure the worker started
# successfully (or not)
sleep 15
active=`ps axo pid | grep $PID`
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "Error starting worker on Resque-Scheduler." >> $LOG
else
echo $PID > $PID_FILE
echo "Started Worker $PID for Resque-Scheduler." >> $LOG
fi
}
function kill_workers {
PATTERN="resque-scheduler-*"
worker_count=`pgrep -f $PATTERN | wc -l`
if [ $worker_count -eq 0 ]; then
echo "No workers on schedule." >> $LOG
else
echo "Sending SIGQUIT to $worker_count on schedule..." >> $LOG
pkill -f $PATTERN &> /dev/null
fi
rm $PID_FILE &> /dev/null
}
echo "`date +'%F %T'` Resque worker init script" >> $LOG
case $CMD in
start)
start_worker
exit 0
;;
stop)
kill_workers
exit 0
;;
*)
echo 'Usage: script/scheduler {start|stop|terminate} application rails-env'
exit 0
;;
esac