forked from Furumi/furumi-web
Publish podcasts after the first device sync on restart
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
abd100d6e7
commit
0e985b4267
8 files changed
+328
-27
No files matched your search
@@ -800,16 +800,25 @@ pub async fn serve_peers(
|
||||
}
|
||||
}
|
||||
|
||||
/// Polls every trusted device of every account every
|
||||
/// [`DEVICE_SYNC_INTERVAL`] (one exchange per device at a time).
|
||||
///
|
||||
/// `first_round` turns `true` once the first complete listing of the
|
||||
/// devices has been polled: every exchange in flight then has answered,
|
||||
/// failed or timed out (30 s). The podcast publication reconciler waits for
|
||||
/// it (see [`crate::podcasts::discovery::run`]).
|
||||
pub async fn sync_loop(
|
||||
pool: sqlx::PgPool,
|
||||
service: Arc<MusicDhtService>,
|
||||
hub: Arc<PlayerDeviceHub>,
|
||||
transport_stats: Arc<TransportStats>,
|
||||
first_round: tokio::sync::watch::Sender<bool>,
|
||||
) {
|
||||
let mut interval = tokio::time::interval(DEVICE_SYNC_INTERVAL);
|
||||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut polls = tokio::task::JoinSet::new();
|
||||
let mut active = std::collections::HashMap::new();
|
||||
let mut first_round = FirstSyncRound::new(first_round);
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
@@ -819,10 +828,15 @@ pub async fn sync_loop(
|
||||
Ok(users) => users,
|
||||
Err(error) => { tracing::warn!("device poll listing failed: {error:#}"); continue; }
|
||||
};
|
||||
let mut listed_all = true;
|
||||
for user_id in users {
|
||||
let devices = match active_remote_devices(&pool, user_id).await {
|
||||
Ok(devices) => devices,
|
||||
Err(error) => { let _ = set_last_error(&pool, user_id, Some(&format!("{error:#}"))).await; continue; }
|
||||
Err(error) => {
|
||||
listed_all = false;
|
||||
let _ = set_last_error(&pool, user_id, Some(&format!("{error:#}"))).await;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
for device in devices {
|
||||
let key = (user_id, device.device_id.clone());
|
||||
@@ -838,12 +852,17 @@ pub async fn sync_loop(
|
||||
active.insert(handle.id(), key);
|
||||
}
|
||||
}
|
||||
if listed_all {
|
||||
// Includes exchanges still running from an earlier tick.
|
||||
first_round.started(active.keys().copied());
|
||||
}
|
||||
}
|
||||
Some(completed) = polls.join_next_with_id(), if !polls.is_empty() => {
|
||||
let (task, result) = match completed {
|
||||
Ok((task, result)) => (task, result),
|
||||
Err(error) => (error.id(), Err(anyhow::Error::from(error))),
|
||||
};
|
||||
first_round.finished(&task);
|
||||
if let Some((user_id, device_id)) = active.remove(&task)
|
||||
&& let Err(error) = result {
|
||||
tracing::debug!(device = %device_id, "web fed device sync failed: {error:#}");
|
||||
@@ -854,6 +873,50 @@ pub async fn sync_loop(
|
||||
}
|
||||
}
|
||||
|
||||
/// Tracks the first device-sync round of a federation run for
|
||||
/// [`sync_loop`]: the exchanges started by the first complete listing of the
|
||||
/// trusted devices (and those still running then). The watch turns `true`
|
||||
/// when the last of them ends, or at once when there are none, and stays
|
||||
/// `true`.
|
||||
pub(crate) struct FirstSyncRound<K> {
|
||||
pending: Option<std::collections::HashSet<K>>,
|
||||
done: tokio::sync::watch::Sender<bool>,
|
||||
}
|
||||
|
||||
impl<K: Eq + std::hash::Hash> FirstSyncRound<K> {
|
||||
pub(crate) fn new(done: tokio::sync::watch::Sender<bool>) -> Self {
|
||||
Self {
|
||||
pending: None,
|
||||
done,
|
||||
}
|
||||
}
|
||||
|
||||
/// The first complete listing started (or found running) `polls`;
|
||||
/// later listings are ignored.
|
||||
pub(crate) fn started(&mut self, polls: impl IntoIterator<Item = K>) {
|
||||
if self.pending.is_some() || *self.done.borrow() {
|
||||
return;
|
||||
}
|
||||
let pending: std::collections::HashSet<K> = polls.into_iter().collect();
|
||||
if pending.is_empty() {
|
||||
self.done.send_replace(true);
|
||||
} else {
|
||||
self.pending = Some(pending);
|
||||
}
|
||||
}
|
||||
|
||||
/// An exchange ended (answered, failed or timed out).
|
||||
pub(crate) fn finished(&mut self, poll: &K) {
|
||||
if let Some(pending) = &mut self.pending
|
||||
&& pending.remove(poll)
|
||||
&& pending.is_empty()
|
||||
{
|
||||
self.pending = None;
|
||||
self.done.send_replace(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn sync_once(
|
||||
pool: &sqlx::PgPool,
|
||||
service: Arc<MusicDhtService>,
|
||||
@@ -5081,6 +5144,8 @@ fn short_id(value: &str) -> String {
|
||||
value.chars().take(10).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod first_round_tests;
|
||||
#[cfg(test)]
|
||||
mod gating_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user