feat(p1-2): increment/decrement_background_tasks persistieren in Plugin-State-Datei
This commit is contained in:
@@ -212,6 +212,10 @@ class PresenceEngine:
|
||||
self._background_tasks: int = 0
|
||||
self._background_tasks_lock = threading.Lock()
|
||||
|
||||
# P1-2: Plugin-State-Datei für DetectionTimer (Gateway-interne Subagenten)
|
||||
self._plugin_state_path: str = _get_plugin_state_path()
|
||||
self._plugin_state_lock = threading.Lock() # Thread-safe, nicht asyncio
|
||||
|
||||
# P1a: Honeymoon-Timer — busy status minimum 1s sichtbar
|
||||
self._busy_started_at: float = 0.0 # time.monotonic() when busy started
|
||||
self._busy_honeymoon_timer: Optional[threading.Timer] = None
|
||||
@@ -453,6 +457,8 @@ class PresenceEngine:
|
||||
def increment_background_tasks(self) -> int:
|
||||
"""Increment background tasks counter. Thread-safe.
|
||||
|
||||
P1-2: Persists to plugin state file for DetectionTimer.
|
||||
|
||||
Returns:
|
||||
New counter value.
|
||||
"""
|
||||
@@ -460,11 +466,14 @@ class PresenceEngine:
|
||||
self._background_tasks += 1
|
||||
count = self._background_tasks
|
||||
self._pulse_controller.set_background_count(count)
|
||||
self._persist_plugin_state(count)
|
||||
return count
|
||||
|
||||
def decrement_background_tasks(self) -> int:
|
||||
"""Decrement background tasks counter. Thread-safe.
|
||||
|
||||
P1-2: Persists to plugin state file for DetectionTimer.
|
||||
|
||||
Returns:
|
||||
New counter value.
|
||||
"""
|
||||
@@ -475,8 +484,27 @@ class PresenceEngine:
|
||||
self._pulse_controller.set_background_count(count)
|
||||
if count == 0:
|
||||
self._pulse_controller.stop()
|
||||
self._persist_plugin_state(count)
|
||||
return count
|
||||
|
||||
def _persist_plugin_state(self, background_tasks: int) -> None:
|
||||
"""P1-2: Persist background_tasks to plugin state file.
|
||||
|
||||
Thread-safe via _plugin_state_lock. Writes atomically via
|
||||
_write_plugin_state (temp + rename).
|
||||
|
||||
Args:
|
||||
background_tasks: Current background tasks count.
|
||||
"""
|
||||
with self._plugin_state_lock:
|
||||
data = {
|
||||
"background_tasks": background_tasks,
|
||||
"last_update_iso": datetime.now(timezone.utc).isoformat(),
|
||||
"source": "plugin_internal",
|
||||
"schema_version": PLUGIN_STATE_SCHEMA_VERSION,
|
||||
}
|
||||
_write_plugin_state(self._plugin_state_path, data)
|
||||
|
||||
# ── Internal: Presence Setting ───────────────────────────────────────
|
||||
|
||||
def _set_presence(self, status: str, message: str) -> None:
|
||||
|
||||
@@ -1833,3 +1833,79 @@ class TestPluginStateFile:
|
||||
assert result["background_tasks"] == 5
|
||||
# Keine .tmp-Datei liegt rum
|
||||
assert not list(tmp_path.glob("*.tmp"))
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# P1-2: Plugin-State Persistenz in increment/decrement_background_tasks
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestPluginStatePersistence:
|
||||
"""P1-2: increment/decrement_background_tasks persistieren in Plugin-State-Datei."""
|
||||
|
||||
def test_increment_writes_plugin_state(self, tmp_path):
|
||||
"""increment_background_tasks() schreibt background_tasks in Plugin-State-Datei."""
|
||||
from plugin.archivar.presence_engine import PresenceEngine, _read_plugin_state
|
||||
|
||||
state_path = str(tmp_path / "hermes_rocket_plugin.json")
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
|
||||
mock_rest = MagicMock()
|
||||
mock_rest.auth_token = "token"
|
||||
mock_rest.user_id = "user123"
|
||||
|
||||
engine = PresenceEngine(rest=mock_rest, enabled=True)
|
||||
# Override path for test
|
||||
engine._plugin_state_path = state_path
|
||||
|
||||
count = engine.increment_background_tasks()
|
||||
assert count == 1
|
||||
|
||||
# Read back from file
|
||||
state = _read_plugin_state(state_path)
|
||||
assert state["background_tasks"] == 1
|
||||
assert state["source"] == "plugin_internal"
|
||||
assert "last_update_iso" in state
|
||||
|
||||
def test_decrement_writes_plugin_state(self, tmp_path):
|
||||
"""decrement_background_tasks() schreibt reduzierten Wert in Plugin-State-Datei."""
|
||||
from plugin.archivar.presence_engine import PresenceEngine, _read_plugin_state
|
||||
|
||||
state_path = str(tmp_path / "hermes_rocket_plugin.json")
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
|
||||
mock_rest = MagicMock()
|
||||
mock_rest.auth_token = "token"
|
||||
mock_rest.user_id = "user123"
|
||||
|
||||
engine = PresenceEngine(rest=mock_rest, enabled=True)
|
||||
engine._plugin_state_path = state_path
|
||||
|
||||
engine.increment_background_tasks()
|
||||
engine.increment_background_tasks()
|
||||
count = engine.decrement_background_tasks()
|
||||
assert count == 1
|
||||
|
||||
state = _read_plugin_state(state_path)
|
||||
assert state["background_tasks"] == 1
|
||||
|
||||
def test_decrement_to_zero_writes_zero(self, tmp_path):
|
||||
"""decrement auf 0 schreibt background_tasks=0 in Datei."""
|
||||
from plugin.archivar.presence_engine import PresenceEngine, _read_plugin_state
|
||||
|
||||
state_path = str(tmp_path / "hermes_rocket_plugin.json")
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
|
||||
mock_rest = MagicMock()
|
||||
mock_rest.auth_token = "token"
|
||||
mock_rest.user_id = "user123"
|
||||
|
||||
engine = PresenceEngine(rest=mock_rest, enabled=True)
|
||||
engine._plugin_state_path = state_path
|
||||
|
||||
engine.increment_background_tasks()
|
||||
count = engine.decrement_background_tasks()
|
||||
assert count == 0
|
||||
|
||||
state = _read_plugin_state(state_path)
|
||||
assert state["background_tasks"] == 0
|
||||
|
||||
Reference in New Issue
Block a user