feat: add background fetch method

This commit is contained in:
Simon Laux
2023-12-12 00:03:47 +01:00
committed by bjoern
parent 48f2ea717e
commit 7b68098785
2 changed files with 59 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ use std::future::Future;
use std::path::{Path, PathBuf};
use anyhow::{ensure, Context as _, Result};
use futures::future::join_all;
use serde::{Deserialize, Serialize};
use tokio::fs;
use tokio::io::AsyncWriteExt;
@@ -291,6 +292,33 @@ impl Accounts {
}
}
/// Performs a background fetch for all accounts in parallel.
///
/// If you need a timeout, then use [Accounts::background_fetch_with_timeout] instead.
pub async fn background_fetch(&self) {
async fn background_fetch_and_log_error(account: Context) {
if let Err(error) = account.background_fetch().await {
warn!(account, "{error:#}");
}
}
join_all(
self.accounts
.values()
.cloned()
.map(background_fetch_and_log_error),
)
.await;
}
/// Performs a background fetch for all accounts in parallel with a timeout.
///
/// If you want no timeout, then use [Accounts::background_fetch] instead.
pub async fn background_fetch_with_timeout(&self, timeout: Duration) -> Result<()> {
tokio::time::timeout(timeout, self.background_fetch()).await?;
Ok(())
}
/// Emits a single event.
pub fn emit_event(&self, event: EventType) {
self.events.emit(Event { id: 0, typ: event })