|
| 1 | +from datetime import datetime |
| 2 | +import docker |
| 3 | +import os |
| 4 | +import threading |
| 5 | +import time |
| 6 | + |
| 7 | +last_accessed_urls = set() |
| 8 | +last_accessed_urls_lock = threading.Lock() |
| 9 | +ondemand_containers = {} |
| 10 | +ondemand_containers_lock = threading.Lock() |
| 11 | +stop_threshold = int(os.environ.get("SWAG_ONDEMAND_STOP_THRESHOLD", "1800")) |
| 12 | + |
| 13 | +class ContainerThread(threading.Thread): |
| 14 | + def process_containers(self, docker_client): |
| 15 | + global ondemand_containers |
| 16 | + containers = docker_client.containers.list(all=True, filters={ "label": ["swag_ondemand=enable"] }) |
| 17 | + container_names = {container.name for container in containers} |
| 18 | + |
| 19 | + for container_name in list(ondemand_containers.keys()): |
| 20 | + if container_name in container_names: |
| 21 | + continue |
| 22 | + ondemand_containers.pop(container_name) |
| 23 | + print(f"{datetime.now()} - Stopped monitoring {container_name}") |
| 24 | + |
| 25 | + for container in containers: |
| 26 | + container_urls = container.labels.get("swag_ondemand_urls", f"https://{container.name}.,http://{container.name}.") |
| 27 | + if container.name not in ondemand_containers.keys(): |
| 28 | + last_accessed = datetime.now() |
| 29 | + print(f"{datetime.now()} - Started monitoring {container.name}") |
| 30 | + else: |
| 31 | + last_accessed = ondemand_containers[container.name]["last_accessed"] |
| 32 | + ondemand_containers[container.name] = { "status": container.status, "urls": container_urls, "last_accessed": last_accessed } |
| 33 | + |
| 34 | + def stop_containers(self, docker_client): |
| 35 | + global ondemand_containers |
| 36 | + |
| 37 | + for container_name in ondemand_containers.keys(): |
| 38 | + if ondemand_containers[container_name]["status"] != "running": |
| 39 | + continue |
| 40 | + inactive_seconds = (datetime.now() - ondemand_containers[container_name]["last_accessed"]).total_seconds() |
| 41 | + if inactive_seconds < stop_threshold: |
| 42 | + continue |
| 43 | + docker_client.containers.get(container_name).stop() |
| 44 | + print(f"{datetime.now()} - Stopped {container_name} after {stop_threshold}s of inactivity") |
| 45 | + |
| 46 | + def start_containers(self, docker_client): |
| 47 | + global ondemand_containers |
| 48 | + last_accessed_urls_lock.acquire() |
| 49 | + last_accessed_urls_combined = ",".join(last_accessed_urls) |
| 50 | + last_accessed_urls.clear() |
| 51 | + last_accessed_urls_lock.release() |
| 52 | + |
| 53 | + for container_name in ondemand_containers.keys(): |
| 54 | + accessed = False |
| 55 | + for ondemand_url in ondemand_containers[container_name]["urls"].split(","): |
| 56 | + if ondemand_url not in last_accessed_urls_combined: |
| 57 | + continue |
| 58 | + ondemand_containers[container_name]["last_accessed"] = datetime.now() |
| 59 | + accessed = True |
| 60 | + if not accessed or ondemand_containers[container_name]["status"] == "running": |
| 61 | + continue |
| 62 | + docker_client.containers.get(container_name).start() |
| 63 | + print(f"{datetime.now()} - Started {container_name}") |
| 64 | + ondemand_containers[container_name]["status"] = "running" |
| 65 | + |
| 66 | + def run(self): |
| 67 | + while True: |
| 68 | + docker_client = docker.from_env() |
| 69 | + with ondemand_containers_lock: |
| 70 | + self.process_containers(docker_client) |
| 71 | + self.start_containers(docker_client) |
| 72 | + self.stop_containers(docker_client) |
| 73 | + time.sleep(5) |
| 74 | + |
| 75 | +class LogReaderThread(threading.Thread): |
| 76 | + logname = "/config/log/nginx/access.log" |
| 77 | + |
| 78 | + def tail(self, f): |
| 79 | + f.seek(0,2) |
| 80 | + inode = os.fstat(f.fileno()).st_ino |
| 81 | + |
| 82 | + while True: |
| 83 | + line = f.readline() |
| 84 | + if not line: |
| 85 | + time.sleep(1) |
| 86 | + if os.stat(self.logname).st_ino != inode: |
| 87 | + f.close() |
| 88 | + f = open(self.logname, 'r') |
| 89 | + inode = os.fstat(f.fileno()).st_ino |
| 90 | + continue |
| 91 | + yield line |
| 92 | + |
| 93 | + def run(self): |
| 94 | + logfile = open(self.logname, "r") |
| 95 | + for line in self.tail(logfile): |
| 96 | + for part in line.split(): |
| 97 | + if not part.startswith("http"): |
| 98 | + continue |
| 99 | + with last_accessed_urls_lock: |
| 100 | + last_accessed_urls.add(part) |
| 101 | + print(f"{datetime.now()} - Accessed {part}") |
| 102 | + break |
| 103 | + |
| 104 | +ContainerThread().start() |
| 105 | +LogReaderThread().start() |
0 commit comments