-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart
More file actions
executable file
·73 lines (60 loc) · 2.19 KB
/
Copy pathstart
File metadata and controls
executable file
·73 lines (60 loc) · 2.19 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
#!/usr/bin/env bash
# re-exec with an identifiable argv[0] ( `start:<dirname>` ) so derived projects
# are distinguishable in `ps` output.
if [ -z "$SB_START_NAMED" ]; then
SB_START_NAMED=1 exec -a "start:$(basename "$PWD")" bash "$0" "$@"
fi
# --noloop: run server once without auto-restart loop,
# for service managers ( e.g., systemd with Restart=always ) that restart on their own.
# --force: start even if another server already answers on .localctl.sock.
LOOP=1
FORCE=0
CFG=""
for a in "$@"; do
case "$a" in
--noloop|-n) LOOP=0 ;;
--force|-F) FORCE=1 ;;
*) CFG="$a" ;;
esac
done
# refuse to start a second server for the same project. with several terminals /
# agents on one machine this is easy to do by accident, and the loser is whoever
# gets EADDRINUSE. checked before the traps below are installed, so that bailing
# out here never touches the running server's .server.pid.
if [ "$FORCE" = 0 ] && ./tool/base/ping -q; then
echo "a server is already running in this project:"
./tool/base/ping
echo
echo "run 'npm run log' to watch it, 'npm run stop' to stop it,"
echo "or './start --force' to start anyway."
exit 1
fi
trap "rm -f .server.pid; kill 0" exit # killing all process on current group on exit
trap "echo server loop stopped; exit" SIGINT SIGTERM # on ctrl-c, exit script.
echo $$ > .server.pid # for `npm stop`; killing this pid takes down the whole group.
# pino-pretty params
# -i : ignore
# -c : color
# -t : format timestamp
# see infrastructure.md -> daemon
while [ 1 ]; do
if [[ $NODE_ENV == "production" ]]; then
# production - use prebuilt js files
CMD=(node ./.backend/engine/index.js)
else
# dev - use livescript files
CMD=(./node_modules/.bin/lsc ./backend/engine/index)
fi
ARGS=(--home "$PWD") # identity hint; also sets process.title in engine
if [ -n "$CFG" ]; then ARGS+=(-c "$CFG"); fi
"${CMD[@]}" "${ARGS[@]}" | \
tee -a server.log | \
./node_modules/.bin/pino-pretty \
-c -o "[{module}] {msg} {responseTime}" \
-i req,res,hostname,pid,module,responseTime \
-t "yy/mm/dd HH:MM:sso"
if [ "$LOOP" = "0" ]; then break; fi
echo "server down; try to auto restart after 1s."
sleep 1;
done
wait