Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible timeout support in detect_process #125

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions demo/runners/support/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import os
import random
import subprocess
from timeit import default_timer

from aiohttp import web, ClientSession, ClientRequest, ClientError
from aiohttp import web, ClientSession, ClientRequest, ClientError, ClientTimeout

from .utils import flatten, log_json, log_msg, log_timer, output_reader

Expand All @@ -18,6 +19,8 @@
DEFAULT_BIN_PATH = "../bin"
DEFAULT_PYTHON_PATH = ".."

START_TIMEOUT = float(os.getenv("START_TIMEOUT", 30.0))

RUN_MODE = os.getenv("RUNMODE")

GENESIS_URL = os.getenv("GENESIS_URL")
Expand Down Expand Up @@ -283,6 +286,7 @@ async def start_process(
None, self._process, agent_args, my_env, loop
)
if wait:
await asyncio.sleep(1.0)
await self.detect_process()

def _terminate(self):
Expand Down Expand Up @@ -356,23 +360,34 @@ async def admin_POST(self, path, data=None, text=False, params=None):

async def detect_process(self):
text = None
for i in range(10):
# wait for process to start and retrieve swagger content
await asyncio.sleep(2.0)
try:
async with self.client_session.get(
self.admin_url + "/api/docs/swagger.json"
) as resp:
if resp.status == 200:
text = await resp.text()
break
except ClientError:
text = None
continue

async def fetch_swagger(url: str, timeout: float):
text = None
start = default_timer()
async with ClientSession(timeout=ClientTimeout(total=3.0)) as session:
while default_timer() - start < timeout:
try:
async with session.get(url) as resp:
if resp.status == 200:
text = await resp.text()
break
except (ClientError, asyncio.TimeoutError):
pass
await asyncio.sleep(0.5)
return text

swagger_url = self.admin_url + "/api/docs/swagger.json"
text = await fetch_swagger(swagger_url, START_TIMEOUT)

if not text:
raise Exception(f"Timed out waiting for agent process to start")
raise Exception(
"Timed out waiting for agent process to start. "
+ f"Admin URL: {swagger_url}"
)
if "Aries Cloud Agent" not in text:
raise Exception(f"Unexpected response from agent process")
raise Exception(
f"Unexpected response from agent process. Admin URL: {swagger_url}"
)

async def fetch_timing(self):
status = await self.admin_GET("/status")
Expand Down