Skip to content

Add CPU and memory usage to service status #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/app/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct Status {
pub state: String,
pub target: String,
pub after: HashMap<String, String>,
pub memory_kb: u64,
pub cpu_percent: f64,
}

pub struct Api {
Expand Down Expand Up @@ -224,6 +226,8 @@ impl Api {
}
after
},
memory_kb: status.memory_kb,
cpu_percent: status.cpu_percent,
};

Ok(encoder::to_value(result)?)
Expand Down
47 changes: 31 additions & 16 deletions src/bin/testapp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate zinit;

use anyhow::Result;
use std::env;
use std::path::Path;

Check warning on line 5 in src/bin/testapp.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `std::path::Path`

Check warning on line 5 in src/bin/testapp.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `std::path::Path`
use tokio::time::{sleep, Duration};
use std::env;

use zinit::app::api::Client;
use zinit::testapp;
Expand All @@ -12,8 +12,16 @@
async fn main() -> Result<()> {
// Define paths for socket and config
let temp_dir = env::temp_dir();
let socket_path = temp_dir.join("zinit-test.sock").to_str().unwrap().to_string();
let config_dir = temp_dir.join("zinit-test-config").to_str().unwrap().to_string();
let socket_path = temp_dir
.join("zinit-test.sock")
.to_str()
.unwrap()
.to_string();
let config_dir = temp_dir
.join("zinit-test-config")
.to_str()
.unwrap()
.to_string();

println!("Starting zinit with socket at: {}", socket_path);
println!("Using config directory: {}", config_dir);
Expand All @@ -29,16 +37,22 @@

// Create service configurations
println!("Creating service configurations...");

// Create a find service
testapp::create_service_config(&config_dir, "find-service", "find / -name \"*.txt\" -type f").await?;

testapp::create_service_config(
&config_dir,
"find-service",
"find / -name \"*.txt\" -type f",
)
.await?;

// Create a sleep service with echo
testapp::create_service_config(
&config_dir,
"sleep-service",
"sh -c 'echo Starting sleep; sleep 30; echo Finished sleep'"
).await?;
&config_dir,
"sleep-service",
"sh -c 'echo Starting sleep; sleep 30; echo Finished sleep'",
)
.await?;

// Wait for zinit to load the configurations
sleep(Duration::from_secs(1)).await;
Expand All @@ -58,7 +72,7 @@
// Start the find service
println!("\nStarting find-service...");
client.start("find-service").await?;

// Wait a bit and check status
sleep(Duration::from_secs(2)).await;
let status = client.status("find-service").await?;
Expand All @@ -67,7 +81,7 @@
// Start the sleep service
println!("\nStarting sleep-service...");
client.start("sleep-service").await?;

// Wait a bit and check status
sleep(Duration::from_secs(2)).await;
let status = client.status("sleep-service").await?;
Expand All @@ -76,7 +90,7 @@
// Stop the find service
println!("\nStopping find-service...");
client.stop("find-service").await?;

// Wait a bit and check status
sleep(Duration::from_secs(2)).await;
let status = client.status("find-service").await?;
Expand All @@ -85,15 +99,16 @@
// Kill the sleep service with SIGTERM
println!("\nKilling sleep-service with SIGTERM...");
client.kill("sleep-service", "SIGTERM").await?;

// Wait a bit and check status
sleep(Duration::from_secs(2)).await;
let status = client.status("sleep-service").await?;
println!("sleep-service status after killing: {:?}", status);

// Cleanup - forget services
println!("\nForgetting services...");
if status.pid == 0 { // Only forget if it's not running
if status.pid == 0 {
// Only forget if it's not running
client.forget("sleep-service").await?;
}
client.forget("find-service").await?;
Expand All @@ -104,4 +119,4 @@

println!("\nTest completed successfully!");
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ extern crate tokio;

pub mod app;
pub mod manager;
pub mod testapp;
pub mod zinit;
pub mod testapp;
12 changes: 6 additions & 6 deletions src/testapp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use std::env;
use std::path::Path;
use std::process::Stdio;
use tokio::process::Command;
use tokio::time::{sleep, Duration};
use std::process::Stdio;
use std::env;

pub async fn start_zinit(socket_path: &str, config_dir: &str) -> Result<()> {
// Create a temporary config directory if it doesn't exist
Expand All @@ -27,12 +27,12 @@ pub async fn start_zinit(socket_path: &str, config_dir: &str) -> Result<()> {
.stderr(Stdio::piped());

let child = cmd.spawn()?;

// Give zinit some time to start up
sleep(Duration::from_secs(1)).await;

println!("Zinit started with PID: {:?}", child.id());

Ok(())
}

Expand All @@ -54,4 +54,4 @@ dir: /

tokio::fs::write(config_path, config_content).await?;
Ok(())
}
}
14 changes: 10 additions & 4 deletions src/zinit/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ impl ZInitError {

/// Create a new InvalidStateTransition error
pub fn invalid_state_transition<S: Into<String>>(message: S) -> Self {
ZInitError::InvalidStateTransition { message: message.into() }
ZInitError::InvalidStateTransition {
message: message.into(),
}
}

/// Create a new DependencyError error
pub fn dependency_error<S: Into<String>>(message: S) -> Self {
ZInitError::DependencyError { message: message.into() }
ZInitError::DependencyError {
message: message.into(),
}
}

/// Create a new ProcessError error
pub fn process_error<S: Into<String>>(message: S) -> Self {
ZInitError::ProcessError { message: message.into() }
ZInitError::ProcessError {
message: message.into(),
}
}
}
}
Loading
Loading