|
| 1 | +import logging |
| 2 | +import time |
| 3 | +from pathlib import Path |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +from watchdog.observers import Observer |
| 8 | +from watchdog.events import FileSystemEventHandler |
| 9 | + |
| 10 | + |
| 11 | +# Paths. |
| 12 | +CONFIG_DIR = Path("/app/") |
| 13 | +CONFIG_PATH = Path("/app/config.yaml") |
| 14 | +EXAMPLE_CONFIG_PATH = Path("/app/example-config.yaml") |
| 15 | + |
| 16 | +# Debouncing: once the config has been reloaded, any queued unprocessed events should be discarded. |
| 17 | +LAST_INVOCATION_TIME = time.time() |
| 18 | + |
| 19 | + |
| 20 | +# Logger setup. |
| 21 | +logger = logging.getLogger("configwatcher") |
| 22 | +LOG_FORMAT = "%(asctime)s %(levelname)-8s %(message)s" |
| 23 | +formatter = logging.Formatter(LOG_FORMAT) |
| 24 | +logger.setLevel(logging.INFO) |
| 25 | +handler = logging.StreamHandler(sys.stdout) |
| 26 | +handler.setLevel(logging.INFO) |
| 27 | +handler.setFormatter(formatter) |
| 28 | +logger.addHandler(handler) |
| 29 | + |
| 30 | + |
| 31 | +def run_cmd(cmd, shell=False): |
| 32 | + r = subprocess.run(cmd, shell=shell, capture_output=True) |
| 33 | + is_error = r.returncode != 0 |
| 34 | + stdout = r.stdout.decode("utf-8") |
| 35 | + if is_error: |
| 36 | + logger.error(f"Error running command, returncode: {r.returncode}") |
| 37 | + logger.error("cmd:") |
| 38 | + logger.error(" ".join(cmd)) |
| 39 | + if r.stdout: |
| 40 | + logger.error("stdout:") |
| 41 | + logger.error(stdout) |
| 42 | + if r.stderr: |
| 43 | + logger.error("stderr:") |
| 44 | + logger.error(r.stderr.decode("utf-8")) |
| 45 | + raise ValueError |
| 46 | + return stdout |
| 47 | + |
| 48 | + |
| 49 | +def reload_config(): |
| 50 | + global LAST_INVOCATION_TIME |
| 51 | + LAST_INVOCATION_TIME = time.time() |
| 52 | + logger.info("Restarting OTD due to config change.") |
| 53 | + run_cmd(["supervisorctl", "-c", "/app/docker/supervisord.conf", "stop", "uwsgi"]) |
| 54 | + run_cmd( |
| 55 | + ["supervisorctl", "-c", "/app/docker/supervisord.conf", "restart", "memcached"] |
| 56 | + ) |
| 57 | + run_cmd(["supervisorctl", "-c", "/app/docker/supervisord.conf", "start", "uwsgi"]) |
| 58 | + run_cmd( |
| 59 | + ["supervisorctl", "-c", "/app/docker/supervisord.conf", "start", "warm_cache"] |
| 60 | + ) |
| 61 | + LAST_INVOCATION_TIME = time.time() |
| 62 | + logger.info("Restarted OTD due to config change.") |
| 63 | + |
| 64 | + |
| 65 | +class Handler(FileSystemEventHandler): |
| 66 | + |
| 67 | + def on_any_event(self, event): |
| 68 | + watch_paths_str = [ |
| 69 | + EXAMPLE_CONFIG_PATH.as_posix(), |
| 70 | + CONFIG_PATH.as_posix(), |
| 71 | + ] |
| 72 | + |
| 73 | + # Filter unwanted events. |
| 74 | + if event.event_type not in {"modified", "created"}: |
| 75 | + logger.debug(f"Dropping event with type {event.event_type=}") |
| 76 | + return |
| 77 | + if event.is_directory: |
| 78 | + logger.debug(f"Dropping dir event") |
| 79 | + return |
| 80 | + if event.src_path not in watch_paths_str: |
| 81 | + logger.debug(f"Dropping event with path {event.src_path=}") |
| 82 | + return |
| 83 | + if not Path(event.src_path).exists(): |
| 84 | + logger.debug(f"Dropping event for nonexistent path {event.src_path=}") |
| 85 | + return |
| 86 | + |
| 87 | + # Debouncing. |
| 88 | + mtime = Path(event.src_path).lstat().st_mtime |
| 89 | + if mtime < LAST_INVOCATION_TIME: |
| 90 | + msg = f"Dropping event for file that hasn't been modified since the last run. {event.src_path=}" |
| 91 | + logger.debug(msg) |
| 92 | + return |
| 93 | + |
| 94 | + logger.debug(f"Dispatching event on {event.src_path=}") |
| 95 | + reload_config() |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + |
| 100 | + event_handler = Handler() |
| 101 | + observer = Observer() |
| 102 | + observer.schedule(event_handler, CONFIG_DIR, recursive=False) |
| 103 | + observer.start() |
| 104 | + |
| 105 | + try: |
| 106 | + while True: |
| 107 | + time.sleep(1) |
| 108 | + finally: |
| 109 | + observer.stop() |
| 110 | + observer.join() |
0 commit comments