From 1995e2de0181cf6dcb2ba889d2bcb2e2ed42586d Mon Sep 17 00:00:00 2001 From: Colin Copeland Date: Tue, 29 Oct 2024 17:10:31 -0400 Subject: [PATCH] time group too --- nc/prime_cache.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/nc/prime_cache.py b/nc/prime_cache.py index 89ea4fe9..d668e0bb 100755 --- a/nc/prime_cache.py +++ b/nc/prime_cache.py @@ -53,7 +53,9 @@ def __enter__(self): def __exit__(self, type, value, traceback): self.elapsed = time.perf_counter() - self.start - self.exceeded_threshold = self.elapsed > self.threshold_seconds + self.exceeded_threshold = ( + self.elapsed > self.threshold_seconds if self.threshold_seconds else False + ) def get_agencies_and_officers(by_officer: bool = False, limit_to_agencies: list = None) -> list: @@ -119,19 +121,22 @@ def prime_group_cache(agency_id: int, num_stops: int, officer_id: int = None): f"Priming cache ({agency_id=}, {officer_id=}, {num_stops=}, {bool(session.auth)=})..." ) urls = get_group_urls(agency_id=agency_id, officer_id=officer_id) - for url in urls: - with Timer(threshold_seconds=CLOUDFRONT_RESPONSE_TIMEOUT - 1) as timer: - response = session.get(url) - logger.debug( - f"Queried {url=} ({response.headers=}, {response.request.headers=}, {timer.elapsed=})" - ) - if timer.exceeded_threshold: - logger.warning(f"Slow response possibly not cached: {url} ({timer.elapsed})") - raise Exception(f"Slow prime cache response possibly not cached {url}") - if response.status_code != 200: - logger.warning(f"Status not OK: {url} ({response.status_code})") - raise Exception(f"Request to {url} failed: {response.status_code}") - logger.info(f"Primed cache ({agency_id=}, {officer_id=}, {num_stops=})") + with Timer() as group_timer: + for url in urls: + with Timer(threshold_seconds=CLOUDFRONT_RESPONSE_TIMEOUT - 1) as endpoint_timer: + response = session.get(url) + logger.debug( + f"Queried {url=} ({response.headers=}, {response.request.headers=}, {endpoint_timer.elapsed=})" + ) + if endpoint_timer.exceeded_threshold: + logger.warning( + f"Slow response possibly not cached: {url} ({endpoint_timer.elapsed})" + ) + raise Exception(f"Slow prime cache response possibly not cached {url}") + if response.status_code != 200: + logger.warning(f"Status not OK: {url} ({response.status_code})") + raise Exception(f"Request to {url} failed: {response.status_code}") + logger.info(f"Primed cache ({agency_id=}, {officer_id=}, {num_stops=}, {group_timer.elapsed=})") def invalidate_cloudfront_cache(sleep_seconds: int = 30) -> dict: