Page MenuHomePhorge

No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0fc9bf3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+/target
+/Cargo.lock
+/.maid
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..15c7c03
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,10 @@
+[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"
diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 0000000..98fd82f
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,9 @@
+# MIT License
+
+### Copyright (c) 2023 theMackabu [theMackabu@gmail.com]
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+_The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software._
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e69de29
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..0be3ea9
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,116 @@
+use psutil::process::{MemoryInfo, Process};
+use std::io::{self, Read};
+use std::process::{Command, Stdio};
+use std::time::{Duration, Instant};
+
+#[derive(Debug)]
+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 memory_usage: Option<MemoryInfo>,
+ pub cpu_usage: f64,
+ pub uptime: u128,
+}
+
+#[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 = std::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 mut memory_usage: Option<MemoryInfo> = None;
+ if let Ok(process) = Process::new(pid) {
+ if let Ok(info) = process.memory_info() {
+ memory_usage = Some(info);
+ }
+ }
+
+ let elapsed_time = start_time.elapsed().as_millis();
+ let result = PawResult {
+ info: PawInfo {
+ memory_usage,
+ cpu_usage: 0.0,
+ uptime: elapsed_time,
+ },
+ process: PawProcess {
+ cmd: &self.command,
+ args: &self.arguments,
+ },
+ };
+
+ callback(result);
+ std::thread::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/tests/test.js b/tests/test.js
new file mode 100644
index 0000000..a5cfa69
--- /dev/null
+++ b/tests/test.js
@@ -0,0 +1,3 @@
+const arr = [];
+for (let i = 0; i < 9000000; i++) arr.push(i);
+setTimeout(() => console.log(arr), 2000);

File Metadata

Mime Type
text/x-diff
Expires
Sun, Feb 1, 6:31 PM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
494883
Default Alt Text
(5 KB)

Event Timeline