This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from tsirysndr/feat/watch-dir-changes
feat(superviseur): restart service on events in working directory
- Loading branch information
Showing
6 changed files
with
250 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use std::{ | ||
path::Path, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
thread, | ||
time::Duration, | ||
}; | ||
|
||
use futures_util::Future; | ||
use notify::{Config, Error, Event, PollWatcher, RecommendedWatcher, Watcher, WatcherKind}; | ||
use tokio::sync::mpsc; | ||
|
||
use crate::{superviseur::SuperviseurCommand, types::configuration::Service}; | ||
|
||
pub struct WatchForChanges {} | ||
|
||
impl WatchForChanges { | ||
pub fn new( | ||
dir: String, | ||
superviseur_tx: mpsc::UnboundedSender<SuperviseurCommand>, | ||
service: Service, | ||
project: String, | ||
) -> Self { | ||
thread::spawn(move || { | ||
let internal = WatchForChangesInternal::new(&dir, superviseur_tx, service, project); | ||
futures::executor::block_on(internal); | ||
}); | ||
Self {} | ||
} | ||
} | ||
|
||
struct WatchForChangesInternal { | ||
cmd_rx: mpsc::UnboundedReceiver<notify::Result<notify::Event>>, | ||
superviseur_tx: mpsc::UnboundedSender<SuperviseurCommand>, | ||
service: Service, | ||
project: String, | ||
watcher: Box<dyn Watcher>, | ||
} | ||
|
||
impl WatchForChangesInternal { | ||
pub fn new( | ||
dir: &str, | ||
superviseur_tx: mpsc::UnboundedSender<SuperviseurCommand>, | ||
service: Service, | ||
project: String, | ||
) -> Self { | ||
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); | ||
let mut watcher: Box<dyn Watcher> = | ||
if RecommendedWatcher::kind() == WatcherKind::PollWatcher { | ||
// custom config for PollWatcher kind | ||
// you | ||
let config = Config::default().with_poll_interval(Duration::from_secs(1)); | ||
Box::new( | ||
PollWatcher::new( | ||
move |result: Result<Event, Error>| { | ||
cmd_tx.send(result).unwrap(); | ||
}, | ||
config, | ||
) | ||
.unwrap(), | ||
) | ||
} else { | ||
// use default config for everything else | ||
Box::new( | ||
RecommendedWatcher::new( | ||
move |result: Result<Event, Error>| { | ||
cmd_tx.send(result).unwrap(); | ||
}, | ||
Config::default(), | ||
) | ||
.unwrap(), | ||
) | ||
}; | ||
watcher | ||
.watch(Path::new(dir), notify::RecursiveMode::Recursive) | ||
.unwrap(); | ||
Self { | ||
cmd_rx, | ||
service, | ||
project, | ||
superviseur_tx, | ||
watcher, | ||
} | ||
} | ||
} | ||
|
||
impl Future for WatchForChangesInternal { | ||
type Output = (); | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
loop { | ||
let event = match self.cmd_rx.poll_recv(cx) { | ||
Poll::Ready(Some(event)) => Some(event), | ||
Poll::Ready(None) => return Poll::Ready(()), // client has disconnected - shut down. | ||
_ => None, | ||
}; | ||
|
||
if event.is_none() { | ||
return Poll::Pending; | ||
} | ||
|
||
match event { | ||
Some(Ok(_)) => { | ||
self.superviseur_tx | ||
.send(SuperviseurCommand::Restart( | ||
self.service.clone(), | ||
self.project.clone(), | ||
)) | ||
.unwrap(); | ||
} | ||
Some(Err(e)) => println!("watch error: {:?}", e), | ||
None => {} | ||
} | ||
} | ||
} | ||
} |