From dbe4149b63103cce473c48164a5afa1aec39a265 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Fri, 23 Jan 2026 23:35:00 +0000 Subject: [PATCH] Debug JSONL updates. --- paskia/db/background.py | 14 ++++++++++++++ paskia/db/jsonl.py | 4 +++- paskia/db/operations.py | 8 +++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/paskia/db/background.py b/paskia/db/background.py index 7b07dbd..33cfd75 100644 --- a/paskia/db/background.py +++ b/paskia/db/background.py @@ -47,15 +47,24 @@ def cleanup() -> None: async def flush() -> None: """Write all pending database changes to disk.""" + import sys from paskia.db.operations import _db if _db is None: + _logger.warning("flush() called but _db is None") + print("[DB] flush() called but _db is None", file=sys.stderr) return + pending_count = len(_db._pending_changes) + if pending_count > 0: + print(f"[DB] flush() called with {pending_count} pending changes, db_path={_db.db_path}", file=sys.stderr) await flush_changes(_db.db_path, _db._pending_changes) async def _background_loop(): """Background task that periodically flushes changes and cleans up.""" + import sys + print("[DB] Background loop starting", file=sys.stderr) + _logger.info("Background loop starting") # Run cleanup immediately on startup to clear old expired items cleanup() await flush() @@ -75,6 +84,7 @@ async def _background_loop(): await flush() # Flush cleanup changes last_cleanup = now except asyncio.CancelledError: + _logger.info("Background loop cancelled, final flush") # Final flush before exit await flush() break @@ -84,10 +94,14 @@ async def _background_loop(): async def start_background(): """Start the background flush/cleanup task.""" + import sys global _background_task 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) async def stop_background(): diff --git a/paskia/db/jsonl.py b/paskia/db/jsonl.py index 1b61d8f..8f785a0 100644 --- a/paskia/db/jsonl.py +++ b/paskia/db/jsonl.py @@ -119,9 +119,11 @@ async def flush_changes( # Append all lines in a single write (binary mode for Windows compatibility) async with aiofiles.open(db_path, "ab") as f: await f.write(b"\n".join(lines) + b"\n") - _logger.debug( + _logger.info( "Flushed %d change(s) to %s", len(changes_to_write), db_path ) + import sys + print(f"[DB] Flushed {len(changes_to_write)} change(s) to {db_path}", file=sys.stderr) return True except OSError: _logger.exception("Failed to flush database changes") diff --git a/paskia/db/operations.py b/paskia/db/operations.py index cae5a99..645dc3b 100644 --- a/paskia/db/operations.py +++ b/paskia/db/operations.py @@ -19,8 +19,6 @@ from uuid import UUID import msgspec -_logger = logging.getLogger(__name__) - from paskia.db.jsonl import ( DB_PATH_DEFAULT, _ChangeRecord, @@ -48,6 +46,8 @@ from paskia.db.structs import ( ) from paskia.util.passphrase import is_well_formed as _is_passphrase +_logger = logging.getLogger(__name__) + # msgspec encoder/decoder _json_encoder = msgspec.json.Encoder() _json_decoder = msgspec.json.Decoder(_DatabaseData) @@ -101,11 +101,13 @@ class DB: create_change_record(self._current_actor, diff) ) self._previous_builtins = current - _logger.debug( + _logger.info( "Queued change by %s, %d pending", self._current_actor, len(self._pending_changes), ) + import sys + print(f"[DB] Queued change by {self._current_actor}, {len(self._pending_changes)} pending", file=sys.stderr) @contextmanager def transaction(self, actor: str = "system"):