devservices_healthcheck.py 939 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. "127.0.0.1:2181",
  28. "--list",
  29. ]
  30. healthchecks = [postgres_healthcheck]
  31. if os.getenv("NEED_KAFKA") == "true":
  32. healthchecks.append(kafka_healthcheck)
  33. for check in healthchecks:
  34. run_cmd(check)
  35. if __name__ == "__main__":
  36. main()