From a0a4b3c8167a8914fb9b6780ec309bbb3d1183bb Mon Sep 17 00:00:00 2001 From: Prashant Vasudevan <71649489+vprashrex@users.noreply.github.com> Date: Sat, 23 May 2026 09:20:25 +0530 Subject: [PATCH 1/2] fix(cron): invoke pending-jobs endpoint alongside evaluations --- scripts/python/invoke-cron.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/scripts/python/invoke-cron.py b/scripts/python/invoke-cron.py index 4bf1c9a7e..35667820e 100644 --- a/scripts/python/invoke-cron.py +++ b/scripts/python/invoke-cron.py @@ -15,7 +15,10 @@ from dotenv import load_dotenv # Configuration -ENDPOINT = "/api/v1/cron/evaluations" # Endpoint to invoke +ENDPOINTS = [ + "/api/v1/cron/evaluations", + "/api/v1/cron/pending-jobs", +] REQUEST_TIMEOUT = 30 # Timeout for requests in seconds # Setup logging @@ -33,7 +36,7 @@ def __init__(self): # Load BASE_URL from environment with default fallback base_url = os.getenv("API_BASE_URL", "http://localhost:8000") self.base_url = base_url.rstrip("/") - self.endpoint = ENDPOINT + self.endpoints = ENDPOINTS # Load interval from environment with default of 5 minutes self.interval_minutes = int(os.getenv("CRON_INTERVAL_MINUTES", "5")) @@ -83,25 +86,23 @@ async def authenticate(self, client: httpx.AsyncClient) -> str: logger.error(f"Authentication error: {e}") raise - async def invoke_endpoint(self, client: httpx.AsyncClient) -> dict: - """Invoke the configured endpoint.""" + async def invoke_endpoint(self, client: httpx.AsyncClient, endpoint: str) -> dict: + """Invoke a single endpoint.""" if not self.access_token: await self.authenticate(client) headers = {"Authorization": f"Bearer {self.access_token}"} - # Debug: Log what we're sending - logger.debug(f"Request URL: {self.base_url}{self.endpoint}") + logger.debug(f"Request URL: {self.base_url}{endpoint}") logger.debug(f"Request headers: {headers}") try: response = await client.get( - f"{self.base_url}{self.endpoint}", + f"{self.base_url}{endpoint}", headers=headers, timeout=REQUEST_TIMEOUT, ) - # Debug: Log response headers and first part of body logger.debug(f"Response status: {response.status_code}") logger.debug(f"Response headers: {dict(response.headers)}") @@ -111,7 +112,7 @@ async def invoke_endpoint(self, client: httpx.AsyncClient) -> dict: await self.authenticate(client) headers = {"Authorization": f"Bearer {self.access_token}"} response = await client.get( - f"{self.base_url}{self.endpoint}", + f"{self.base_url}{endpoint}", headers=headers, timeout=REQUEST_TIMEOUT, ) @@ -132,7 +133,7 @@ async def run(self): """Main loop to invoke endpoint periodically.""" logger.info(f"Using API Base URL: {self.base_url}") logger.info( - f"Starting cron job - invoking {self.endpoint} every {self.interval_minutes} minutes" + f"Starting cron job - invoking {self.endpoints} every {self.interval_minutes} minutes" ) # Use async context manager to ensure proper cleanup @@ -145,8 +146,9 @@ async def run(self): start_time = datetime.now() logger.info(f"Invoking endpoint at {start_time}") - result = await self.invoke_endpoint(client) - logger.info(f"Endpoint invoked successfully: {result}") + for endpoint in self.endpoints: + result = await self.invoke_endpoint(client, endpoint) + logger.info(f"[{endpoint}] invoked successfully: {result}") # Calculate next invocation time elapsed = (datetime.now() - start_time).total_seconds() From 30285dbe4d7b79f41fe11479fe1a55fe7c4877c8 Mon Sep 17 00:00:00 2001 From: Prashant Vasudevan <71649489+vprashrex@users.noreply.github.com> Date: Sat, 23 May 2026 09:39:53 +0530 Subject: [PATCH 2/2] fix(cron): enhance logging for endpoint invocations in invoke-cron script --- scripts/python/invoke-cron.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/scripts/python/invoke-cron.py b/scripts/python/invoke-cron.py index 35667820e..963bac7ef 100644 --- a/scripts/python/invoke-cron.py +++ b/scripts/python/invoke-cron.py @@ -93,8 +93,7 @@ async def invoke_endpoint(self, client: httpx.AsyncClient, endpoint: str) -> dic headers = {"Authorization": f"Bearer {self.access_token}"} - logger.debug(f"Request URL: {self.base_url}{endpoint}") - logger.debug(f"Request headers: {headers}") + logger.debug(f"[invoke_endpoint] Request URL: {self.base_url}{endpoint}") try: response = await client.get( @@ -103,12 +102,13 @@ async def invoke_endpoint(self, client: httpx.AsyncClient, endpoint: str) -> dic timeout=REQUEST_TIMEOUT, ) - logger.debug(f"Response status: {response.status_code}") - logger.debug(f"Response headers: {dict(response.headers)}") + logger.debug(f"[invoke_endpoint] Response status: {response.status_code}") # If unauthorized or forbidden (token expired/invalid), re-authenticate and retry once if response.status_code in (401, 403): - logger.info("Token expired or invalid, re-authenticating...") + logger.info( + "[invoke_endpoint] Token expired or invalid, re-authenticating..." + ) await self.authenticate(client) headers = {"Authorization": f"Bearer {self.access_token}"} response = await client.get( @@ -122,11 +122,11 @@ async def invoke_endpoint(self, client: httpx.AsyncClient, endpoint: str) -> dic except httpx.HTTPStatusError as e: logger.error( - f"Endpoint invocation failed with status {e.response.status_code}: {e.response.text}" + f"[invoke_endpoint] Endpoint invocation failed with status {e.response.status_code}: {e.response.text}" ) raise except Exception as e: - logger.error(f"Endpoint invocation error: {e}") + logger.error(f"[invoke_endpoint] Endpoint invocation error: {e}") raise async def run(self): @@ -147,8 +147,13 @@ async def run(self): logger.info(f"Invoking endpoint at {start_time}") for endpoint in self.endpoints: - result = await self.invoke_endpoint(client, endpoint) - logger.info(f"[{endpoint}] invoked successfully: {result}") + try: + result = await self.invoke_endpoint(client, endpoint) + logger.info(f"[{endpoint}] invoked successfully: {result}") + except Exception as endpoint_error: + logger.error( + f"[{endpoint}] invocation failed: {endpoint_error}" + ) # Calculate next invocation time elapsed = (datetime.now() - start_time).total_seconds()