mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-24 16:24:32 +00:00
First attempt at converting "sig" to "activity" for v3
This commit is contained in:
@@ -16,19 +16,21 @@ class FileDownloadActivityRefDataProvider(ActivityRefDataProvider):
|
||||
"""Generic activity ref data provider class for providers that fetch their data from the web by downloading a
|
||||
file."""
|
||||
|
||||
def __init__(self, sig_name, provider_config, url, poll_interval):
|
||||
def __init__(self, activity_name, provider_config, url, poll_interval):
|
||||
"""Set up the provider, note poll_interval is in *days*."""
|
||||
super().__init__(sig_name, provider_config)
|
||||
super().__init__(activity_name, provider_config)
|
||||
self._url = url
|
||||
self._poll_interval = poll_interval
|
||||
self._thread = None
|
||||
self._url_data_cache = URLDataCache(f"activity_ref_data_{sig_name}")
|
||||
self._url_data_cache = URLDataCache(f"activity_ref_data_{activity_name}")
|
||||
|
||||
def start(self):
|
||||
# Fire off the polling thread. It will poll immediately on startup, then sleep for poll_interval between
|
||||
# subsequent polls, so start() returns immediately and the application can continue starting.
|
||||
logger.info(f"Set up query of {self.sig_name} activity ref data every {self._poll_interval!s} days.")
|
||||
self._thread = Thread(target=self._run, name=f"FileDownloadActivityRefDataProvider-{self.sig_name}", daemon=True)
|
||||
logger.info(f"Set up query of {self.activity_name} activity ref data every {self._poll_interval!s} days.")
|
||||
self._thread = Thread(
|
||||
target=self._run, name=f"FileDownloadActivityRefDataProvider-{self.activity_name}", daemon=True
|
||||
)
|
||||
self._thread.start()
|
||||
|
||||
def stop(self):
|
||||
@@ -36,7 +38,9 @@ class FileDownloadActivityRefDataProvider(ActivityRefDataProvider):
|
||||
if self._thread:
|
||||
self._thread.join(timeout=12)
|
||||
if self._thread.is_alive():
|
||||
logger.warning(f"{self.sig_name} activity ref data worker thread did not exit on time and will be killed.")
|
||||
logger.warning(
|
||||
f"{self.activity_name} activity ref data worker thread did not exit on time and will be killed."
|
||||
)
|
||||
|
||||
def _run(self):
|
||||
while True:
|
||||
@@ -48,7 +52,7 @@ class FileDownloadActivityRefDataProvider(ActivityRefDataProvider):
|
||||
try:
|
||||
# Request data from API. Use the data cache (with a TTL of 1 day) here, not as the main mechanism for
|
||||
# caching, but just so continual restarts of the software during testing don't hammer the servers.
|
||||
logger.debug(f"Downloading {self.sig_name} activity ref data...")
|
||||
logger.debug(f"Downloading {self.activity_name} activity ref data...")
|
||||
http_response = self._url_data_cache.get(self._url, headers=HTTP_HEADERS)
|
||||
# Check response code was good
|
||||
if http_response.ok:
|
||||
@@ -60,20 +64,22 @@ class FileDownloadActivityRefDataProvider(ActivityRefDataProvider):
|
||||
|
||||
self.status = "OK"
|
||||
self.last_update_time = datetime.now(pytz.UTC)
|
||||
logger.debug(f"Received activity ref data for {self.sig_name}")
|
||||
logger.debug(f"Received activity ref data for {self.activity_name}")
|
||||
else:
|
||||
self.status = "Error"
|
||||
logger.warning(f"HTTP {http_response.status_code} when downloading activity ref data for {self.sig_name}.")
|
||||
logger.warning(
|
||||
f"HTTP {http_response.status_code} when downloading activity ref data for {self.activity_name}."
|
||||
)
|
||||
|
||||
except ConnectionError:
|
||||
self.status = "Error"
|
||||
logger.warning(f"Connection error when downloading activity ref data for {self.sig_name}.")
|
||||
logger.warning(f"Connection error when downloading activity ref data for {self.activity_name}.")
|
||||
except (ConnectTimeout, ReadTimeout):
|
||||
self.status = "Error"
|
||||
logger.warning(f"Timeout when downloading activity ref data for {self.sig_name}.")
|
||||
logger.warning(f"Timeout when downloading activity ref data for {self.activity_name}.")
|
||||
except Exception:
|
||||
self.status = "Error"
|
||||
logger.exception(f"Exception in HTTP Activity Ref Data Provider ({self.sig_name})")
|
||||
logger.exception(f"Exception in HTTP Activity Ref Data Provider ({self.activity_name})")
|
||||
self._stop_event.wait(timeout=1)
|
||||
|
||||
def _http_response_to_data(self, http_response):
|
||||
|
||||
Reference in New Issue
Block a user