xref: /aosp_15_r20/external/libbpf/ci/managers/travis_wait.bash (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker# This was borrowed from https://github.com/travis-ci/travis-build/tree/master/lib/travis/build/bash
2*f7c14bbaSAndroid Build Coastguard Worker# to get around https://github.com/travis-ci/travis-ci/issues/9979. It should probably be removed
3*f7c14bbaSAndroid Build Coastguard Worker# as soon as Travis CI has started to provide an easy way to export the functions to bash scripts.
4*f7c14bbaSAndroid Build Coastguard Worker
5*f7c14bbaSAndroid Build Coastguard Workertravis_jigger() {
6*f7c14bbaSAndroid Build Coastguard Worker  local cmd_pid="${1}"
7*f7c14bbaSAndroid Build Coastguard Worker  shift
8*f7c14bbaSAndroid Build Coastguard Worker  local timeout="${1}"
9*f7c14bbaSAndroid Build Coastguard Worker  shift
10*f7c14bbaSAndroid Build Coastguard Worker  local count=0
11*f7c14bbaSAndroid Build Coastguard Worker
12*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\\n"
13*f7c14bbaSAndroid Build Coastguard Worker
14*f7c14bbaSAndroid Build Coastguard Worker  while [[ "${count}" -lt "${timeout}" ]]; do
15*f7c14bbaSAndroid Build Coastguard Worker    count="$((count + 1))"
16*f7c14bbaSAndroid Build Coastguard Worker    echo -ne "Still running (${count} of ${timeout}): ${*}\\r"
17*f7c14bbaSAndroid Build Coastguard Worker    sleep 60
18*f7c14bbaSAndroid Build Coastguard Worker  done
19*f7c14bbaSAndroid Build Coastguard Worker
20*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\\n${ANSI_RED}Timeout (${timeout} minutes) reached. Terminating \"${*}\"${ANSI_RESET}\\n"
21*f7c14bbaSAndroid Build Coastguard Worker  kill -9 "${cmd_pid}"
22*f7c14bbaSAndroid Build Coastguard Worker}
23*f7c14bbaSAndroid Build Coastguard Worker
24*f7c14bbaSAndroid Build Coastguard Workertravis_wait() {
25*f7c14bbaSAndroid Build Coastguard Worker  local timeout="${1}"
26*f7c14bbaSAndroid Build Coastguard Worker
27*f7c14bbaSAndroid Build Coastguard Worker  if [[ "${timeout}" =~ ^[0-9]+$ ]]; then
28*f7c14bbaSAndroid Build Coastguard Worker    shift
29*f7c14bbaSAndroid Build Coastguard Worker  else
30*f7c14bbaSAndroid Build Coastguard Worker    timeout=20
31*f7c14bbaSAndroid Build Coastguard Worker  fi
32*f7c14bbaSAndroid Build Coastguard Worker
33*f7c14bbaSAndroid Build Coastguard Worker  local cmd=("${@}")
34*f7c14bbaSAndroid Build Coastguard Worker  local log_file="travis_wait_${$}.log"
35*f7c14bbaSAndroid Build Coastguard Worker
36*f7c14bbaSAndroid Build Coastguard Worker  "${cmd[@]}" &>"${log_file}" &
37*f7c14bbaSAndroid Build Coastguard Worker  local cmd_pid="${!}"
38*f7c14bbaSAndroid Build Coastguard Worker
39*f7c14bbaSAndroid Build Coastguard Worker  travis_jigger "${!}" "${timeout}" "${cmd[@]}" &
40*f7c14bbaSAndroid Build Coastguard Worker  local jigger_pid="${!}"
41*f7c14bbaSAndroid Build Coastguard Worker  local result
42*f7c14bbaSAndroid Build Coastguard Worker
43*f7c14bbaSAndroid Build Coastguard Worker  {
44*f7c14bbaSAndroid Build Coastguard Worker    set +e
45*f7c14bbaSAndroid Build Coastguard Worker    wait "${cmd_pid}" 2>/dev/null
46*f7c14bbaSAndroid Build Coastguard Worker    result="${?}"
47*f7c14bbaSAndroid Build Coastguard Worker    ps -p"${jigger_pid}" &>/dev/null && kill "${jigger_pid}"
48*f7c14bbaSAndroid Build Coastguard Worker    set -e
49*f7c14bbaSAndroid Build Coastguard Worker  }
50*f7c14bbaSAndroid Build Coastguard Worker
51*f7c14bbaSAndroid Build Coastguard Worker  if [[ "${result}" -eq 0 ]]; then
52*f7c14bbaSAndroid Build Coastguard Worker    echo -e "\\n${ANSI_GREEN}The command ${cmd[*]} exited with ${result}.${ANSI_RESET}"
53*f7c14bbaSAndroid Build Coastguard Worker  else
54*f7c14bbaSAndroid Build Coastguard Worker    echo -e "\\n${ANSI_RED}The command ${cmd[*]} exited with ${result}.${ANSI_RESET}"
55*f7c14bbaSAndroid Build Coastguard Worker  fi
56*f7c14bbaSAndroid Build Coastguard Worker
57*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\\n${ANSI_GREEN}Log:${ANSI_RESET}\\n"
58*f7c14bbaSAndroid Build Coastguard Worker  cat "${log_file}"
59*f7c14bbaSAndroid Build Coastguard Worker
60*f7c14bbaSAndroid Build Coastguard Worker  return "${result}"
61*f7c14bbaSAndroid Build Coastguard Worker}
62