Skip to content

Commit

Permalink
chore(daemon): lazy load package changes watcher (#9396)
Browse files Browse the repository at this point in the history
### Description

Before we'd initialize the package changes watcher when the daemon
starts up. Now we do it only when watch mode needs it.

### Testing Instructions

Tested manually:

```
cargo run -p turbo -- --cwd ../my-turborepo --skip-infer daemon restart 
```
In different terminal:
```
cargo run -p turbo -- --cwd ../my-turborepo --skip-infer daemon logs 
```

Change a file in the repository, note that no logs are emitted for this
change.

Then run

```
turbo watch dev
```

Note that package changes start showing up.
  • Loading branch information
NicholasLYang authored Nov 6, 2024
1 parent 180f547 commit 54efd40
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions crates/turborepo-lib/src/daemon/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
collections::{HashMap, HashSet},
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
Arc, Mutex, OnceLock,
},
time::{Duration, Instant},
};
Expand Down Expand Up @@ -61,10 +61,11 @@ pub enum CloseReason {
/// a general API and close this up.
#[derive(Clone)]
pub struct FileWatching {
repo_root: AbsoluteSystemPathBuf,
watcher: Arc<FileSystemWatcher>,
pub glob_watcher: Arc<GlobWatcher>,
pub package_watcher: Arc<PackageWatcher>,
pub package_changes_watcher: Arc<PackageChangesWatcher>,
pub package_changes_watcher: OnceLock<Arc<PackageChangesWatcher>>,
pub hash_watcher: Arc<HashWatcher>,
}

Expand Down Expand Up @@ -136,20 +137,28 @@ impl FileWatching {
scm,
));

let package_changes_watcher = Arc::new(PackageChangesWatcher::new(
repo_root,
recv.clone(),
hash_watcher.clone(),
));

Ok(FileWatching {
repo_root,
watcher,
glob_watcher,
package_watcher,
package_changes_watcher,
package_changes_watcher: OnceLock::new(),
hash_watcher,
})
}

pub fn get_or_init_package_changes_watcher(&self) -> Arc<PackageChangesWatcher> {
self.package_changes_watcher
.get_or_init(|| {
let recv = self.watcher.watch();
Arc::new(PackageChangesWatcher::new(
self.repo_root.clone(),
recv,
self.hash_watcher.clone(),
))
})
.clone()
}
}

/// Timeout for every RPC the server handles
Expand Down Expand Up @@ -591,7 +600,7 @@ impl proto::turbod_server::Turbod for TurboGrpcServiceInner {
) -> Result<tonic::Response<Self::PackageChangesStream>, tonic::Status> {
let mut package_changes_rx = self
.file_watching
.package_changes_watcher
.get_or_init_package_changes_watcher()
.package_changes()
.await;

Expand Down

0 comments on commit 54efd40

Please sign in to comment.