-
Notifications
You must be signed in to change notification settings - Fork 0
/
_worker_manager
executable file
·86 lines (76 loc) · 1.99 KB
/
_worker_manager
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
#!/bin/sh
# ==============================================================================
# This mananger script is used for status/start/stop/restart the app's workers
# that interact with the redis queue; we use pm2 to set them as background tasks
# ==============================================================================
# Load vars defined in .env
source .env
# Give name to workers launched from this deployment environment
# This will be used to select them collectively by pm2
PROC_NAME="woRQer-"$DEPLOYMENT_TIER
# Test if we've already got redis running:
calc_processes() {
PM2_PROCESSES=$(pm2 pid $PROC_NAME) # Store woRQer pids as string
PM2_PROCESSES_ARRAY=($(echo $PM2_PROCESSES)) # Split pids into array entries
PM2_PROCESSES_LENGTH=(${#PM2_PROCESSES_ARRAY[@]}) # Get length of array
return $PM2_PROCESSES_LENGTH
}
calc_processes
d_status() {
if [[ $PM2_PROCESSES_LENGTH -gt 0 ]]; then
echo -e """${BLU}
PM2 is running "$PM2_PROCESSES_LENGTH" redis-queue-task workers.
Worker PIDs: $(echo $PM2_PROCESSES)
Run \`pm2 list $PROC_NAME\`, etc. for further details.
${WHI}"""
else
echo "PM2 is not running any workers for this deployment environment"
fi
}
d_start() {
if [[ $PM2_PROCESSES_LENGTH -gt 0 ]]; then
echo "PM2 is already running PIDs "$(echo $PM2_PROCESSES)"; cancelling start!"
else
echo "--------------"
echo "PM2 is not running; beginning "$(echo $LIVE_WORKER_INSTANCES)" workers now!"
pm2 \
-i $LIVE_WORKER_INSTANCES \
-n $PROC_NAME \
start src/tasks/worker.py
echo "--------------"
fi
}
d_stop() {
if [ $PM2_PROCESSES_LENGTH -gt 0 ]; then
echo "Stopping pm2 workers ..."
pm2 delete $PROC_NAME
echo " done!"
else
d_status
fi
}
d_restart() {
d_stop
sleep 2
calc_processes
d_start
}
case $1 in
status)
d_status
;;
start)
d_start
;;
stop)
d_stop
;;
restart)
d_restart
;;
*)
echo "usage: $NAME {status|start|stop|restart}"
exit 1
;;
esac
exit 0