-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker
executable file
·133 lines (113 loc) · 3.07 KB
/
worker
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
#!/bin/bash
#
# init script for resque workers
# sample usage:
# ./script/shared/worker start update_staging_db qed_api development id [--no-env]
# ==================
# = Configurations =
# ==================
# Print commands and their arguments as they are executed.
# use only for development
# set -x
# ===========
# = Exports =
# ===========
# $6 => sub-queue (if we split the work which would go into one queue into sub queues with the same worker
# $5 => id for multiple worker working on the same queue
export RAILS_ENV=$4
export RACK_ENV=$4
CMD=$1
Q=$2
APPLICATION=$3
ENV=$4
if [ -z $6 ]; then
# Append underscore environment to queue for active record like queue name
export QUEUE=$APPLICATION"_"$Q"_"$ENV
else
# attach worker to queue with additional diversifyer (0,1,2 ...)
# useful if we need to split the same work into sub-queues because we need to have
# the same kind of element (idenfied by an id) in the same queue for consistency reasons
# only one worker on such a queue is currently supported
export QUEUE=$APPLICATION"_"$Q"_"$ENV"_"$6
fi
if [ -z $5 ]; then
PID_FILE="/tmp/resque-worker."$QUEUE".pid"
else
PID_FILE="/tmp/resque-worker."$QUEUE"."$5".pid"
fi
# ========================
# = Set global variables =
# ========================
if [ "$8" == "--no-env" ]; then
ENVIRONMENT=""
else
ENVIRONMENT="environment"
fi
if [ -z "$7" ]; then
export JOBS_PER_FORK=1000
else
export JOBS_PER_FORK=$7
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-workers.log"
# ============
# = Programm =
# ============
function start_worker {
cd $APPLICATION_ROOT
bundle exec rake $ENVIRONMENT resque:work >> $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 $QUEUE." >> $LOG
else
echo $PID > $PID_FILE
echo "Started Worker $PID on $QUEUE." >> $LOG
fi
}
function kill_workers {
PATTERN="resque.* $QUEUE"
# if we need it, make a better division between pkill stuff and pid kill
# right now its completly mixed and very error prone
worker_count=`pgrep -f "$PATTERN" | wc -l`
#if [ $worker_count -eq 0 ]; then
# echo "No workers on $QUEUE." >> $LOG
#else
echo "Sending SIGQUIT to $worker_count workers on $QUEUE..." >> $LOG
#pkill -f "$PATTERN" &> /dev/null
kill -15 $(<"$PID_FILE") &> /dev/null
rm $PID_FILE &> /dev/null
#fi
}
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/worker {start|stop|terminate} queue-name application rails-env number-workers'
exit 0
;;
esac