Skip to content

Commit 4ba10db

Browse files
authored
Feat: do not iterate on all runs (#259)
* Feat: do not iterate on all runs * Feat: move to 50 runs
1 parent bc44ccb commit 4ba10db

1 file changed

Lines changed: 49 additions & 13 deletions

File tree

  • src/discord-cluster-manager/launchers

src/discord-cluster-manager/launchers/github.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ async def trigger(self, inputs: dict) -> bool:
165165
raise ValueError(f"Could not find workflow {self.workflow_file}") from e
166166

167167
branch_name = get_github_branch_name()
168-
logger.info(
168+
logger.debug(
169169
"Dispatching workflow %s on branch %s with inputs %s",
170170
self.workflow_file,
171171
branch_name,
@@ -174,20 +174,56 @@ async def trigger(self, inputs: dict) -> bool:
174174
success = await asyncio.to_thread(workflow.create_dispatch, branch_name, inputs=inputs)
175175

176176
if success:
177-
logger.info("Waiting for workflow to start...")
178-
await asyncio.sleep(2)
179-
180-
def get_runs_sync():
181-
paginated_list = workflow.get_runs()
182-
return list(paginated_list)
177+
wait_seconds = 5
178+
logger.info(
179+
f"Workflow dispatch successful. Waiting {wait_seconds}s for the run to appear..."
180+
)
181+
await asyncio.sleep(wait_seconds)
182+
recent_runs_paginated = await asyncio.to_thread(
183+
workflow.get_runs, event="workflow_dispatch"
184+
)
183185

184-
runs_list = await asyncio.to_thread(get_runs_sync)
186+
logger.info(
187+
f"Checking recent workflow_dispatch runs after {trigger_time.isoformat()}..."
188+
)
189+
found_run = None
190+
runs_checked = 0
191+
try:
192+
run_iterator = recent_runs_paginated.__iter__()
193+
while runs_checked < 50:
194+
try:
195+
run = next(run_iterator)
196+
runs_checked += 1
197+
logger.debug(
198+
f"Checking run {run.id} created at {run.created_at.isoformat()}"
199+
)
200+
if run.created_at.replace(tzinfo=datetime.timezone.utc) > trigger_time:
201+
found_run = run
202+
logger.info(f"Found matching workflow run: ID {found_run.id}")
203+
break
204+
else:
205+
logger.info(f"Run {run.id} is older than trigger time, stopping check.")
206+
break
207+
except StopIteration:
208+
logger.debug("Reached end of recent runs list.")
209+
break
210+
except Exception as e:
211+
logger.error(f"Error iterating through recent runs: {e}", exc_info=True)
212+
return False
185213

186-
for run in runs_list:
187-
if run.created_at.replace(tzinfo=datetime.timezone.utc) > trigger_time:
188-
self.run = run
189-
return True
190-
return False
214+
if found_run:
215+
self.run = found_run
216+
return True
217+
else:
218+
logger.warning(
219+
f"Could not find a workflow run created after {trigger_time.isoformat()}."
220+
)
221+
return False
222+
else:
223+
logger.error(
224+
f"Failed to dispatch workflow {self.workflow_file} on branch {branch_name}."
225+
)
226+
return False
191227

192228
async def wait_for_completion(
193229
self, callback: Callable[["GitHubRun"], Awaitable[None]], timeout_minutes: int = 5

0 commit comments

Comments
 (0)