diff --git a/OpenBench/management/commands/runserver.py b/OpenBench/management/commands/runserver.py index 4f2f003..0979e8b 100644 --- a/OpenBench/management/commands/runserver.py +++ b/OpenBench/management/commands/runserver.py @@ -1,29 +1,46 @@ import django.apps -import threading, time +import requests, sys, threading, time import OpenBench.utils from django.core.management.commands.runserver import Command as BaseRunserverCommand class ArtifactWatcher(threading.Thread): + def check_for_artifacts(self, data): + + # Success, if we had all artifacts. Otherwise consider re-running the Workflow + retval, has_all = OpenBench.utils.fetch_artifact_url(*data, return_data=True) + if has_all: return retval, True + + # Otherwise, we may have returned the Jobs + Artifacts + jobs, artifacts = retval + + # Catch expired artifacts, or a Github API bug and run the Workflow + if all(job['conclusion'] == 'success' for job in jobs): + run_id = jobs[0]['run_id'] + url = OpenBench.utils.path_join(data[0], 'actions', 'runs', str(run_id), 'rerun') + requests.post(url=url, headers=data[2]).json() + + # We did not have all artifacts needed, return the original source + return data[0], False + def update_test(self, test): - dev_has_all = test.dev.source.endswith('artifacts') - base_has_all = test.base.source.endswith('artifacts') - - dev_headers = OpenBench.utils.read_git_credentials(test.dev_engine) - base_headers = OpenBench.utils.read_git_credentials(test.base_engine) - - if dev_headers and not dev_has_all: - test.dev.source, dev_has_all = OpenBench.utils.fetch_artifact_url( - test.dev.source, test.dev_engine, dev_headers, test.dev.sha) + # Check for Artifacts for Dev, and re-run if needed + if not (dev_has_all := test.dev.source.endswith('artifacts')): + dev_headers = OpenBench.utils.read_git_credentials(test.dev_engine) + data = [test.dev.source, test.dev_engine, dev_headers, test.dev.sha] + test.dev.source, dev_has_all = self.check_for_artifacts(data) test.dev.save() - if base_headers and not base_has_all: - test.base.source, base_has_all = OpenBench.utils.fetch_artifact_url( - test.base.source, test.base_engine, base_headers, test.base.sha) + # Check for Artifacts for Base, and re-run if needed + if not (base_has_all := test.base.source.endswith('artifacts')): + base_headers = OpenBench.utils.read_git_credentials(test.base_engine) + data = [test.base.source, test.base_engine, base_headers, test.base.sha] + test.base.source, base_has_all = self.check_for_artifacts(data) test.base.save() + # If both finished, flag the test as no longer awaiting if dev_has_all and base_has_all: test.awaiting = False test.save() @@ -31,8 +48,12 @@ class ArtifactWatcher(threading.Thread): def run(self): while True: for test in OpenBench.utils.get_awaiting_tests(): - self.update_test(test) - time.sleep(10) + try: self.update_test(test) + except: + import traceback, sys + traceback.print_exc() + sys.stdout.flush() + time.sleep(15) class Command(BaseRunserverCommand): diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 64123b6..7a9d4ce 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -296,7 +296,7 @@ def collect_github_info(request, errors, field): url, has_all = fetch_artifact_url(base, engine, headers, data['sha']) return (url, branch, data['sha'], bench), has_all -def fetch_artifact_url(base, engine, headers, sha): +def fetch_artifact_url(base, engine, headers, sha, return_data=False): try: # Fetch the run id for the openbench workflow for this comment @@ -304,17 +304,26 @@ def fetch_artifact_url(base, engine, headers, sha): url += '?head_sha=%s' % (sha) run_id = requests.get(url=url, headers=headers).json()['workflow_runs'][0]['id'] - # Construct the final URL that will be used to look at artifacts + # Fetch information about individual job results + url = path_join(base, 'actions', 'runs', str(run_id), 'jobs') + jobs = requests.get(url=url, headers=headers).json()['jobs'] + + # Fetch information about individual artifact url = path_join(base, 'actions', 'runs', str(run_id), 'artifacts') artifacts = requests.get(url=url, headers=headers).json()['artifacts'] - # Verify that all of the artifacts exist and are not expired - available = [ f['name'] for f in artifacts if f['expired'] == False ] - required = OPENBENCH_CONFIG['engines'][engine]['build']['artifacts'] - has_all = all(['%s-%s' % (sha, name) in available for name in required]) + # All jobs finished, with an associated, non-expired Artifact + error = any(job['conclusion'] != 'success' for job in jobs) + error = error or any(artifact['expired'] for artifact in artifacts) + error = error or len(jobs) != len(artifacts) - # Return the URL iff we found them; otherwise base - return (url if has_all else base, has_all) + # Toss back the data if we care about it + if error and return_data: + return ((jobs, artifacts), False) + + # Only set the url if we have everything we need + assert not error + return (url, True) except Exception as error: # If anything goes wrong, retry later with the same base URL