travis_keepalive.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env bash
  2. # travis_keepalive runs the given command and preserves its return value,
  3. # while it forks a child process what periodically produces a log line,
  4. # so that Travis won't abort the build after 10 minutes.
  5. # Why?
  6. # `t.Log()` in Go holds the buffer until the test does not pass or fail,
  7. # and `-race` can increase the execution time by 2-20x.
  8. set -euo pipefail
  9. readonly KEEPALIVE_INTERVAL=300 # seconds => 5m
  10. main() {
  11. keepalive
  12. $@
  13. }
  14. # Keepalive produces a log line in each KEEPALIVE_INTERVAL.
  15. keepalive() {
  16. local child_pid
  17. # Note: We fork here!
  18. repeat "keepalive" &
  19. child_pid=$!
  20. ensureChildOnEXIT "${child_pid}"
  21. }
  22. repeat() {
  23. local this="$1"
  24. while true; do
  25. echo "${this}"
  26. sleep "${KEEPALIVE_INTERVAL}"
  27. done
  28. }
  29. # Ensures that the child gets killed on normal program exit.
  30. ensureChildOnEXIT() {
  31. # Note: SIGINT and SIGTERM are forwarded to the child process by Bash
  32. # automatically, so we don't have to deal with signals.
  33. local child_pid="$1"
  34. trap "kill ${child_pid}" EXIT
  35. }
  36. main "$@"