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