Skip to content

Commit 2bc6107

Browse files
Fix ingest system: Refresh database session after book imports
- Resolves issue where multiple books don't appear until container restart - Adds automatic session refresh after each successful calibredb import - Uses existing TaskReconnectDatabase infrastructure for safe refresh - Maintains fault tolerance - continues processing if refresh fails - Books now appear immediately in CWA UI without restart Fixes database session isolation between external calibredb adds and CWA's SQLAlchemy session cache.
1 parent ef870e9 commit 2bc6107

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

CONTRIBUTORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CONTRIBUTORS
22

33
This file is automatically generated. DO NOT EDIT MANUALLY.
4-
Generated on: 2025-09-18T08:04:48.149897Z
4+
Generated on: 2025-09-18T08:27:22.533394Z
55

66
Upstream project: https://github.com/janeczku/calibre-web
77
Fork project (Calibre-Web Automated, since 2024): https://github.com/crocodilestick/calibre-web-automated

scripts/ingest_processor.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ def add_book_to_library(self, book_path:str, text: bool=True, format: str="text"
438438
# Trigger auto-send for users who have it enabled
439439
self.trigger_auto_send_if_enabled(staged_path.stem, book_path)
440440

441+
# CRITICAL FIX: Refresh Calibre-Web's database session to make new books visible
442+
# This solves the issue where multiple books don't appear until container restart
443+
self.refresh_calibre_web_session()
444+
441445
# If we overwrote an existing book, Calibre does not bump books.timestamp, only last_modified.
442446
# Update timestamp to last_modified for any rows changed by this import so sorting by 'new' reflects overwrites.
443447
if self.cwa_settings.get('auto_ingest_automerge') == 'overwrite':
@@ -611,6 +615,38 @@ def trigger_auto_send_if_enabled(self, book_title: str, book_path: str) -> None:
611615
print(f"[ingest-processor] Error in auto-send trigger: {e}", flush=True)
612616

613617

618+
def refresh_calibre_web_session(self) -> None:
619+
"""Refresh Calibre-Web's database session to make newly added books visible
620+
621+
This solves the issue where external calibredb adds aren't immediately visible
622+
in Calibre-Web until container restart.
623+
"""
624+
if not _CPS_AVAILABLE:
625+
print("[ingest-processor] CPS modules not available, skipping session refresh", flush=True)
626+
return
627+
628+
try:
629+
# Import here to avoid circular imports and ensure CPS is available
630+
from cps.tasks.database import TaskReconnectDatabase
631+
632+
# Create and run the reconnect task
633+
task = TaskReconnectDatabase()
634+
print("[ingest-processor] Refreshing Calibre-Web database session...", flush=True)
635+
636+
# Run the task directly - this forces a database session refresh
637+
# which makes newly imported books immediately visible in the UI
638+
task.run(None) # worker_thread not needed for direct execution
639+
640+
print("[ingest-processor] Database session refreshed successfully", flush=True)
641+
642+
except ImportError as e:
643+
print(f"[ingest-processor] Could not import TaskReconnectDatabase: {e}", flush=True)
644+
except Exception as e:
645+
print(f"[ingest-processor] Error refreshing database session: {e}", flush=True)
646+
# Don't fail the import if session refresh fails
647+
print("[ingest-processor] Continuing despite session refresh failure - books may require manual refresh", flush=True)
648+
649+
614650
def set_library_permissions(self):
615651
try:
616652
nsm = os.getenv("NETWORK_SHARE_MODE", "false").strip().lower() in ("1", "true", "yes", "on")

0 commit comments

Comments
 (0)