Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions crates/argus-app/src/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::time::{Duration, Instant};

use crate::channel::Channel;
use argus_core::history::{self, HistoryRecorder};
use argus_core::watch::{WatchAction, WatchError, WatchRecorder, WatchView};
use argus_core::{Collector, Snapshot};
use argus_service::{DiskClient, NetworkClient, RuleRunner, SensorClient};

Expand Down Expand Up @@ -66,6 +67,11 @@ pub enum Control {
/// Deletes the history in this folder; replies once the files are gone.
/// Goes through this thread so no frame is being written meanwhile.
ClearHistory(PathBuf, Sender<io::Result<()>>),
Watch(
PathBuf,
WatchAction,
Sender<Result<Option<WatchView>, WatchError>>,
),
}

pub struct Sampler {
Expand Down Expand Up @@ -204,6 +210,7 @@ impl Sampler {
let mut subscriber: Option<Channel<Snapshot>> = None;
let mut window: Option<Probe> = None;
let mut history: Option<HistoryRecorder> = None;
let mut watch: Option<WatchRecorder> = None;
let mut interval = DEFAULT_INTERVAL;
let mut minimized = BACKGROUND_INTERVAL;
let mut tray = BACKGROUND_INTERVAL;
Expand Down Expand Up @@ -255,11 +262,32 @@ impl Sampler {
history = dir.map(HistoryRecorder::new);
continue;
}
Ok(Control::Watch(dir, action, reply)) => {
let result = if let Some(recorder) = &mut watch {
recorder.action(action)
} else if matches!(action, WatchAction::Delete) {
WatchRecorder::delete_saved(&dir).map(|()| None)
} else {
WatchRecorder::open(dir).and_then(|mut recorder| {
let result = recorder.action(action);
watch = Some(recorder);
result
})
};
let _ = reply.send(result);
continue;
}
Ok(Control::ClearHistory(dir, reply)) => {
let result = match &mut history {
Some(recorder) => recorder.clear(),
None => history::clear(&dir),
let observed = match &mut watch {
Some(recorder) => recorder.action(WatchAction::Delete).map(|_| ()),
None => WatchRecorder::delete_saved(&dir),
};
let result = observed
.map_err(|_| io::Error::other("process observation cleanup failed"))
.and_then(|()| match &mut history {
Some(recorder) => recorder.clear(),
None => history::clear(&dir),
});
let _ = reply.send(result);
continue;
}
Expand Down Expand Up @@ -296,6 +324,14 @@ impl Sampler {
if let Some(recorder) = &mut history {
recorder.record(&snapshot);
}
if let Some(recorder) = &mut watch
&& recorder.record(&snapshot).is_err()
{
argus_core::diagnostics::warn(
"process observation",
"recording interrupted",
);
}
// A paused view stays as it is, except right after an action.
let send =
publish_refresh(&mut owed, collector.process_sample_started()) || !paused;
Expand Down
148 changes: 148 additions & 0 deletions crates/argus-app/tests/process_watch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//! Real sampler integration; only observes this test process and writes its own temp folder.
use argus_app::{
channel::Channel,
sampler::{Control, Sampler},
};
use argus_core::{
Collector, MetaStore,
watch::{WatchAction, WatchError, WatchMode, WatchRecorder, WatchState, WatchView},
};
use std::sync::{Arc, mpsc};
use std::time::Duration;

fn request(
sampler: &Sampler,
dir: &std::path::Path,
action: WatchAction,
) -> Result<Option<WatchView>, WatchError> {
let (tx, rx) = mpsc::channel();
sampler.send(Control::Watch(dir.to_owned(), action, tx));
rx.recv_timeout(Duration::from_secs(15)).unwrap()
}

#[test]
#[ignore = "real Windows collector integration, run explicitly with --ignored"]
fn observes_and_deletes_through_the_real_sampler() {
let dir = std::env::temp_dir().join(format!("argus-sampler-watch-{}", std::process::id()));
let rules = Arc::new(argus_service::RuleRunner::spawn(false));
// No rules are installed and no service operations or system settings are changed.
let sampler = Sampler::spawn(|| Collector::new(MetaStore::new()), rules);
let (tx, rx) = mpsc::channel();
sampler.send(Control::Subscribe(Channel::new(move |snapshot| {
tx.send(snapshot)?;
Ok(())
})));
let snapshot = rx.recv_timeout(Duration::from_secs(30)).unwrap();
let own = snapshot
.processes
.iter()
.find(|p| p.pid == std::process::id())
.unwrap();
let start = WatchAction::Start {
pid: own.pid,
create_time: own.create_time,
name: "sampler-owned-test".into(),
};
request(&sampler, &dir, start).unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(15);
loop {
let _ = rx.recv_timeout(Duration::from_secs(10)).unwrap();
let view = request(&sampler, &dir, WatchAction::Status)
.unwrap()
.unwrap();
if view.points.iter().any(|point| point.memory.is_some()) {
break;
}
assert!(
std::time::Instant::now() < deadline,
"no valid observation through sampler"
);
}
assert_eq!(
request(&sampler, &dir, WatchAction::Stop)
.unwrap()
.unwrap()
.state,
WatchState::Stopped
);
assert!(
request(&sampler, &dir, WatchAction::Delete)
.unwrap()
.is_none()
);
assert!(!dir.join("process-watch.jsonl").exists());
request(
&sampler,
&dir,
WatchAction::Start {
pid: own.pid,
create_time: own.create_time,
name: "sampler-owned-test".into(),
},
)
.unwrap();
let (clear_tx, clear_rx) = mpsc::channel();
sampler.send(Control::ClearHistory(dir.clone(), clear_tx));
clear_rx
.recv_timeout(Duration::from_secs(15))
.unwrap()
.unwrap();
assert!(
request(&sampler, &dir, WatchAction::Status)
.unwrap()
.is_none()
);
request(
&sampler,
&dir,
WatchAction::StartProgram {
pid: own.pid,
create_time: own.create_time,
name: "sampler-owned-program".into(),
},
)
.unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(15);
loop {
let _ = rx.recv_timeout(Duration::from_secs(10)).unwrap();
let view = request(&sampler, &dir, WatchAction::Status)
.unwrap()
.unwrap();
assert_eq!(view.mode, WatchMode::Program);
if view.points.iter().any(|point| point.memory.is_some()) {
break;
}
assert!(std::time::Instant::now() < deadline);
}
let stopped = request(&sampler, &dir, WatchAction::Stop).unwrap().unwrap();
assert_eq!(stopped.state, WatchState::Stopped);
assert_eq!(stopped.live_runs, 0);
assert!(WatchRecorder::storage_bytes(&dir) > 0);
request(&sampler, &dir, WatchAction::Delete).unwrap();
assert_eq!(WatchRecorder::storage_bytes(&dir), 0);
request(
&sampler,
&dir,
WatchAction::StartProgram {
pid: own.pid,
create_time: own.create_time,
name: "sampler-owned-program".into(),
},
)
.unwrap();
let (clear_tx, clear_rx) = mpsc::channel();
sampler.send(Control::ClearHistory(dir.clone(), clear_tx));
clear_rx
.recv_timeout(Duration::from_secs(15))
.unwrap()
.unwrap();
assert!(
request(&sampler, &dir, WatchAction::Status)
.unwrap()
.is_none()
);
assert_eq!(WatchRecorder::storage_bytes(&dir), 0);
drop(sampler);
drop(rx);
std::fs::remove_dir(dir).unwrap();
}
Loading