|
22 | 22 | ) |
23 | 23 | from agent.base import AgentException, Base |
24 | 24 | from agent.bench import Bench |
25 | | -from agent.exceptions import BenchNotExistsException |
| 25 | +from agent.exceptions import BenchNotExistsException, RegistryDownException |
26 | 26 | from agent.job import Job, Step, job, step |
27 | 27 | from agent.patch_handler import run_patches |
28 | 28 | from agent.site import Site |
29 | | -from agent.utils import get_supervisor_processes_status |
| 29 | +from agent.utils import get_supervisor_processes_status, is_registry_healthy |
30 | 30 |
|
31 | 31 |
|
32 | 32 | class Server(Base): |
@@ -55,8 +55,22 @@ def docker_login(self, registry): |
55 | 55 | password = registry["password"] |
56 | 56 | return self.execute(f"docker login -u {username} -p {password} {url}") |
57 | 57 |
|
| 58 | + def establish_connection_with_registry(self, max_retries: int, registry: dict[str, str]): |
| 59 | + """Given the attempt count try and establish connection with the registry else Raise""" |
| 60 | + for attempt in range(max_retries): |
| 61 | + try: |
| 62 | + if not is_registry_healthy(registry["url"], registry["username"], registry["password"]): |
| 63 | + raise RegistryDownException("Registry is not available") |
| 64 | + break |
| 65 | + except RegistryDownException as e: |
| 66 | + if attempt == max_retries - 1: |
| 67 | + raise Exception("Failed to pull image") from e |
| 68 | + |
| 69 | + time.sleep(60) |
| 70 | + |
58 | 71 | @step("Initialize Bench") |
59 | | - def bench_init(self, name, config): |
| 72 | + def bench_init(self, name, config, registry: dict[str, str]): |
| 73 | + self.establish_connection_with_registry(max_retries=3, registry=registry) |
60 | 74 | bench_directory = os.path.join(self.benches_directory, name) |
61 | 75 | os.mkdir(bench_directory) |
62 | 76 | directories = ["logs", "sites", "config"] |
@@ -98,7 +112,7 @@ def dump(self): |
98 | 112 | @job("New Bench", priority="low") |
99 | 113 | def new_bench(self, name, bench_config, common_site_config, registry, mounts=None): |
100 | 114 | self.docker_login(registry) |
101 | | - self.bench_init(name, bench_config) |
| 115 | + self.bench_init(name, bench_config, registry) |
102 | 116 | bench = Bench(name, self, mounts=mounts) |
103 | 117 | bench.update_config(common_site_config, bench_config) |
104 | 118 | if bench.bench_config.get("single_container"): |
|
0 commit comments