|
| 1 | +import atexit |
| 2 | +import time |
| 3 | +from functools import cache |
| 4 | +from pathlib import Path |
| 5 | +from threading import Event, Thread |
| 6 | + |
| 7 | +import requests |
| 8 | +import yaml |
| 9 | + |
| 10 | +from daq_config_server.constants import ( |
| 11 | + WHITELIST_REFRESH_RATE_S, |
| 12 | + WHITELIST_URL, |
| 13 | +) |
| 14 | +from daq_config_server.log import LOGGER |
| 15 | + |
| 16 | + |
| 17 | +class WhitelistFetcher: |
| 18 | + """Read the whitelist from the main branch of this repo from github, and check for |
| 19 | + updates every 5 minutes. This lets the deployed server see updates to the whitelist |
| 20 | + without requiring a new release or a restart""" |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + self._initial_load() |
| 24 | + self._stop = Event() |
| 25 | + self.update_in_background_thread = Thread( |
| 26 | + target=self._periodically_update_whitelist, daemon=True |
| 27 | + ) |
| 28 | + self.update_in_background_thread.start() |
| 29 | + |
| 30 | + def _fetch_and_update(self): |
| 31 | + response = requests.get(WHITELIST_URL) |
| 32 | + response.raise_for_status() |
| 33 | + data = yaml.safe_load(response.text) |
| 34 | + self.whitelist_files = {Path(p) for p in data.get("whitelist_files")} |
| 35 | + self.whitelist_dirs = {Path(p) for p in data.get("whitelist_dirs")} |
| 36 | + |
| 37 | + def _initial_load(self): |
| 38 | + try: |
| 39 | + self._fetch_and_update() |
| 40 | + LOGGER.info("Successfully read whitelist from GitHub.") |
| 41 | + except Exception as e: |
| 42 | + LOGGER.error(f"Initial whitelist load failed: {e}") |
| 43 | + raise RuntimeError("Failed to load whitelist during initialization.") from e |
| 44 | + |
| 45 | + def _periodically_update_whitelist(self): |
| 46 | + while not self._stop.is_set(): |
| 47 | + time.sleep(WHITELIST_REFRESH_RATE_S) |
| 48 | + try: |
| 49 | + self._fetch_and_update() |
| 50 | + LOGGER.info("Whitelist updated successfully.") |
| 51 | + except Exception as e: |
| 52 | + LOGGER.error(f"Failed to update whitelist: {e}") |
| 53 | + |
| 54 | + def stop(self): |
| 55 | + self._stop.set() |
| 56 | + self.update_in_background_thread.join(timeout=1) |
| 57 | + |
| 58 | + |
| 59 | +@cache |
| 60 | +def get_whitelist() -> WhitelistFetcher: |
| 61 | + fetcher = WhitelistFetcher() |
| 62 | + atexit.register(fetcher.stop) |
| 63 | + return fetcher |
0 commit comments