Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit d9961d5

Browse files
committed
...
1 parent 4703472 commit d9961d5

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async fn main() -> Result<()> {
167167
)
168168
.get_matches();
169169

170-
use dirs; // Add this import
170+
use dirs; // Add this import
171171

172172
let socket = matches.value_of("socket").unwrap();
173173
let debug = matches.is_present("debug");
@@ -177,8 +177,15 @@ use dirs; // Add this import
177177
} else {
178178
#[cfg(target_os = "macos")]
179179
{
180-
let home_dir = dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
181-
home_dir.join("hero").join("cfg").join("zinit").to_str().ok_or_else(|| anyhow::anyhow!("Invalid path for config directory"))?.to_string()
180+
let home_dir = dirs::home_dir()
181+
.ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
182+
home_dir
183+
.join("hero")
184+
.join("cfg")
185+
.join("zinit")
186+
.to_str()
187+
.ok_or_else(|| anyhow::anyhow!("Invalid path for config directory"))?
188+
.to_string()
182189
}
183190
#[cfg(not(target_os = "macos"))]
184191
{

src/zinit/lifecycle.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use crate::zinit::errors::ZInitError;
55
use crate::zinit::ord::{service_dependency_order, ProcessDAG, DUMMY_ROOT};
66
use crate::zinit::service::ZInitService;
77
use crate::zinit::state::{State, Target};
8-
use crate::zinit::types::{ProcessStats, ServiceStats, ServiceTable};
98
#[cfg(target_os = "linux")]
109
use crate::zinit::types::Watcher;
11-
use sysinfo::{self, PidExt, ProcessExt, System, SystemExt};
10+
use crate::zinit::types::{ProcessStats, ServiceStats, ServiceTable};
1211
use std::collections::HashMap;
12+
use sysinfo::{self, PidExt, ProcessExt, System, SystemExt};
1313

1414
// Define a local extension trait for WaitStatus
1515
trait WaitStatusExt {
@@ -32,9 +32,9 @@ use std::sync::Arc;
3232
#[cfg(target_os = "linux")]
3333
use tokio::sync::mpsc;
3434
use tokio::sync::{Notify, RwLock};
35+
use tokio::time::sleep;
3536
#[cfg(target_os = "linux")]
3637
use tokio::time::timeout;
37-
use tokio::time::sleep;
3838
#[cfg(target_os = "linux")]
3939
use tokio_stream::StreamExt;
4040

@@ -245,27 +245,27 @@ impl LifecycleManager {
245245
async fn get_process_stats(&self, pid: i32) -> Result<(u64, f32)> {
246246
// Create a new System instance
247247
let mut system = System::new();
248-
248+
249249
// Convert i32 pid to sysinfo::Pid
250250
let sys_pid = sysinfo::Pid::from(pid as usize);
251-
251+
252252
// First refresh to get initial CPU values
253253
system.refresh_process(sys_pid);
254-
254+
255255
// Wait a short time for CPU measurement
256256
sleep(std::time::Duration::from_millis(100)).await;
257-
257+
258258
// Refresh again to get updated CPU values
259259
system.refresh_process(sys_pid);
260-
260+
261261
// Get the process
262262
if let Some(process) = system.process(sys_pid) {
263263
// Get memory in bytes
264264
let memory_usage = process.memory();
265-
265+
266266
// Get CPU usage as percentage
267267
let cpu_usage = process.cpu_usage();
268-
268+
269269
Ok((memory_usage, cpu_usage))
270270
} else {
271271
// Process not found
@@ -278,22 +278,22 @@ impl LifecycleManager {
278278
// Create a new System instance with all processes information
279279
let mut system = System::new_all();
280280
system.refresh_all();
281-
281+
282282
// Convert i32 pid to sysinfo::Pid
283283
let sys_pid = sysinfo::Pid::from(parent_pid as usize);
284-
284+
285285
// Wait a short time for CPU measurement
286286
sleep(std::time::Duration::from_millis(100)).await;
287-
287+
288288
// Refresh processes to get updated CPU values
289289
system.refresh_processes();
290-
290+
291291
let mut children = Vec::new();
292-
292+
293293
// Recursively collect all descendant PIDs
294294
let mut descendant_pids = Vec::new();
295295
self.collect_descendants(sys_pid, &system.processes(), &mut descendant_pids);
296-
296+
297297
// Get stats for each child process
298298
for &child_pid in &descendant_pids {
299299
if let Some(process) = system.process(child_pid) {
@@ -304,10 +304,10 @@ impl LifecycleManager {
304304
});
305305
}
306306
}
307-
307+
308308
Ok(children)
309309
}
310-
310+
311311
/// Recursively collect all descendant PIDs of a process
312312
fn collect_descendants(
313313
&self,
@@ -328,7 +328,7 @@ impl LifecycleManager {
328328
info!("shutting down");
329329
#[cfg(target_os = "linux")]
330330
return self.power(RebootMode::RB_POWER_OFF).await;
331-
331+
332332
#[cfg(not(target_os = "linux"))]
333333
{
334334
*self.shutdown.write().await = true;
@@ -346,7 +346,7 @@ impl LifecycleManager {
346346
info!("rebooting");
347347
#[cfg(target_os = "linux")]
348348
return self.power(RebootMode::RB_AUTOBOOT).await;
349-
349+
350350
#[cfg(not(target_os = "linux"))]
351351
{
352352
*self.shutdown.write().await = true;

src/zinit/types.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
use nix::sys::wait::WaitStatus;
2+
use serde::{Deserialize, Serialize};
23
use std::collections::HashMap;
34
use std::sync::Arc;
45
use tokio::sync::watch;
56
use tokio::sync::RwLock;
67
use tokio_stream::wrappers::WatchStream;
7-
use serde::{Deserialize, Serialize};
88

99
/// Stats information for a service
1010
#[derive(Debug, Clone, Serialize, Deserialize)]
1111
pub struct ServiceStats {
1212
/// Memory usage in bytes
1313
pub memory_usage: u64,
14-
14+
1515
/// CPU usage as a percentage (0-100)
1616
pub cpu_usage: f32,
17-
17+
1818
/// Process ID of the service
1919
pub pid: i32,
20-
20+
2121
/// Child process stats if any
2222
pub children: Vec<ProcessStats>,
2323
}
@@ -27,10 +27,10 @@ pub struct ServiceStats {
2727
pub struct ProcessStats {
2828
/// Process ID
2929
pub pid: i32,
30-
30+
3131
/// Memory usage in bytes
3232
pub memory_usage: u64,
33-
33+
3434
/// CPU usage as a percentage (0-100)
3535
pub cpu_usage: f32,
3636
}

0 commit comments

Comments
 (0)