Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2706613
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/Cargo.toml b/Cargo.toml
index 15c7c03..68ffe33 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,10 +1,12 @@
[package]
name = "pawd"
version = "0.1.0"
edition = "2021"
license = "MIT"
repository = "https://lab.themackabu.dev/crates/pawd"
description = "Process analysis & watcher daemon"
[dependencies]
-psutil = "3.2.2"
+psutil = { version = "3.2.2", features = ["serde"] }
+serde = "1.0.192"
+serde_derive = "1.0.192"
diff --git a/src/lib.rs b/src/lib.rs
index 05656ec..ff41ed8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,114 +1,92 @@
+mod structs;
+
use psutil::process::{MemoryInfo, Process};
use std::io::{self, Read};
use std::process::{Command, Stdio};
use std::thread::{sleep, spawn};
use std::time::{Duration, Instant};
+use structs::{PawDone, PawInfo, PawProcess, PawResult};
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct Paw {
command: String,
arguments: Vec<String>,
duration: Duration,
}
-#[derive(Debug, Clone)]
-pub struct PawResult<'a> {
- pub info: PawInfo,
- pub process: PawProcess<'a>,
-}
-
-#[derive(Debug, Clone)]
-pub struct PawProcess<'a> {
- pub cmd: &'a String,
- pub args: &'a Vec<String>,
-}
-
-#[derive(Debug, Clone)]
-pub struct PawInfo {
- pub uptime: u128,
- pub memory_usage: Option<MemoryInfo>,
- pub cpu_percent: Option<f32>,
-}
-
-#[derive(Debug, Clone)]
-pub struct PawDone {
- pub stdout: String,
- pub code: Option<i32>,
-}
-
impl Paw {
pub fn new<T: AsRef<str>>(command: &str, arguments: &[T], duration: u64) -> Paw {
Paw {
command: command.to_string(),
arguments: arguments.iter().map(|s| s.as_ref().to_string()).collect(),
duration: Duration::from_millis(duration),
}
}
pub fn watch<F: Fn(PawResult) + Send + 'static>(&self, callback: F) -> Result<PawDone, Box<dyn std::error::Error>> {
let mut child = Command::new(&self.command).args(&self.arguments).stdout(Stdio::piped()).spawn()?;
let done: PawDone;
let pid = child.id();
let start_time = Instant::now();
let stdout_handle = child.stdout.take().unwrap();
let stdout_thread = spawn(move || {
let mut buffer = String::new();
let mut reader = io::BufReader::new(stdout_handle);
reader.read_to_string(&mut buffer).unwrap();
buffer
});
loop {
let uptime = start_time.elapsed().as_millis();
let mut memory_usage: Option<MemoryInfo> = None;
let mut cpu_percent: Option<f32> = None;
if let Ok(mut process) = Process::new(pid) {
memory_usage = process.memory_info().ok();
cpu_percent = process.cpu_percent().ok();
}
let result = PawResult {
info: PawInfo { memory_usage, cpu_percent, uptime },
process: PawProcess {
- cmd: &self.command,
- args: &self.arguments,
+ cmd: self.command.clone(),
+ args: self.arguments.clone(),
},
};
callback(result);
sleep(self.duration);
if let Some(status) = child.try_wait()? {
done = PawDone {
stdout: stdout_thread.join().unwrap(),
code: status.code(),
};
break;
}
}
Ok(done)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn watch() {
let paw = Paw::new("node", &["tests/test.js"], 500);
let callback = move |result: PawResult| {
println!("{:?}", result);
};
match paw.watch(callback) {
Ok(result) => println!("{:?}", result),
Err(error) => println!("{error}"),
}
}
}
diff --git a/src/structs.rs b/src/structs.rs
new file mode 100644
index 0000000..3a67bc7
--- /dev/null
+++ b/src/structs.rs
@@ -0,0 +1,27 @@
+use psutil::process::MemoryInfo;
+use serde_derive::{Deserialize, Serialize};
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
+pub struct PawResult {
+ pub info: PawInfo,
+ pub process: PawProcess,
+}
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
+pub struct PawProcess {
+ pub cmd: String,
+ pub args: Vec<String>,
+}
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
+pub struct PawInfo {
+ pub uptime: u128,
+ pub memory_usage: Option<MemoryInfo>,
+ pub cpu_percent: Option<f32>,
+}
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
+pub struct PawDone {
+ pub stdout: String,
+ pub code: Option<i32>,
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Feb 1, 11:31 AM (9 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
494684
Default Alt Text
(4 KB)
Attached To
Mode
R7 Paw
Attached
Detach File
Event Timeline
Log In to Comment