-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmod.rs
208 lines (181 loc) · 6.26 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
mod event;
use crate::*;
use async_trait::async_trait;
use std::path::PathBuf;
use std::sync::Arc;
use std::{sync::Mutex, time::SystemTime};
use tokio::sync::mpsc::{self, channel, Receiver};
use tokio::sync::Notify;
use tracing::{error, info, instrument, warn};
pub use event::*;
pub struct Watcher {
name: String,
state: WatcherState,
sender: mpsc::UnboundedSender<runtime::PRMessage>,
ignore: Vec<String>,
abort: Arc<Notify>,
root: PathBuf,
}
impl Watcher {
pub fn new(
name: &String,
state: &WatcherState,
sender: &mpsc::UnboundedSender<runtime::PRMessage>,
abort: &Arc<Notify>,
root: &PathBuf,
ignore: &Vec<String>,
) -> Self {
Self {
name: name.clone(),
state: state.clone(),
sender: sender.clone(),
ignore: ignore.clone(),
abort: abort.clone(),
root: root.clone(),
}
}
#[instrument(parent = None, name = "FSWatcher", skip_all, fields(name = self.name))]
pub async fn start(self) {
let (mut rx, _w) = self.get_watcher().unwrap();
let watchignore = self.ignore.iter().map(AsRef::as_ref).collect::<Vec<&str>>();
let ignore = wax::any::<wax::Glob, _>(watchignore).unwrap();
tracing::info!("Watching");
loop {
tokio::select! {
_ = self.abort.notified() => break,
event = rx.recv() => {
if event.is_none() { break; }
let event = event.unwrap();
let event = match Event::new(&ignore, &self.state, event) {
Some(e) => e,
None => continue,
};
// IGNORE EVENTS OF RENAME FOR PATHS THAT NO LONGER EXISTS
if !event.path().exists() && event.is_rename_event() {
tracing::debug!("{} [ignored]", event);
continue;
}
self.sender.send(PRMessage::FSEvent(event)).ok();
}
}
}
tracing::info!("[Dropped]");
}
fn get_watcher(&self) -> Result<(Receiver<notify::Event>, impl notify::Watcher)> {
use notify::{Config, RecommendedWatcher, RecursiveMode::Recursive, Watcher};
let (tx, rx) = channel::<notify::Event>(1);
let create = <RecommendedWatcher as Watcher>::new;
let to_err = |e: notify::Error| crate::Error::Unexpected(e.to_string());
let mut watcher = create(move |res: notify::Result<notify::Event>| {
res.map(|event| tx.blocking_send(event).unwrap()).ok();
})
.map_err(to_err)?;
watcher.watch(&self.root, Recursive).map_err(to_err)?;
watcher
.configure(Config::NoticeEvents(true))
.map_err(to_err)?;
Ok((rx, watcher))
}
}
/// Trait to make an object react to filesystem changes.
#[async_trait]
pub trait Watchable: ToString + Send + Sync + 'static {
/// Trigger Restart of Watchable.
async fn trigger(
&self,
project: &mut ProjectImpl,
ev: &Event,
b: &Arc<Broadcast>,
) -> Result<()>;
/// A function that controls whether a a Watchable should restart
async fn should_trigger(&self, ev: &Event) -> bool;
/// A function that controls whether a watchable should be dropped
async fn should_discard(&self, ev: &Event) -> bool;
/// Drop watchable for watching a given file system
async fn discard(&self);
}
#[derive(Default)]
pub struct WatchSubscribers {
name: String,
inner: HashMap<String, Box<(dyn Watchable + Send + Sync + 'static)>>,
}
impl WatchSubscribers {
pub fn new(name: &String) -> Self {
Self {
name: name.clone(),
inner: Default::default(),
}
}
#[instrument(parent = None, name = "FSWatcher", skip_all, fields(name = self.name))]
pub fn add<W: Watchable>(&mut self, watchable: W) {
let key = watchable.to_string();
if self.inner.contains_key(&key) {
warn!("trying to add {key}!!");
} else {
self.inner.insert(key, Box::new(watchable));
}
}
#[instrument(parent = None, name = "FSWatcher", skip_all, fields(name = self.name))]
pub async fn remove<S: ToString>(&mut self, t: &S) {
let key = t.to_string();
if let Some(w) = self.inner.remove(&key) {
w.discard().await;
info!("Removed watch subscriber: `{key}`");
} else {
error!("Trying to remove non-existent watch subscriber: `{key}`")
}
}
pub fn keys(&self) -> Vec<String> {
self.inner.keys().map(ToString::to_string).collect()
}
#[instrument(parent = None, name = "FSWatcher", skip_all, fields(name = self.name))]
pub async fn trigger(
&mut self,
project: &mut ProjectImpl,
event: &Event,
broadcast: &Arc<Broadcast>,
) {
let mut discards = vec![];
for (key, w) in self.inner.iter() {
if w.should_discard(&event).await {
w.discard().await;
discards.push(key.to_string());
} else if w.should_trigger(&event).await {
let trigger = w.trigger(project, event, broadcast);
if let Err(err) = trigger.await {
error!("trigger errored for `{key}`!: {err}");
}
}
}
for key in discards {
info!("Discarded: `{key}`");
self.inner.remove(&key);
}
}
}
#[derive(Clone)]
pub struct WatcherState {
debounce: Arc<Mutex<SystemTime>>,
last_path: Arc<Mutex<PathBuf>>,
}
impl WatcherState {
pub fn new() -> Self {
Self {
debounce: Arc::new(Mutex::new(SystemTime::now())),
last_path: Default::default(),
}
}
pub fn update_debounce(&self) {
let mut debounce = self.debounce.lock().unwrap();
*debounce = SystemTime::now();
tracing::trace!("Debounce updated!!!");
}
pub fn last_run(&self) -> u128 {
self.debounce.lock().unwrap().elapsed().unwrap().as_millis()
}
/// Get a reference to the internal state's last path.
#[must_use]
pub fn last_path(&self) -> Arc<Mutex<PathBuf>> {
self.last_path.clone()
}
}