Debug JSONL updates.

This commit is contained in:
2026-01-23 23:35:00 +00:00
parent 3d5f82c3df
commit dbe4149b63
3 changed files with 22 additions and 4 deletions
+14
View File
@@ -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():
+3 -1
View File
@@ -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")
+5 -3
View File
@@ -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"):