-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathh_cleaner.py
64 lines (50 loc) · 1.87 KB
/
h_cleaner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import threading
from datetime import datetime, timedelta
from time import perf_counter, sleep
import psutil
from h_debug import Loggers
LOGGER_MEMORY = Loggers.memory
MAX_SURVIVE_LOGS = timedelta(minutes=30)
def add_log_entry_memory(msg):
_m = psutil.virtual_memory()
available = _m.available / 1024 / 1024 / 1024
percent = _m.percent
LOGGER_MEMORY.info(f"{available:>5.2f} | {percent:>4.1f} | {msg}")
class MemoryCleaner(threading.Thread):
def __init__(self, OPENED_LOGS):
super().__init__(daemon=True)
self.OPENED_LOGS = OPENED_LOGS
def cleaner(self):
add_log_entry_memory(f"MemoryCleaner started | Openned {len(self.OPENED_LOGS)}")
pc1 = perf_counter()
now = datetime.now()
reports = list(self.OPENED_LOGS)
for report_id in reports:
report = self.OPENED_LOGS[report_id]
if now - report.last_access > MAX_SURVIVE_LOGS:
del self.OPENED_LOGS[report_id]
add_log_entry_memory(f"NUKED OLD | {report_id}")
try:
reports = sorted(self.OPENED_LOGS, key=lambda x: self.OPENED_LOGS[x].last_access)
except Exception:
reports = list(self.OPENED_LOGS)
LOGGER_MEMORY.exception("sorted")
for report_id in reports:
if psutil.virtual_memory().percent < 75:
break
del self.OPENED_LOGS[report_id]
add_log_entry_memory(f'{(perf_counter() - pc1)*1000:>10,.3f}ms | MemoryCleaner done')
def start(self):
if self.is_alive():
return
try:
return super().start()
except Exception:
LOGGER_MEMORY.exception("start")
def run(self):
while True:
sleep(10)
try:
self.cleaner()
except Exception:
LOGGER_MEMORY.exception("main")