dev_1.0.0_yuyr_2:重新提交 PR,增加 master/agent 以及系统集成测试 (#17)
Reviewed-on: #17 Reviewed-by: sundapeng <sundp@mail.zgclab.edu.cn> Reviewed-by: xuxt <xuxt@zgclab.edu.cn>
This commit was merged in pull request #17.
This commit is contained in:
44
src/agent/app/state.py
Normal file
44
src/agent/app/state.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from .log import get_logger
|
||||
|
||||
LOGGER = get_logger("argus.agent.state")
|
||||
|
||||
|
||||
def load_node_state(path: str) -> Optional[Dict[str, Any]]:
|
||||
"""读取本地 node.json,容器重启后沿用之前的 ID。"""
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as handle:
|
||||
return json.load(handle)
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
except json.JSONDecodeError as exc:
|
||||
LOGGER.warning("node.json is invalid JSON; ignoring", extra={"error": str(exc)})
|
||||
return None
|
||||
|
||||
|
||||
def save_node_state(path: str, data: Dict[str, Any]) -> None:
|
||||
"""原子化写入 node.json,避免并发读取坏数据。"""
|
||||
directory = Path(path).parent
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.NamedTemporaryFile("w", dir=directory, delete=False, encoding="utf-8") as tmp:
|
||||
json.dump(data, tmp, separators=(",", ":"))
|
||||
tmp.flush()
|
||||
os.fsync(tmp.fileno())
|
||||
temp_path = tmp.name
|
||||
os.replace(temp_path, path)
|
||||
|
||||
|
||||
def clear_node_state(path: str) -> None:
|
||||
try:
|
||||
os.remove(path)
|
||||
except FileNotFoundError:
|
||||
return
|
||||
except OSError as exc:
|
||||
LOGGER.warning("Failed to remove node state file", extra={"error": str(exc), "path": path})
|
||||
Reference in New Issue
Block a user