forked from LockerProject/Locker
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocker
executable file
·130 lines (112 loc) · 3.13 KB
/
locker
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
#!/bin/sh
show_help() {
cat <<EOF
Usage: locker [options]
Options:
--production Run in production mode (default: development mode)
EOF
}
locker_mode=development
# Parse command line options
while true; do
case "$1" in
-h|--help|-\?) show_help; exit 0;;
--production) locker_mode=production; shift ;;
--development) shift ;;
--) shift; break;;
-*) echo "invalid option: $1" 1>&2; show_help; exit 1;;
*) break;;
esac
done
# If we were installed via lockerbox, find our dependencies there
lockerbox_env=../lockerbox_environment.sh
if [ -f "$lockerbox_env" ]; then
. "$lockerbox_env"
fi
# Make sure we have some API keys, otherwise we can't do much
apikeys=Config/apikeys.json
if [ ! -f "$apikeys" ]; then
echo "No API keys found in $apikeys" >&2
echo "see https://github.com/LockerProject/Locker/wiki/GettingAPIKeys" >&2
exit 1
fi
cleanup_mongo() {
mongodata=Me/mongodata
mongolock=$mongodata/mongod.lock
if [ -s $mongolock ]; then
echo "mongod lock file exists: $mongolock..."
pid=$(cat "$mongolock")
if [ "$locker_mode" = "production" ]; then
# Shut it down
echo "Shutting down mongod."
kill "$pid"
while kill -0 "$pid" >/dev/null 2>&1; do
echo "Waiting for mongod to exit..."
sleep 1
done
echo "mongod shut down."
else
# Automatically check and repair mongo
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "...and it's still running. Hopefully that's OK."
else
echo "...but it's dead. Cleaning up..."
rm -f "$mongolock"
if ! mongod --dbpath "$mongodata" --repair; then
echo "mongod --repair failed!" >&2
exit 1
fi
fi
fi
fi
}
monitor() {
# Monitor the process and restart it if it dies
trap shutdown INT TERM
while true; do
start_time=$(date +%s)
# start the command
$* &
lockerd_pid=$!
if wait $lockerd_pid; then
# Clean shutdown
exit 0
fi
# Abnormal exit
cleanup_mongo
end_time=$(date +%s)
runtime=$(($end_time - $start_time))
if [ "$runtime" -lt 10 ]; then
echo "locker was only running for $runtime seconds, not respawning" >&2
exit 1
fi
wait=5
echo "Locker exited unexpectedly, respawning in $wait seconds..." >&2
sleep $wait
done
}
shutdown() {
kill -TERM $lockerd_pid
wait $lockerd_pid
status=$?
echo "locker exited with status $status"
cleanup_mongo
exit $status
}
cleanup_mongo
# Extra paths to search for files : separated
export NODE_PATH=Common/node
# Start the locker
case "$locker_mode" in
production)
# Tell node to run in production mode
NODE_ENV=production
export NODE_ENV
monitor node lockerd.js "$@"
;;
development|*)
# Just run it
exec node lockerd.js "$@"
;;
esac
exit 1