-
Notifications
You must be signed in to change notification settings - Fork 5
/
sidekiq.sh
executable file
·90 lines (72 loc) · 2.06 KB
/
sidekiq.sh
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
#!/bin/sh
ACTION=$1
RETRY_LIMIT=3
RETRY_WAIT=5
start_function(){
bundle exec sidekiq -C config/sidekiq.yml
}
stop_function(){
# SIGTERM triggers a quick exit; gracefully terminate instead.
# Find PID
SIDEKIQ_PID=$(ps aux | grep sidekiq | grep busy | awk '{ print $2 }')
if [ -z "$SIDEKIQ_PID" ]; then
echo "No Sidekiq PIDs found."
else
echo "Sending TSTP signal..."
kill -TSTP ${SIDEKIQ_PID}
# Wait until it finishes all the jobs and then send a TERM signal to it
wait_function
echo "Sending TERM signal..."
kill -TERM ${SIDEKIQ_PID}
fi
}
wait_function(){
sleep 2
i=0
while [ ${i} -lt ${RETRY_LIMIT} ]; do
i=$((i+1))
IS_SIDEKIQ_DONE=$(sidekiqmon processes | grep -q "(0 busy)"; echo $?)
if [ ${IS_SIDEKIQ_DONE} -eq 0 ]; then
echo "Sidekiq finished all jobs."
break
else
echo "Sidekiq is still busy. Retry $i/$RETRY_LIMIT Waiting $RETRY_WAIT seconds..."
sleep ${RETRY_WAIT}
fi
done
}
# This is used in the kubernetes deployment readinessProbe and livenessProbe.
probe_function(){
MONITOR_OUTPUT=$(sidekiqmon processes) || exit 1
# Check redis connection
REDIS_CHECK=$(echo ${MONITOR_OUTPUT} | grep -q "ECONNREFUSED"; echo $?)
# Check there is at least 1 process running
PROCESSES_CHECK=$(echo ${MONITOR_OUTPUT} | grep -q "Processes (0)"; echo $?)
# Check there is more than 1 thread running (normally will be 3)
THREADS_CHECK=$(echo ${MONITOR_OUTPUT} | grep -qE "Threads: \b[0-1]\b"; echo $?)
# If any of the above checks returns '0' it means the regex found a match
if [ ${REDIS_CHECK} -eq 0 ] || [ ${PROCESSES_CHECK} -eq 0 ] || [ ${THREADS_CHECK} -eq 0 ]; then
echo 'Sidekiq probe: KO'
exit 1
fi
echo 'Sidekiq probe: OK'
}
if [ -z "$REDIS_URL" ]; then
echo 'REDIS_URL env variable not set. Defaulting to redis://localhost:6379'
fi
case "$ACTION" in
start)
start_function
;;
stop)
stop_function
;;
restart)
stop_function && start_function
;;
probe)
probe_function
;;
*)
echo "Usage: $0 [start|stop|restart|probe]"
esac