@@ -1408,28 +1408,16 @@ impl CorpusStorage {
14081408 db. apply_update ( update, |_| { } ) ?;
14091409 }
14101410 // start background thread to persists the results
1411-
14121411 let active_background_workers = self . active_background_workers . clone ( ) ;
14131412 {
14141413 let ( lock, _cvar) = & * active_background_workers;
14151414 let mut nr_active_background_workers = lock. lock ( ) ?;
14161415 * nr_active_background_workers += 1 ;
14171416 }
14181417 thread:: spawn ( move || {
1419- trace ! ( "Starting background thread to sync WAL updates" ) ;
1420- let lock = db_entry. read ( ) . unwrap ( ) ;
1421- if let Ok ( db) = get_read_or_error ( & lock) {
1422- let db: & AnnotationGraph = db;
1423- if let Err ( e) = db. background_sync_wal_updates ( ) {
1424- error ! ( "Can't sync changes in background thread: {:?}" , e) ;
1425- } else {
1426- trace ! ( "Finished background thread to sync WAL updates" ) ;
1427- }
1418+ if let Err ( err) = sync_wal_updates_in_background ( db_entry, active_background_workers) {
1419+ error ! ( "Error in WAL update background thread: {}" , err) ;
14281420 }
1429- let ( lock, cvar) = & * active_background_workers;
1430- let mut nr_active_background_workers = lock. lock ( ) . unwrap ( ) ;
1431- * nr_active_background_workers -= 1 ;
1432- cvar. notify_all ( ) ;
14331421 } ) ;
14341422
14351423 Ok ( ( ) )
@@ -2739,3 +2727,24 @@ fn create_lockfile_for_directory(db_dir: &Path) -> Result<File> {
27392727
27402728 Ok ( lock_file)
27412729}
2730+
2731+ fn sync_wal_updates_in_background (
2732+ db_entry : Arc < RwLock < CacheEntry > > ,
2733+ active_background_workers : Arc < ( Mutex < usize > , Condvar ) > ,
2734+ ) -> Result < ( ) > {
2735+ trace ! ( "Starting background thread to sync WAL updates" ) ;
2736+ let lock = db_entry. read ( ) ?;
2737+ if let Ok ( db) = get_read_or_error ( & lock) {
2738+ let db: & AnnotationGraph = db;
2739+ if let Err ( e) = db. background_sync_wal_updates ( ) {
2740+ error ! ( "Can't sync changes in background thread: {:?}" , e) ;
2741+ } else {
2742+ trace ! ( "Finished background thread to sync WAL updates" ) ;
2743+ }
2744+ }
2745+ let ( lock, cvar) = & * active_background_workers;
2746+ let mut nr_active_background_workers = lock. lock ( ) ?;
2747+ * nr_active_background_workers -= 1 ;
2748+ cvar. notify_all ( ) ;
2749+ Ok ( ( ) )
2750+ }
0 commit comments