store.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """Durable local state for the Teams pipeline plugin."""
  2. from __future__ import annotations
  3. import hashlib
  4. import json
  5. import os
  6. import threading
  7. from copy import deepcopy
  8. from datetime import datetime, timezone, UTC
  9. from functools import partialmethod
  10. from pathlib import Path
  11. from tempfile import NamedTemporaryFile
  12. from typing import Any, Dict, Optional
  13. from hermes_constants import get_hermes_home
  14. DEFAULT_TEAMS_PIPELINE_STORE_FILENAME = "teams_pipeline_store.json"
  15. # Persisted top-level buckets; the on-disk JSON shape must stay stable for existing user stores.
  16. _BUCKETS = ("subscriptions", "notification_receipts", "event_timestamps", "jobs", "sink_records")
  17. def _utc_now_iso() -> str:
  18. return datetime.now(UTC).isoformat()
  19. def resolve_teams_pipeline_store_path(path: str | Path | None = None) -> Path:
  20. explicit = str(path).strip() if path is not None else ""
  21. env_path = os.getenv("MSGRAPH_WEBHOOK_STORE_PATH", "").strip()
  22. return Path(explicit or env_path) if (explicit or env_path) else get_hermes_home() / DEFAULT_TEAMS_PIPELINE_STORE_FILENAME
  23. class TeamsPipelineStore:
  24. """JSON-backed durable store for Teams pipeline state; every write is an atomic temp-file replace."""
  25. def __init__(self, path: str | Path):
  26. self.path = Path(path)
  27. self._lock = threading.RLock()
  28. self._state: dict[str, dict[str, Any]] = {bucket: {} for bucket in _BUCKETS}
  29. self._load()
  30. def _load(self) -> None:
  31. with self._lock:
  32. if not self.path.exists():
  33. return
  34. data = json.loads(self.path.read_text(encoding="utf-8-sig") or "{}")
  35. if not isinstance(data, dict):
  36. return
  37. self._state = {bucket: dict(data.get(bucket) or {}) for bucket in _BUCKETS}
  38. def _persist(self) -> None:
  39. self.path.parent.mkdir(parents=True, exist_ok=True)
  40. with NamedTemporaryFile("w", encoding="utf-8", dir=str(self.path.parent), delete=False) as tmp:
  41. json.dump(self._state, tmp, indent=2, sort_keys=True)
  42. tmp.flush()
  43. tmp_path = Path(tmp.name)
  44. tmp_path.replace(self.path)
  45. def _list(self, bucket: str) -> dict[str, dict[str, Any]]:
  46. with self._lock:
  47. return deepcopy(self._state[bucket])
  48. def _get(self, bucket: str, key: str) -> Optional[dict[str, Any]]:
  49. with self._lock:
  50. record = self._state[bucket].get(key)
  51. return deepcopy(record) if isinstance(record, dict) else None
  52. def _upsert(self, bucket: str, id_field: str, key: str, payload: dict[str, Any]) -> dict[str, Any]:
  53. """Merge ``payload`` over the existing record, stamping ``id_field`` and created/updated timestamps."""
  54. with self._lock:
  55. existing = self._state[bucket].get(key, {})
  56. merged = {**existing, **deepcopy(payload)}
  57. merged[id_field] = key
  58. merged.setdefault("created_at", existing.get("created_at") or _utc_now_iso())
  59. merged["updated_at"] = _utc_now_iso()
  60. self._state[bucket][key] = merged
  61. self._persist()
  62. return deepcopy(merged)
  63. list_subscriptions = partialmethod(_list, "subscriptions")
  64. get_subscription = partialmethod(_get, "subscriptions")
  65. upsert_subscription = partialmethod(_upsert, "subscriptions", "subscription_id")
  66. list_jobs = partialmethod(_list, "jobs")
  67. get_job = partialmethod(_get, "jobs")
  68. upsert_job = partialmethod(_upsert, "jobs", "job_id")
  69. get_sink_record = partialmethod(_get, "sink_records")
  70. upsert_sink_record = partialmethod(_upsert, "sink_records", "sink_key")
  71. def delete_subscription(self, subscription_id: str) -> bool:
  72. with self._lock:
  73. if self._state["subscriptions"].pop(subscription_id, None) is None:
  74. return False
  75. self._persist()
  76. return True
  77. @classmethod
  78. def build_notification_receipt_key(cls, notification: dict[str, Any]) -> str:
  79. if explicit_id := notification.get("id"):
  80. return f"id:{explicit_id}"
  81. canonical = json.dumps(notification, sort_keys=True, separators=(",", ":"))
  82. return f"sha256:{hashlib.sha256(canonical.encode('utf-8')).hexdigest()}"
  83. def record_notification_receipt(self, receipt_key: str, payload: Optional[dict[str, Any]] = None, *, received_at: Optional[str] = None) -> bool:
  84. """Record a receipt once; returns False when the key was already seen (duplicate delivery)."""
  85. with self._lock:
  86. if receipt_key in self._state["notification_receipts"]:
  87. return False
  88. self._state["notification_receipts"][receipt_key] = {"received_at": received_at or _utc_now_iso(),
  89. "payload": deepcopy(payload) if isinstance(payload, dict) else payload}
  90. self._persist()
  91. return True
  92. def stats(self) -> dict[str, int]:
  93. with self._lock:
  94. return {bucket: len(self._state[bucket]) for bucket in _BUCKETS}