|
11 | 11 |
|
12 | 12 | last_accessed_urls = set() |
13 | 13 | last_accessed_urls_lock = threading.Lock() |
14 | | -ondemand_containers = {} |
15 | | -ondemand_containers_lock = threading.Lock() |
16 | 14 |
|
17 | 15 | class ContainerThread(threading.Thread): |
18 | 16 | def __init__(self): |
19 | 17 | super().__init__() |
20 | 18 | self.daemon = True |
| 19 | + self.ondemand_containers = {} |
21 | 20 | try: |
22 | 21 | self.docker_client = docker.from_env() |
23 | 22 | except Exception as e: |
24 | 23 | logging.exception(e) |
25 | 24 |
|
26 | 25 | def process_containers(self): |
27 | | - global ondemand_containers |
28 | 26 | containers = self.docker_client.containers.list(all=True, filters={ "label": ["swag_ondemand=enable"] }) |
29 | 27 | container_names = {container.name for container in containers} |
30 | 28 |
|
31 | | - for container_name in list(ondemand_containers.keys()): |
| 29 | + for container_name in list(self.ondemand_containers.keys()): |
32 | 30 | if container_name in container_names: |
33 | 31 | continue |
34 | | - ondemand_containers.pop(container_name) |
| 32 | + self.ondemand_containers.pop(container_name) |
35 | 33 | logging.info(f"Stopped monitoring {container_name}") |
36 | 34 |
|
37 | 35 | for container in containers: |
38 | 36 | container_urls = container.labels.get("swag_ondemand_urls", f"https://{container.name}.,http://{container.name}.") |
39 | | - if container.name not in ondemand_containers.keys(): |
| 37 | + if container.name not in self.ondemand_containers.keys(): |
40 | 38 | last_accessed = datetime.now() |
41 | 39 | logging.info(f"Started monitoring {container.name}") |
42 | 40 | else: |
43 | | - last_accessed = ondemand_containers[container.name]["last_accessed"] |
44 | | - ondemand_containers[container.name] = { "status": container.status, "urls": container_urls, "last_accessed": last_accessed } |
| 41 | + last_accessed = self.ondemand_containers[container.name]["last_accessed"] |
| 42 | + self.ondemand_containers[container.name] = { "status": container.status, "urls": container_urls, "last_accessed": last_accessed } |
45 | 43 |
|
46 | 44 | def stop_containers(self): |
47 | | - global ondemand_containers |
48 | | - |
49 | | - for container_name in ondemand_containers.keys(): |
50 | | - if ondemand_containers[container_name]["status"] != "running": |
| 45 | + for container_name in self.ondemand_containers.keys(): |
| 46 | + if self.ondemand_containers[container_name]["status"] != "running": |
51 | 47 | continue |
52 | | - inactive_seconds = (datetime.now() - ondemand_containers[container_name]["last_accessed"]).total_seconds() |
| 48 | + inactive_seconds = (datetime.now() - self.ondemand_containers[container_name]["last_accessed"]).total_seconds() |
53 | 49 | if inactive_seconds < STOP_THRESHOLD: |
54 | 50 | continue |
55 | 51 | self.docker_client.containers.get(container_name).stop() |
56 | 52 | logging.info(f"Stopped {container_name} after {STOP_THRESHOLD}s of inactivity") |
57 | 53 |
|
58 | 54 | def start_containers(self): |
59 | | - global ondemand_containers |
60 | 55 | with last_accessed_urls_lock: |
61 | 56 | last_accessed_urls_combined = ",".join(last_accessed_urls) |
62 | 57 | last_accessed_urls.clear() |
63 | 58 |
|
64 | | - for container_name in ondemand_containers.keys(): |
| 59 | + for container_name in self.ondemand_containers.keys(): |
65 | 60 | accessed = False |
66 | | - for ondemand_url in ondemand_containers[container_name]["urls"].split(","): |
| 61 | + for ondemand_url in self.ondemand_containers[container_name]["urls"].split(","): |
67 | 62 | if ondemand_url not in last_accessed_urls_combined: |
68 | 63 | continue |
69 | | - ondemand_containers[container_name]["last_accessed"] = datetime.now() |
| 64 | + self.ondemand_containers[container_name]["last_accessed"] = datetime.now() |
70 | 65 | accessed = True |
71 | | - if not accessed or ondemand_containers[container_name]["status"] == "running": |
| 66 | + if not accessed or self.ondemand_containers[container_name]["status"] == "running": |
72 | 67 | continue |
73 | 68 | self.docker_client.containers.get(container_name).start() |
74 | 69 | logging.info(f"Started {container_name}") |
75 | | - ondemand_containers[container_name]["status"] = "running" |
| 70 | + self.ondemand_containers[container_name]["status"] = "running" |
76 | 71 |
|
77 | 72 | def run(self): |
78 | 73 | while True: |
79 | 74 | try: |
80 | | - with ondemand_containers_lock: |
81 | | - self.process_containers() |
82 | | - self.start_containers() |
83 | | - self.stop_containers() |
84 | | - time.sleep(2) |
| 75 | + self.process_containers() |
| 76 | + self.start_containers() |
| 77 | + self.stop_containers() |
| 78 | + time.sleep(5) |
85 | 79 | except Exception as e: |
86 | 80 | logging.exception(e) |
87 | 81 |
|
|
0 commit comments