Skip to content

Commit 7dde5cf

Browse files
committed
Improve performance
1 parent 6bb0f4d commit 7dde5cf

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

root/app/swag-ondemand.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,71 @@
1111

1212
last_accessed_urls = set()
1313
last_accessed_urls_lock = threading.Lock()
14-
ondemand_containers = {}
15-
ondemand_containers_lock = threading.Lock()
1614

1715
class ContainerThread(threading.Thread):
1816
def __init__(self):
1917
super().__init__()
2018
self.daemon = True
19+
self.ondemand_containers = {}
2120
try:
2221
self.docker_client = docker.from_env()
2322
except Exception as e:
2423
logging.exception(e)
2524

2625
def process_containers(self):
27-
global ondemand_containers
2826
containers = self.docker_client.containers.list(all=True, filters={ "label": ["swag_ondemand=enable"] })
2927
container_names = {container.name for container in containers}
3028

31-
for container_name in list(ondemand_containers.keys()):
29+
for container_name in list(self.ondemand_containers.keys()):
3230
if container_name in container_names:
3331
continue
34-
ondemand_containers.pop(container_name)
32+
self.ondemand_containers.pop(container_name)
3533
logging.info(f"Stopped monitoring {container_name}")
3634

3735
for container in containers:
3836
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():
4038
last_accessed = datetime.now()
4139
logging.info(f"Started monitoring {container.name}")
4240
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 }
4543

4644
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":
5147
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()
5349
if inactive_seconds < STOP_THRESHOLD:
5450
continue
5551
self.docker_client.containers.get(container_name).stop()
5652
logging.info(f"Stopped {container_name} after {STOP_THRESHOLD}s of inactivity")
5753

5854
def start_containers(self):
59-
global ondemand_containers
6055
with last_accessed_urls_lock:
6156
last_accessed_urls_combined = ",".join(last_accessed_urls)
6257
last_accessed_urls.clear()
6358

64-
for container_name in ondemand_containers.keys():
59+
for container_name in self.ondemand_containers.keys():
6560
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(","):
6762
if ondemand_url not in last_accessed_urls_combined:
6863
continue
69-
ondemand_containers[container_name]["last_accessed"] = datetime.now()
64+
self.ondemand_containers[container_name]["last_accessed"] = datetime.now()
7065
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":
7267
continue
7368
self.docker_client.containers.get(container_name).start()
7469
logging.info(f"Started {container_name}")
75-
ondemand_containers[container_name]["status"] = "running"
70+
self.ondemand_containers[container_name]["status"] = "running"
7671

7772
def run(self):
7873
while True:
7974
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)
8579
except Exception as e:
8680
logging.exception(e)
8781

0 commit comments

Comments
 (0)