From ba552e24cd2f76a7d0c112a5494e0d5c0725d45a Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Fri, 23 Jan 2026 23:41:47 +0000 Subject: [PATCH] Debug JSONL updates. --- paskia/db/background.py | 21 ++++++++++++++++++++- paskia/db/operations.py | 13 +++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/paskia/db/background.py b/paskia/db/background.py index 33cfd75..c7fa36d 100644 --- a/paskia/db/background.py +++ b/paskia/db/background.py @@ -96,12 +96,31 @@ async def start_background(): """Start the background flush/cleanup task.""" import sys global _background_task + + # Check if task exists but is no longer running (e.g., after uvicorn reload) + if _background_task is not None: + if _background_task.done(): + print(f"[DB] Previous background task was done, restarting", file=sys.stderr) + _background_task = None + else: + # Task exists and is running - but might be in a dead event loop + try: + # Check if task is in current event loop + loop = asyncio.get_running_loop() + task_loop = _background_task.get_loop() + if loop is not task_loop: + print(f"[DB] Background task in different event loop, restarting", file=sys.stderr) + _background_task = None + except Exception as e: + print(f"[DB] Error checking background task loop: {e}, restarting", file=sys.stderr) + _background_task = None + if _background_task is None: _background_task = asyncio.create_task(_background_loop()) _logger.info("Database background task started") print("[DB] Database background task started", file=sys.stderr) else: - print(f"[DB] Background task already exists: {_background_task}", file=sys.stderr) + print(f"[DB] Background task already running: {_background_task}", file=sys.stderr) async def stop_background(): diff --git a/paskia/db/operations.py b/paskia/db/operations.py index 645dc3b..4e3f636 100644 --- a/paskia/db/operations.py +++ b/paskia/db/operations.py @@ -107,7 +107,9 @@ class DB: len(self._pending_changes), ) import sys + import json print(f"[DB] Queued change by {self._current_actor}, {len(self._pending_changes)} pending", file=sys.stderr) + print(f"[DB] diff: {json.dumps(diff, default=str)}", file=sys.stderr) @contextmanager def transaction(self, actor: str = "system"): @@ -171,8 +173,10 @@ def build_role(uuid: UUID) -> Role: def build_org(uuid: UUID, include_roles: bool = False) -> Org: + import sys o = _db._data.orgs[uuid] perm_scopes = [p.scope for p in _db._data.permissions.values() if uuid in p.orgs] + print(f"[DB] build_org({uuid}): permissions={perm_scopes}", file=sys.stderr) org = Org(uuid=uuid, display_name=o.display_name, permissions=perm_scopes) if include_roles: org.roles = [ @@ -525,6 +529,8 @@ def rename_permission( Also updates all role references to use the new scope. """ + import sys + # Find permission by old scope key = None for pid, p in _db._data.permissions.items(): @@ -539,6 +545,10 @@ def rename_permission( if p.scope == new_scope and pid != key: raise ValueError(f"Permission with scope '{new_scope}' already exists") + # Debug: Print orgs before change + print(f"[DB] rename_permission: {old_scope} -> {new_scope}", file=sys.stderr) + print(f"[DB] orgs BEFORE: {_db._data.permissions[key].orgs}", file=sys.stderr) + with _db.transaction(actor): # Update the permission _db._data.permissions[key].scope = new_scope @@ -552,6 +562,9 @@ def rename_permission( del r.permissions[old_scope] r.permissions[new_scope] = True + # Debug: Print orgs after change + print(f"[DB] orgs AFTER: {_db._data.permissions[key].orgs}", file=sys.stderr) + def delete_permission(uuid: str | UUID, actor: str = "system") -> None: """Delete a permission."""