use super::{hermes, native, store, AutomationOverview, Provider};
use crate::core::path_guard::is_path_safe;
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::{
collections::HashSet,
path::{Path, PathBuf},
time::{Duration, Instant},
};
use tauri::Emitter;
const EVENT: &str = "automations-changed";
fn paths(root: &Path) -> HashSet<PathBuf> {
let mut paths = HashSet::from([root.to_path_buf()]);
paths.extend(super::libraries::watch_paths());
if let Ok(records) = store::load(root) {
for record in records
.into_iter()
.filter(|r| r.draft.provider == Provider::System)
{
let dir = native::directory(root, &record);
if is_path_safe(root, &dir) || super::package::checked(root, &record).is_ok() {
paths.insert(dir);
if let Some(workdir) = record.draft.scripts_dir() {
let manifest = super::package::manifest_path(Path::new(workdir));
if manifest.is_file() {
if let Some(parent) = manifest.parent() {
if is_path_safe(Path::new(workdir), parent) && !parent.is_symlink() {
paths.insert(parent.to_path_buf());
}
}
}
for file in &record.draft.files {
if let Some(parent) = Path::new(&workdir).join(&file.path).parent() {
if is_path_safe(Path::new(&workdir), parent) && !parent.is_symlink() {
paths.insert(parent.to_path_buf());
}
}
}
paths.insert(PathBuf::from(workdir));
}
}
}
}
let mut report = AutomationOverview::default();
hermes::collect(&mut report);
for task in report.tasks {
if let Some(dir) = Path::new(&task.location).parent() {
paths.insert(dir.to_path_buf());
}
}
paths
.into_iter()
.filter_map(|path| path.canonicalize().ok())
.filter(|path| path.is_dir())
.collect()
}
fn relevant(event: &Event, root: &Path, watched: &HashSet<PathBuf>) -> bool {
if matches!(event.kind, EventKind::Access(_)) {
return false;
}
event.paths.iter().any(|path| {
let Some(parent) = path.parent() else {
return false;
};
if !watched.contains(parent) {
return false;
}
if super::libraries::definition_event(path) {
return true;
}
match path.file_name().and_then(|name| name.to_str()) {
Some("tasks.json") => parent == root,
Some("started" | "exit") => {
parent.parent() == Some(root)
|| parent.file_name().is_some_and(|name| name == ".local")
}
Some("jobs.json") => parent != root,
Some(name)
if [".py", ".sh", ".ps1", ".json", ".md"]
.iter()
.any(|extension| name.ends_with(extension)) =>
{
true
}
_ => false,
}
})
}
pub(crate) fn start<R: tauri::Runtime>(app: tauri::AppHandle<R>) {
std::thread::spawn(move || {
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = match RecommendedWatcher::new(
move |event| {
let _ = tx.send(event);
},
notify::Config::default(),
) {
Ok(watcher) => watcher,
Err(error) => {
log::warn!("Cannot observe automation changes: {error}");
return;
}
};
let root = store::root();
let root = root.canonicalize().unwrap_or(root);
if let Err(error) = super::sync_workspace_scripts() {
log::warn!("Cannot synchronize automation scripts: {error}");
}
super::managed::upgrade_runners();
let mut watched = HashSet::new();
let mut rescan_at = Instant::now();
let mut changed_at = None;
let mut initialized = false;
loop {
if Instant::now() >= rescan_at {
let desired = paths(&root);
let previous = watched.clone();
for path in watched.difference(&desired) {
let _ = watcher.unwatch(path);
}
watched.retain(|path| desired.contains(path));
for path in desired.difference(&watched).cloned().collect::<Vec<_>>() {
if watcher.watch(&path, RecursiveMode::NonRecursive).is_ok() {
watched.insert(path);
}
}
if initialized && watched != previous {
changed_at = Some(Instant::now());
}
initialized = true;
rescan_at = Instant::now() + Duration::from_secs(10);
}
match rx.recv_timeout(Duration::from_millis(100)) {
Ok(Ok(event)) if relevant(&event, &root, &watched) => {
let library_root = super::libraries::root();
for path in event.paths.iter().filter(|path| {
path.parent() == Some(library_root.as_path())
&& super::libraries::definition_event(path)
}) {
for old in watched
.iter()
.filter(|watched| watched.starts_with(path))
.cloned()
.collect::<Vec<_>>()
{
let _ = watcher.unwatch(&old);
watched.remove(&old);
}
}
changed_at = Some(Instant::now());
rescan_at = Instant::now();
}
Ok(Err(error)) => log::debug!("Automation observation failed: {error}"),
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
_ => {}
}
if changed_at.is_some_and(|at| at.elapsed() >= Duration::from_millis(400)) {
changed_at = None;
if let Err(error) = super::sync_workspace_scripts() {
log::warn!("Cannot synchronize automation scripts: {error}");
}
super::managed::upgrade_runners();
if super::has_pending_backup() {
super::super::cloud_sync::notify_central_change();
}
let _ = app.emit(EVENT, ());
}
}
});
}
#[cfg(test)]
mod tests;