-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsd_status.py
More file actions
56 lines (42 loc) · 1.53 KB
/
Copy pathsd_status.py
File metadata and controls
56 lines (42 loc) · 1.53 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
"""Shared warmup status, event log, and logger."""
from __future__ import annotations
import logging
import threading
import time
WARMUP_EVENT_LIMIT = 1000
WARMUP_STATUS: dict = {
"state": "idle",
"started_at": None,
"finished_at": None,
"completed": 0,
"total": 0,
"current": None,
"current_started_at": None,
"error": None,
"event_seq": 0,
"events": [],
}
WARMUP_LOCK = threading.Lock()
WARMUP_CANCEL_EVENT = threading.Event()
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
LOGGER = logging.getLogger("simudyne_duck")
def now_iso() -> str:
return time.strftime("%Y-%m-%dT%H:%M:%S")
def record_event(message: str, level: str = "info", **fields) -> None:
event = {"time": now_iso(), "level": level, "message": message}
if fields:
event["fields"] = fields
with WARMUP_LOCK:
WARMUP_STATUS["event_seq"] += 1
event["seq"] = WARMUP_STATUS["event_seq"]
WARMUP_STATUS["events"].append(event)
if len(WARMUP_STATUS["events"]) > WARMUP_EVENT_LIMIT:
WARMUP_STATUS["events"] = WARMUP_STATUS["events"][-WARMUP_EVENT_LIMIT:]
if level == "error":
LOGGER.error("%s | %s", message, fields if fields else "{}")
elif level == "warning":
LOGGER.warning("%s | %s", message, fields if fields else "{}")
else:
LOGGER.info("%s | %s", message, fields if fields else "{}")
class WarmupCancelled(Exception):
"""Raised to unwind the warmup pipeline when cancellation is requested."""