|
1 | 1 | import argparse |
2 | 2 | import socket |
| 3 | +import urllib.parse |
| 4 | + |
| 5 | +import requests |
3 | 6 |
|
4 | 7 |
|
5 | 8 | def get_args(argv: list[str]) -> argparse.Namespace: |
6 | 9 | parser = argparse.ArgumentParser( |
7 | | - description="Check if the API is able to accept local TCP connections.", |
| 10 | + description="Health checks", |
| 11 | + ) |
| 12 | + subcommands = parser.add_subparsers(dest="subcommand", required=True) |
| 13 | + tcp_parser = subcommands.add_parser( |
| 14 | + "tcp", help="Check if the API is able to accept local TCP connections" |
8 | 15 | ) |
9 | | - parser.add_argument( |
| 16 | + tcp_parser.add_argument( |
10 | 17 | "--port", |
11 | 18 | "-p", |
12 | 19 | type=int, |
13 | 20 | default=8000, |
14 | 21 | help="Port to check the API on (default: 8000)", |
15 | 22 | ) |
16 | | - parser.add_argument( |
| 23 | + tcp_parser.add_argument( |
17 | 24 | "--timeout", |
18 | 25 | "-t", |
19 | 26 | type=int, |
20 | 27 | default=1, |
21 | 28 | help="Socket timeout for the connection attempt in seconds (default: 1)", |
22 | 29 | ) |
| 30 | + http_parser = subcommands.add_parser( |
| 31 | + "http", help="Check if the API is able to serve HTTP requests" |
| 32 | + ) |
| 33 | + http_parser.add_argument( |
| 34 | + "--port", |
| 35 | + "-p", |
| 36 | + type=int, |
| 37 | + default=8000, |
| 38 | + help="Port to check the API on (default: 8000)", |
| 39 | + ) |
| 40 | + http_parser.add_argument( |
| 41 | + "--timeout", |
| 42 | + "-t", |
| 43 | + type=int, |
| 44 | + default=1, |
| 45 | + help="Request timeout in seconds (default: 1)", |
| 46 | + ) |
| 47 | + http_parser.add_argument( |
| 48 | + "path", |
| 49 | + nargs="?", |
| 50 | + type=str, |
| 51 | + default="/health/liveness", |
| 52 | + help="Request path (default: /health/liveness)", |
| 53 | + ) |
23 | 54 | return parser.parse_args(argv) |
24 | 55 |
|
25 | 56 |
|
26 | | -def main(argv: list[str]) -> None: |
27 | | - args = get_args(argv) |
| 57 | +def check_tcp_connection( |
| 58 | + port: int, |
| 59 | + timeout_seconds: int, |
| 60 | +) -> None: |
28 | 61 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
29 | | - sock.settimeout(args.timeout) |
| 62 | + sock.settimeout(timeout_seconds) |
30 | 63 | try: |
31 | | - sock.connect(("127.0.0.1", args.port)) |
| 64 | + sock.connect(("127.0.0.1", port)) |
32 | 65 | except socket.error as e: |
33 | | - print(f"Failed: {e} {args.port=}") |
| 66 | + print(f"Failed: {e} {port=}") |
34 | 67 | exit(1) |
35 | 68 | else: |
36 | 69 | exit(0) |
37 | 70 | finally: |
38 | 71 | sock.close() |
| 72 | + |
| 73 | + |
| 74 | +def check_http_response( |
| 75 | + port: int, |
| 76 | + timeout_seconds: int, |
| 77 | + path: str, |
| 78 | +) -> None: |
| 79 | + url = urllib.parse.urljoin(f"http://127.0.0.1:{port}", path) |
| 80 | + requests.get( |
| 81 | + url, |
| 82 | + timeout=timeout_seconds, |
| 83 | + ).raise_for_status() |
| 84 | + |
| 85 | + |
| 86 | +def main(argv: list[str]) -> None: |
| 87 | + args = get_args(argv) |
| 88 | + match args.subcommand: |
| 89 | + case "tcp": |
| 90 | + check_tcp_connection( |
| 91 | + port=args.port, |
| 92 | + timeout_seconds=args.timeout, |
| 93 | + ) |
| 94 | + case "http": |
| 95 | + check_http_response( |
| 96 | + port=args.port, |
| 97 | + timeout_seconds=args.timeout, |
| 98 | + path=args.path, |
| 99 | + ) |
0 commit comments