forked from topcoder-archive/tc-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers.sh
executable file
·107 lines (92 loc) · 1.98 KB
/
workers.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
ACTION=$1
SIGNAL=""
pid=$(ps ax | grep startCluster | grep -v grep | xargs | cut -f1 -d' ')
if [ "$pid" == "" ]; then
echo "No cluster master process found."
exit
fi
function workers_ps {
ps_out=$(ps ax | grep -- "actionhero start$" | grep -v grep)
IFS='
'
IFS=${IFS:0:1}
workers=( $ps_out )
}
workers_ps
wpids=()
for worker in "${workers[@]}"
do
wpids+=("$(echo -e "${worker}" | xargs | cut -f1 -d' ')")
done
if [ "$ACTION" == "reload" ]; then
SIGNAL="HUP"
elif [ "$ACTION" == "add" ]; then
SIGNAL="TTIN"
elif [ "$ACTION" == "rm" ]; then
SIGNAL="TTOU"
elif [ "$ACTION" == "kill" ]; then
for wpid in "${wpids[@]}"
do
kill -9 $wpid # kill the workers and let master restart
done
exit
elif [ "$ACTION" == "recycle" ]; then
numWorkers=${#wpids[@]}
echo "Start Recycle, Workers Running: ${#workers[@]}"
for wpid in "${wpids[@]}"
do
kill -TTIN $pid # add one
sleep 1
done
declare -i sleepTime
sleepTime=$numWorkers*2
sleep $sleepTime
workers_ps
echo "Max Workers Running: ${#workers[@]}"
# signal recycle of all
kill -HUP $pid
sleepTime=$numWorkers*2
sleep $sleepTime
for wpid in "${wpids[@]}"
do
kill -TTOU $pid # remove one
sleep 1
done
sleep $sleepTime
workers_ps
echo "End Recycle, Workers Running: ${#workers[@]}"
exit
elif [ "$ACTION" == "ls" ]; then
for worker in "${workers[@]}"
do
echo -e "${worker}" | xargs | cut -f1,4 -d' '
done
echo "Count: ${#wpids[@]}"
exit
elif [ "$ACTION" == "count" ]; then
echo "${#wpids[@]}"
exit
elif [ "$ACTION" == "double" ]; then
for wpid in "${wpids[@]}"
do
kill -TTIN $pid # add one
sleep 1
done
exit
elif [ "$ACTION" == "halve" ]; then
for ((i = 0 ; i < ${#wpids[@]}/2 ; i++ ))
do
kill -TTOU $pid # remove one
sleep 1
done
exit
elif [ "$ACTION" == "debug" ]; then
kill -USR1 $2
exit
else
echo "Usage: workers.sh [reload|recycle|add|rm|ls|count]"
exit
fi
kill -$SIGNAL $pid
echo "Signal $SIGNAL sent to process: $pid"