|
23 | 23 | MAX_TRIES = 180 # 30 minutes max |
24 | 24 |
|
25 | 25 |
|
| 26 | +def _get_main_app_httpx_client_host() -> tuple[httpx.Client, str]: |
| 27 | + """Get an HTTPX client to connect to the main app, depending on the deployment type. |
| 28 | +
|
| 29 | + The second return value is the base URL to use for the main app. |
| 30 | + Returns |
| 31 | + ------- |
| 32 | + httpx.Client: The HTTPX client. |
| 33 | + str: The base URL to use for the main app. |
| 34 | + """ |
| 35 | + _headers = {} |
| 36 | + sign_request(_headers) |
| 37 | + if os.getenv('HP_SHARED_KEY'): |
| 38 | + transport = httpx.HTTPTransport(uds=os.getenv('HP_EXAPP_SOCK', '/tmp/exapp.sock')) # noqa: S108 |
| 39 | + return httpx.Client(transport=transport, headers=_headers), 'main_app' |
| 40 | + |
| 41 | + connect_host = 'localhost' if os.environ['APP_HOST'] in ('0.0.0.0', '::') else os.environ['APP_HOST'] # noqa: S104 |
| 42 | + return httpx.Client(headers=_headers), f'{connect_host}:{os.environ["APP_PORT"]}' |
| 43 | + |
| 44 | + |
26 | 45 | if __name__ == '__main__': |
27 | 46 | # intial buffer |
28 | 47 | sleep(STARTUP_CHECK_SEC) |
|
47 | 66 | _max_tries = MAX_TRIES |
48 | 67 | _enabled = False |
49 | 68 | _last_err = None |
50 | | - _headers = {} |
51 | | - sign_request(_headers) |
| 69 | + client, base_url = _get_main_app_httpx_client_host() |
52 | 70 | # wait for the main process to be ready, check the /enabled endpoint |
53 | 71 | while _max_tries > 0: |
54 | | - with httpx.Client() as client: |
55 | | - try: |
56 | | - ret = client.get(f'http://{os.environ["APP_HOST"]}:{os.environ["APP_PORT"]}/enabled', headers=_headers) |
57 | | - ret.raise_for_status() |
58 | | - |
59 | | - if not ret.json().get('enabled', False): |
60 | | - raise RuntimeError('Main app is not enabled, sleeping for a while...') |
61 | | - except (httpx.RequestError, RuntimeError) as e: |
62 | | - print( |
63 | | - f'{MAX_TRIES-_max_tries+1}/{MAX_TRIES}:' |
64 | | - f' [Embedding server] Waiting for the main app to be enabled/ready: {e}', |
65 | | - flush=True, |
66 | | - ) |
67 | | - _last_err = e |
68 | | - sleep(STARTUP_CHECK_SEC) |
69 | | - _max_tries -= 1 |
70 | | - continue |
71 | | - |
72 | | - _enabled = True |
73 | | - break |
| 72 | + try: |
| 73 | + ret = client.get(f'http://{base_url}/enabled') |
| 74 | + ret.raise_for_status() |
| 75 | + |
| 76 | + if not ret.json().get('enabled', False): |
| 77 | + raise RuntimeError('Main app is not enabled, sleeping for a while...') |
| 78 | + except (httpx.RequestError, RuntimeError) as e: |
| 79 | + print( |
| 80 | + f'{MAX_TRIES-_max_tries+1}/{MAX_TRIES}:' |
| 81 | + f' [Embedding server] Waiting for the main app to be enabled/ready: {e}', |
| 82 | + flush=True, |
| 83 | + ) |
| 84 | + _last_err = e |
| 85 | + sleep(STARTUP_CHECK_SEC) |
| 86 | + _max_tries -= 1 |
| 87 | + continue |
| 88 | + |
| 89 | + _enabled = True |
| 90 | + break |
74 | 91 |
|
75 | 92 | if not _enabled: |
76 | 93 | logger.error( |
|
0 commit comments