Skip to content

Commit 103227a

Browse files
authored
Refactor process readiness check in PolicyEngineRunner (#800)
Removed the wait_until_process_is_up function and integrated its logic into the engine's health check process. The engine now confirms its health before executing lifecycle callbacks, improving clarity and efficiency in the startup sequence.
1 parent 62c946a commit 103227a

File tree

1 file changed

+7
-32
lines changed
  • packages/opal-client/opal_client/engine

1 file changed

+7
-32
lines changed

packages/opal-client/opal_client/engine/runner.py

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import os
33
import shutil
44
import signal
5-
import time
65
from abc import ABC, abstractmethod
76
from typing import Callable, Coroutine, List, Optional
87

98
import aiohttp
10-
import psutil
119
from opal_client.config import EngineLogFormat, opal_client_config
1210
from opal_client.engine.logger import log_engine_output_opa, log_engine_output_simple
1311
from opal_client.engine.options import CedarServerOptions, OpaServerOptions
@@ -17,26 +15,6 @@
1715
AsyncCallback = Callable[[], Coroutine]
1816

1917

20-
async def wait_until_process_is_up(
21-
process_pid: int,
22-
callback: Optional[AsyncCallback],
23-
wait_interval: float = 0.1,
24-
timeout: Optional[float] = None,
25-
):
26-
"""Waits until the pid of the process exists, then optionally runs a
27-
callback.
28-
29-
optionally receives a timeout to give up.
30-
"""
31-
start_time = time.time()
32-
while not psutil.pid_exists(process_pid):
33-
if timeout is not None and start_time - time.time() > timeout:
34-
break
35-
await asyncio.sleep(wait_interval)
36-
if callback is not None:
37-
await callback()
38-
39-
4018
class PolicyEngineRunner(ABC):
4119
"""Runs the policy engine in a supervised subprocess.
4220
@@ -210,22 +188,19 @@ async def _run_process_until_terminated(self) -> int:
210188
start_new_session=True,
211189
)
212190

213-
# waits until the process is up, then runs a callback
214-
asyncio.create_task(
215-
wait_until_process_is_up(
216-
self._process.pid, callback=self._run_start_callbacks
217-
)
218-
)
219-
220-
# After the process is up (PID exists) we also want to make sure the
191+
# After the process is up, we also want to make sure the
221192
# engine reports as healthy before we continue. We run the health
222-
# check in the background and set an event so __aenter__ can await it.
193+
# check in the background and set an event, so __aenter__ can await it.
223194
async def _set_ready_when_healthy():
224195
try:
225196
await self._wait_for_engine_health()
226-
self._engine_ready.set()
227197
except Exception as e:
228198
logger.error("Engine failed health check: {err}", err=e)
199+
else:
200+
self._engine_ready.set()
201+
# Now that the engine is confirmed healthy, run the
202+
# lifecycle callbacks (initial start or rehydration).
203+
await self._run_start_callbacks()
229204

230205
asyncio.create_task(_set_ready_when_healthy())
231206

0 commit comments

Comments
 (0)