mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
refactor: factor out wait_for_all_work_done()
This commit is contained in:
@@ -553,23 +553,7 @@ impl Context {
|
|||||||
|
|
||||||
if self.scheduler.is_running().await {
|
if self.scheduler.is_running().await {
|
||||||
self.scheduler.maybe_network().await;
|
self.scheduler.maybe_network().await;
|
||||||
|
self.wait_for_all_work_done().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;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Pause the scheduler to ensure another connection does not start
|
// Pause the scheduler to ensure another connection does not start
|
||||||
// while we are fetching on a dedicated connection.
|
// while we are fetching on a dedicated connection.
|
||||||
|
|||||||
@@ -535,7 +535,7 @@ impl Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if all background work is done.
|
/// 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 lock = self.scheduler.inner.read().await;
|
||||||
let stores: Vec<_> = match *lock {
|
let stores: Vec<_> = match *lock {
|
||||||
InnerSchedulerState::Started(ref sched) => sched
|
InnerSchedulerState::Started(ref sched) => sched
|
||||||
@@ -555,4 +555,23 @@ impl Context {
|
|||||||
}
|
}
|
||||||
true
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user