Record worktree extensions every 5 minutes

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-06-29 16:58:19 +02:00
parent f9e0fec396
commit 639cd71a3b
7 changed files with 151 additions and 125 deletions

View file

@ -52,7 +52,7 @@ use std::{
atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
Arc,
},
time::Instant,
time::{Duration, Instant},
};
use thiserror::Error;
use util::{post_inc, ResultExt, TryFutureExt as _};
@ -403,13 +403,22 @@ impl Project {
});
let (online_tx, online_rx) = watch::channel_with(online);
let mut send_extension_counts = None;
let _maintain_online_status = cx.spawn_weak({
let mut online_rx = online_rx.clone();
move |this, mut cx| async move {
while online_rx.recv().await.is_some() {
while let Some(online) = online_rx.recv().await {
let this = this.upgrade(&cx)?;
if online {
send_extension_counts = Some(
this.update(&mut cx, |this, cx| this.send_extension_counts(cx)),
);
} else {
send_extension_counts.take();
}
this.update(&mut cx, |this, cx| {
if !this.is_online() {
if !online {
this.unshared(cx);
}
this.metadata_changed(false, cx)
@ -463,6 +472,40 @@ impl Project {
})
}
fn send_extension_counts(&self, cx: &mut ModelContext<Self>) -> Task<Option<()>> {
cx.spawn_weak(|this, cx| async move {
loop {
let this = this.upgrade(&cx)?;
this.read_with(&cx, |this, cx| {
if let Some(project_id) = this.remote_id() {
for worktree in this.visible_worktrees(cx) {
if let Some(worktree) = worktree.read(cx).as_local() {
let mut extensions = Vec::new();
let mut counts = Vec::new();
for (extension, count) in worktree.extension_counts() {
extensions.push(extension.to_string_lossy().to_string());
counts.push(*count as u32);
}
this.client
.send(proto::UpdateWorktreeExtensions {
project_id,
worktree_id: worktree.id().to_proto(),
extensions,
counts,
})
.log_err();
}
}
}
});
cx.background().timer(Duration::from_secs(60 * 5)).await;
}
})
}
pub async fn remote(
remote_id: u64,
client: Arc<Client>,