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:
|
||||
|
||||
Reference in New Issue
Block a user