-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
56 lines (46 loc) · 1.72 KB
/
Copy pathmonitor.py
File metadata and controls
56 lines (46 loc) · 1.72 KB
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
from __future__ import annotations
import threading
from collections.abc import Callable
import psutil
from config import Config
from dialog import show_lockin_dialog
from leetcode import LeetCodeClient
def current_status_label(cfg: Config, lc: LeetCodeClient) -> str:
n = cfg.min_problems_per_day
solved = lc.solved_today_count()
if solved is None:
return f"?/{n} - api error"
if solved >= n:
return f"{solved}/{n} solved today"
return f"{solved}/{n} - locked"
def monitor_loop(
cfg: Config,
lc: LeetCodeClient,
stop: threading.Event,
on_fail_open_api_error: Callable[[], None] | None = None,
) -> None:
targets = {p.lower() for p in cfg.processes}
while not stop.is_set():
solved = lc.solved_today_count()
if solved is None and cfg.fail_open_on_api_error:
if on_fail_open_api_error is not None:
on_fail_open_api_error()
else:
effective = solved if solved is not None else 0
if effective < cfg.min_problems_per_day:
killed_any = False
for proc in psutil.process_iter(["name", "pid"]):
if stop.is_set():
break
name = (proc.info.get("name") or "").lower()
if name not in targets:
continue
try:
proc.kill()
killed_any = True
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
if killed_any:
show_lockin_dialog(effective, cfg.min_problems_per_day, cfg.quotes)
if stop.wait(cfg.poll_interval_seconds):
break