diff --git a/src/context.rs b/src/context.rs index 0c597fb6ee..3fa76bc0ee 100644 --- a/src/context.rs +++ b/src/context.rs @@ -553,23 +553,7 @@ impl Context { if self.scheduler.is_running().await { self.scheduler.maybe_network().await; - - // Wait until fetching is finished. - // Ideally we could wait for connectivity change events, - // but sleep loop is good enough. - - // First 100 ms sleep in chunks of 10 ms. - for _ in 0..10 { - if self.all_work_done().await { - break; - } - tokio::time::sleep(std::time::Duration::from_millis(10)).await; - } - - // If we are not finished in 100 ms, keep waking up every 100 ms. - while !self.all_work_done().await { - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - } + self.wait_for_all_work_done().await; } else { // Pause the scheduler to ensure another connection does not start // while we are fetching on a dedicated connection. diff --git a/src/scheduler/connectivity.rs b/src/scheduler/connectivity.rs index c230decfd8..37e2cafc6e 100644 --- a/src/scheduler/connectivity.rs +++ b/src/scheduler/connectivity.rs @@ -535,7 +535,7 @@ impl Context { } /// Returns true if all background work is done. - pub async fn all_work_done(&self) -> bool { + async fn all_work_done(&self) -> bool { let lock = self.scheduler.inner.read().await; let stores: Vec<_> = match *lock { InnerSchedulerState::Started(ref sched) => sched @@ -555,4 +555,23 @@ impl Context { } true } + + /// Waits until background work is finished. + pub async fn wait_for_all_work_done(&self) { + // Ideally we could wait for connectivity change events, + // but sleep loop is good enough. + + // First 100 ms sleep in chunks of 10 ms. + for _ in 0..10 { + if self.all_work_done().await { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + + // If we are not finished in 100 ms, keep waking up every 100 ms. + while !self.all_work_done().await { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + } + } }