Debug JSONL updates.

This commit is contained in:
2026-01-23 23:41:47 +00:00
parent dbe4149b63
commit ba552e24cd
2 changed files with 33 additions and 1 deletions
+20 -1
View File
@@ -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():
+13
View File
@@ -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."""