start-buck-daemon
1.98 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
#!/bin/bash
# -----------------------------------------------------------------------------
# Launches Buck daemon if not already running and requests Buck task execution.
# -----------------------------------------------------------------------------
BUCK_DAEMON=$1
TASK=${2:-unspecified}
DATA=${3}
# TODO: Figure out how to parametrize better
BUCK_PROPS="-Dcheckstyle.config=$4 -Dcheckstyle.suppressions=$5"
PORT_FILE="$1.port"
function ppid() {
ps -p ${1:-$$} -o ppid= -o pid= -o comm=
}
function buck_pid() {
BUCK_PID=($(ppid))
while [ ${BUCK_PID[0]} -ne 0 ]; do
BUCK_PID=($(ppid $BUCK_PID))
if [ "${BUCK_PID[2]}" == "buck" ]; then
# use parent PID of buck
echo ${BUCK_PID[0]}
return
fi
if [ "${BUCK_PID[2]}" == "buckd" ] ||
[[ "${BUCK_PID[2]}" == *"python"* ]]; then
# use PID of buckd or python
echo ${BUCK_PID[1]}
return
fi
done
# fallback last read PID
echo ${BUCK_PID[1]}
}
function port() {
cat $PORT_FILE 2>/dev/null || echo 0
}
function check_socket() {
nc localhost $(port) < /dev/null 2>/dev/null
return $?
}
# check to see if buck daemon is running; if not, start it
if ! check_socket; then
# Starting buck daemon...
#FIXME change to /dev/null if/when we are confident
nohup java $BUCK_PROPS -jar $BUCK_DAEMON $PORT_FILE $(buck_pid) >>/tmp/buck.daemon 2>&1 &
TRIES=20
i=0
# Wait for buck daemon to start for 2 seconds
while [ $i -lt $TRIES ]; do
if check_socket; then
CONNECTED=true
break
fi
let i=i+1
sleep 0.1
done
if [ -z "$CONNECTED" ]; then
echo "Failed to start buck daemon"
exit 3
fi
fi
# run the actual buck daemon client
OUT=$((printf "%s\n" $TASK; cat $DATA) | nc localhost $(port))
if [ $? -ne 0 ]; then
echo "Error connecting to buck daemon server"
exit 2
fi
if [ -n "$OUT" ]; then
printf "$OUT"
exit 1
fi