@@ -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