Skip to content

Commit

Permalink
check for traefik entrypoint on startup
Browse files Browse the repository at this point in the history
Informative error instead of hang in case of misconfigured values
  • Loading branch information
minrk committed Mar 14, 2023
1 parent 553086f commit f6d3dd9
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions jupyterhub_traefik_proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,16 @@ async def _traefik_api_request(self, path):
self.log.debug("%s GET %s", resp.code, url)
return resp

_static_config_awaited = False

async def _wait_for_static_config(self):
async def _check_traefik_static_conf_ready():
"""Check if traefik loaded its static configuration yet"""
try:
await self._traefik_api_request("/api/overview")
await self._traefik_api_request(
f"/api/entrypoints/{self.traefik_entrypoint}"
)
except ConnectionRefusedError:
self.log.debug(
f"Connection Refused waiting for traefik at {self.traefik_api_url}. It's probably starting up..."
Expand All @@ -328,9 +333,14 @@ async def _check_traefik_static_conf_ready():
)
return False
if e.code == 404:
self.log.debug(
f"traefik api at {e.response.request.url} overview not ready yet"
)
if "/entrypoints/" in e.response.request.url:
self.log.warning(
f"c.{self.__class__.__name__}.traefik_entrypoint={self.traefik_entrypoint!r} not found in traefik. Is it correct?"
)
else:
self.log.debug(
f"traefik api at {e.response.request.url} overview not ready yet"
)
return False
# unexpected
self.log.error(f"Error checking for traefik static configuration {e}")
Expand All @@ -343,6 +353,7 @@ async def _check_traefik_static_conf_ready():
"Traefik static configuration not available",
timeout=self.check_route_timeout,
)
self._static_config_awaited = True

def _stop_traefik(self):
self.log.info("Cleaning up traefik proxy [pid=%i]...", self.traefik_process.pid)
Expand Down Expand Up @@ -461,6 +472,14 @@ async def start(self):
self._start_traefik()
await self._wait_for_static_config()

async def check_routes(self, *args, **kwargs):
# make sure we've loaded static config
# this won't have happened automatically
# if not self.should_start
if not self._static_config_awaited:
await self._wait_for_static_config()
return await super().check_routes(*args, **kwargs)

async def stop(self):
"""Stop the proxy.
Expand Down

0 comments on commit f6d3dd9

Please sign in to comment.