-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode_ninja_runner.sh
More file actions
executable file
·116 lines (99 loc) · 2.89 KB
/
node_ninja_runner.sh
File metadata and controls
executable file
·116 lines (99 loc) · 2.89 KB
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
#!/bin/bash
#
# !!! IMPORTANT !!!!
# MAKE SURE THESE ARE CORRECT FOR YOUR SYSTEM
#
PGREP=/usr/bin/pgrep
AWK=/usr/bin/awk
NODE=/usr/local/bin/node
PS=/bin/ps
PS_FLAGS=wux
AWK_PROG='{print $6}' # on OS X and ubuntu res mem size is $6 in "ps wux"
PAUSE_TIME=4
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldylw=${txtbld}$(tput setaf 3) # yellow
bldgrn=${txtbld}$(tput setaf 2) # green
txtrst=$(tput sgr0) # Reset
INFO=${bldgrn}INFO:${txtrst}
ERROR=${bldred}ERROR:${txtrst}
WARN=${bldylw}WARNING:${txtrst}
app_name=`basename $0`
function usage ()
{
echo "usage: $app_name max_memory n_crashes n_minutes script.js [...]"
echo " - max_memory in MB"
echo " - permit no more than 'n_crashes' per 'n_minutes'"
exit 1;
}
function check_progs
{
if [ ! -f $NODE ]; then echo "$ERROR Missing $NODE, aborting"; exit 1; fi
if [ ! -f $PGREP ]; then echo "$ERROR Missing $PGREP, aborting"; exit 1; fi
if [ ! -f $AWK ]; then echo "$ERROR Missing $AWK, aborting"; exit 1; fi
if [ ! -f $PS ]; then echo "$ERROR Missing $PS, aborting"; exit 1; fi
}
function already_running
{
echo "'node $1' already be running. Cowardly refusing to start another."
exit 1
}
check_progs
if [ $# -lt 4 ];
then
usage
fi
# bash only does integer arithmetic, so we'll mult by 100
# to avoid decimals
RESTART_WEIGHT=0
MAX_WEIGHT=$(( $3 * 6000 ))
WEIGHT_TIME_CHUNK=$(( (6000 * $3) / $2 ))
FADE_TIME_CHUNK=$(( ($MAX_WEIGHT / $2) / (600 / ($PAUSE_TIME * 10)) ))
#echo $WEIGHT_TIME_CHUNK $MAX_WEIGHT $FADE_TIME_CHUNK
# first make sure it's not running.
PID=`$PGREP -n -f "$NODE $4"`
if [ "$PID" != "" ]; then
already_running $4
fi
# now launch it and start monitoring
echo "$INFO Launching node..."
$NODE $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 &
while true
do
sleep $PAUSE_TIME
PID=`$PGREP -n -f "$NODE $4"`
NEED_RESTART=no
if [ "$PID" == "" ]; then
echo
echo "$WARN Node appears to have crashed."
NEED_RESTART=yes
else
# check memory usage
MEM_USAGE=`$PS $PS_FLAGS $PID | $AWK 'NR>1' | $AWK "$AWK_PROG"`
MEM_USAGE=$(( $MEM_USAGE / 1024 ))
if [ $MEM_USAGE -gt $1 ];
then
echo "$ERROR node has exceed permitted memory of $1 mb, restarting."
kill $PID
NEED_RESTART=yes
fi
fi
RESTART_WEIGHT=$(($RESTART_WEIGHT - $FADE_TIME_CHUNK))
if [ "$RESTART_WEIGHT" -lt "0" ];
then
RESTART_WEIGHT=0
fi
if [ "$NEED_RESTART" == "yes" ];
then
if [ "$RESTART_WEIGHT" -le "$MAX_WEIGHT" ];
then
echo "$INFO Restarting..."
$NODE $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 &
RESTART_WEIGHT=$(( $RESTART_WEIGHT + $WEIGHT_TIME_CHUNK ))
else
echo "$ERROR Too many restarts. Aborting."
exit -1
fi
fi
done