devservices_healthcheck.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import annotations
  2. import os
  3. import subprocess
  4. from time import sleep
  5. def run_cmd(
  6. args: list[str],
  7. *,
  8. retries: int = 3,
  9. timeout: int = 5,
  10. ) -> None:
  11. for retry in range(1, retries + 1):
  12. returncode = subprocess.call(args)
  13. if returncode != 0:
  14. sleep(timeout)
  15. else:
  16. return
  17. raise SystemExit(1)
  18. def main() -> None:
  19. # Available health checks
  20. postgres_healthcheck = ["docker", "exec", "sentry_postgres", "pg_isready", "-U", "postgres"]
  21. kafka_healthcheck = [
  22. "docker",
  23. "exec",
  24. "sentry_kafka",
  25. "kafka-topics",
  26. "--zookeeper",
  27. # TODO: sentry_zookeeper:2181 doesn't work in CI, but 127.0.0.1 doesn't work locally
  28. "127.0.0.1:2181",
  29. "--list",
  30. ]
  31. healthchecks = [postgres_healthcheck]
  32. if os.getenv("NEED_KAFKA") == "true":
  33. healthchecks.append(kafka_healthcheck)
  34. for check in healthchecks:
  35. run_cmd(check)
  36. if __name__ == "__main__":
  37. main()