Skip to content

Commit

Permalink
Try to rewatch removed files in --attach
Browse files Browse the repository at this point in the history
Vim creates a new file inode when modifying files, which results in a 'remove' event. This commit handles this by treating 'remove' events as a trigger for reloads. After sending the reload command, the watcher now tries to watch the same path again, which watches the newly created file.
  • Loading branch information
phil-opp committed Jun 4, 2024
1 parent d89ba3b commit c7fcc48
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions binaries/cli/src/attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,40 @@ pub fn attach_dataflow(
}

// Setup dataflow file watcher if reload option is set.
let watcher_tx = tx.clone();
let _watcher = if hot_reload {
if hot_reload {
let (watcher_events_tx, watcher_events_rx) = mpsc::sync_channel(1);
let hash = node_path_lookup.clone();
let paths = hash.keys();
let notifier = move |event| {
if let Ok(NotifyEvent {
paths,
kind: EventKind::Modify(ModifyKind::Data(_data)),
..
}) = event
{
if let Ok(event) = event {
if watcher_events_tx.send(event).is_err() {
tracing::warn!("failed to forward watch event");
}
}
};

let mut watcher = RecommendedWatcher::new(
notifier,
Config::default().with_poll_interval(Duration::from_secs(1)),
)?;

for path in paths {
watcher.watch(path, RecursiveMode::Recursive)?;
}

let watcher_tx = tx.clone();
std::thread::spawn(|| {
while let Ok(event) = watcher_events_rx.recv() {
let rewatch = match event.kind {
// file was modified, but still exists
EventKind::Modify(ModifyKind::Data(_data)) => false,
// file was removed and probably replaced by a new one (e.g. vim does this on save)
EventKind::Remove(_) => true,
// ignore other event types
_ => return,
};

// send reload request for modified nodes/operators
for path in paths {
if let Some((dataflow_id, node_id, operator_id)) = node_path_lookup.get(&path) {

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Examples (macos-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Examples (ubuntu-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Clippy

the trait bound `std::path::PathBuf: std::borrow::Borrow<&std::path::PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Bench (ubuntu-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Bench (macos-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Bench (windows-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / CLI Test (windows-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / CLI Test (ubuntu-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / CLI Test (macos-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / ROS2 Bridge Examples

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Examples (windows-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied

Check failure on line 93 in binaries/cli/src/attach.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

the trait bound `PathBuf: Borrow<&PathBuf>` is not satisfied
watcher_tx
Expand All @@ -78,22 +101,18 @@ pub fn attach_dataflow(
.unwrap();
}
}
// TODO: Manage different file event
}
};

let mut watcher = RecommendedWatcher::new(
notifier,
Config::default().with_poll_interval(Duration::from_secs(1)),
)?;

for path in paths {
watcher.watch(path, RecursiveMode::Recursive)?;
}
Some(watcher)
} else {
None
};
if rewatch {
// watch paths again
for path in paths {
if let Err(err) = watcher.watch(path, RecursiveMode::Recursive) {
tracing::warn!("failed to watch `{}` again -> further modifications will be ignored", path.display());
}
}
}
}
});
}

// Setup Ctrlc Watcher to stop dataflow after ctrlc
let ctrlc_tx = tx;
Expand Down

0 comments on commit c7fcc48

Please sign in to comment.