blob: e6e9c1e08970ebb9117493abed6dcf715a387e75 [file] [log] [blame]
Thomas Vachuska275d2e82016-07-14 17:41:34 -07001#!/bin/bash
2# -----------------------------------------------------------------------------
3# Launches Buck daemon if not already running and requests Buck task execution.
4# -----------------------------------------------------------------------------
5
6BUCK_DAEMON=$1
7TASK=${2:-unspecified}
8DATA=${3}
9
10# TODO: Figure out how to parametrize better
11BUCK_PROPS="-Dcheckstyle.config=$4 -Dcheckstyle.suppressions=$5"
12
13PORT_FILE="$1.port"
14
15function ppid() {
16 ps -p ${1:-$$} -o ppid= -o pid= -o comm=
17}
18
19function buck_pid() {
20 BUCK_PID=($(ppid))
21 while [ ${BUCK_PID[0]} -ne 0 ]; do
22 BUCK_PID=($(ppid $BUCK_PID))
23 if [ "${BUCK_PID[2]}" == "buck" ]; then
24 # use parent PID of buck
25 echo ${BUCK_PID[0]}
26 return
27 fi
28 if [ "${BUCK_PID[2]}" == "buckd" ] ||
29 [[ "${BUCK_PID[2]}" == *"python"* ]]; then
30 # use PID of buckd or python
31 echo ${BUCK_PID[1]}
32 return
33 fi
34 done
35 # fallback last read PID
36 echo ${BUCK_PID[1]}
37}
38
39function port() {
40 cat $PORT_FILE 2>/dev/null || echo 0
41}
42
43function check_socket() {
44 nc localhost $(port) < /dev/null 2>/dev/null
45 return $?
46}
47
48# check to see if buck daemon is running; if not, start it
49if ! check_socket; then
50 # Starting buck daemon...
51 #FIXME change to /dev/null if/when we are confident
52 nohup java $BUCK_PROPS -jar $BUCK_DAEMON $PORT_FILE $(buck_pid) >>/tmp/buck.daemon 2>&1 &
53
54 TRIES=20
55 i=0
56 # Wait for buck daemon to start for 2 seconds
57 while [ $i -lt $TRIES ]; do
58 if check_socket; then
59 CONNECTED=true
60 break
61 fi
62 let i=i+1
63 sleep 0.1
64 done
65 if [ -z "$CONNECTED" ]; then
66 echo "Failed to start buck daemon"
67 exit 3
68 fi
69fi
70
71# run the actual buck daemon client
72OUT=$((printf "%s\n" $TASK; cat $DATA) | nc localhost $(port))
73if [ $? -ne 0 ]; then
74 echo "Error connecting to buck daemon server"
75 exit 2
76fi
77if [ -n "$OUT" ]; then
78 printf "$OUT"
79 exit 1
80fi