aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-06-19 09:37:44 +0100
committerdiogo464 <[email protected]>2025-06-19 09:37:44 +0100
commit2bdfafbfe14c300b63a7659e8ded45bad42c8208 (patch)
tree758ebccf04bb3e0748beea20e7fc85d22bf2ea51 /src/main.rs
parent000607fdd40887f3735281e0383d8f6c80a3d382 (diff)
Rename PidFileData to PidFile
- Rename struct and update all references throughout codebase - Cleaner, more intuitive name that follows Rust conventions - Update documentation comments - No functional changes, purely cosmetic refactor 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs54
1 files changed, 27 insertions, 27 deletions
diff --git a/src/main.rs b/src/main.rs
index c82208b..4ab1dd9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,20 +11,20 @@ use std::time::Duration;
11 11
12/// Represents the contents of a PID file 12/// Represents the contents of a PID file
13#[derive(Debug, Clone)] 13#[derive(Debug, Clone)]
14struct PidFileData { 14struct PidFile {
15 /// Process ID 15 /// Process ID
16 pid: u32, 16 pid: u32,
17 /// Command that was executed (program + arguments) 17 /// Command that was executed (program + arguments)
18 command: Vec<String>, 18 command: Vec<String>,
19} 19}
20 20
21impl PidFileData { 21impl PidFile {
22 /// Create a new PidFileData instance 22 /// Create a new PidFile instance
23 fn new(pid: u32, command: Vec<String>) -> Self { 23 fn new(pid: u32, command: Vec<String>) -> Self {
24 Self { pid, command } 24 Self { pid, command }
25 } 25 }
26 26
27 /// Write PID file data to a file 27 /// Write PID file to a file
28 fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> { 28 fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
29 let mut file = File::create(path)?; 29 let mut file = File::create(path)?;
30 writeln!(file, "{}", self.pid)?; 30 writeln!(file, "{}", self.pid)?;
@@ -34,7 +34,7 @@ impl PidFileData {
34 Ok(()) 34 Ok(())
35 } 35 }
36 36
37 /// Read PID file data from a file 37 /// Read PID file from a file
38 fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self> { 38 fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
39 let contents = std::fs::read_to_string(path)?; 39 let contents = std::fs::read_to_string(path)?;
40 let lines: Vec<&str> = contents.lines().collect(); 40 let lines: Vec<&str> = contents.lines().collect();
@@ -244,8 +244,8 @@ fn run_daemon(id: &str, command: &[String]) -> Result<()> {
244 .with_context(|| format!("Failed to start process '{}' with args {:?}", program, args))?; 244 .with_context(|| format!("Failed to start process '{}' with args {:?}", program, args))?;
245 245
246 // Write PID and command to file 246 // Write PID and command to file
247 let pid_data = PidFileData::new(child.id(), command.to_vec()); 247 let pid_file_data = PidFile::new(child.id(), command.to_vec());
248 pid_data.write_to_file(&pid_file)?; 248 pid_file_data.write_to_file(&pid_file)?;
249 249
250 // Don't wait for the child - let it run detached 250 // Don't wait for the child - let it run detached
251 std::mem::forget(child); 251 std::mem::forget(child);
@@ -261,14 +261,14 @@ fn is_process_running(pid_file: &str) -> Result<bool> {
261 return Ok(false); // No PID file means no running process 261 return Ok(false); // No PID file means no running process
262 } 262 }
263 263
264 let pid_data = match PidFileData::read_from_file(pid_file) { 264 let pid_file_data = match PidFile::read_from_file(pid_file) {
265 Ok(data) => data, 265 Ok(data) => data,
266 Err(_) => return Ok(false), // Invalid PID file 266 Err(_) => return Ok(false), // Invalid PID file
267 }; 267 };
268 268
269 // Check if process is still running using kill -0 269 // Check if process is still running using kill -0
270 let output = Command::new("kill") 270 let output = Command::new("kill")
271 .args(&["-0", &pid_data.pid.to_string()]) 271 .args(&["-0", &pid_file_data.pid.to_string()])
272 .output()?; 272 .output()?;
273 273
274 Ok(output.status.success()) 274 Ok(output.status.success())
@@ -278,7 +278,7 @@ fn stop_daemon(id: &str, timeout: u64) -> Result<()> {
278 let pid_file = format!("{}.pid", id); 278 let pid_file = format!("{}.pid", id);
279 279
280 // Check if PID file exists and read PID data 280 // Check if PID file exists and read PID data
281 let pid_data = match PidFileData::read_from_file(&pid_file) { 281 let pid_file_data = match PidFile::read_from_file(&pid_file) {
282 Ok(data) => data, 282 Ok(data) => data,
283 Err(_) => { 283 Err(_) => {
284 if Path::new(&pid_file).exists() { 284 if Path::new(&pid_file).exists() {
@@ -291,7 +291,7 @@ fn stop_daemon(id: &str, timeout: u64) -> Result<()> {
291 } 291 }
292 }; 292 };
293 293
294 let pid = pid_data.pid; 294 let pid = pid_file_data.pid;
295 295
296 tracing::info!( 296 tracing::info!(
297 "Stopping daemon '{}' (PID: {}) with timeout {}s", 297 "Stopping daemon '{}' (PID: {}) with timeout {}s",
@@ -599,19 +599,19 @@ fn list_daemons(quiet: bool) -> Result<()> {
599 let id = path_str.strip_suffix(".pid").unwrap_or(&path_str); 599 let id = path_str.strip_suffix(".pid").unwrap_or(&path_str);
600 600
601 // Read PID data from file 601 // Read PID data from file
602 match PidFileData::read_from_file(&path) { 602 match PidFile::read_from_file(&path) {
603 Ok(pid_data) => { 603 Ok(pid_file_data) => {
604 let status = if is_process_running_by_pid(pid_data.pid) { 604 let status = if is_process_running_by_pid(pid_file_data.pid) {
605 "RUNNING" 605 "RUNNING"
606 } else { 606 } else {
607 "DEAD" 607 "DEAD"
608 }; 608 };
609 609
610 if quiet { 610 if quiet {
611 println!("{}:{}:{}", id, pid_data.pid, status); 611 println!("{}:{}:{}", id, pid_file_data.pid, status);
612 } else { 612 } else {
613 let command = pid_data.command_string(); 613 let command = pid_file_data.command_string();
614 println!("{:<20} {:<8} {:<10} {}", id, pid_data.pid, status, command); 614 println!("{:<20} {:<8} {:<10} {}", id, pid_file_data.pid, status, command);
615 } 615 }
616 } 616 }
617 Err(_) => { 617 Err(_) => {
@@ -649,12 +649,12 @@ fn status_daemon(id: &str) -> Result<()> {
649 } 649 }
650 650
651 // Read PID data from file 651 // Read PID data from file
652 match PidFileData::read_from_file(&pid_file) { 652 match PidFile::read_from_file(&pid_file) {
653 Ok(pid_data) => { 653 Ok(pid_file_data) => {
654 println!("PID: {}", pid_data.pid); 654 println!("PID: {}", pid_file_data.pid);
655 println!("Command: {}", pid_data.command_string()); 655 println!("Command: {}", pid_file_data.command_string());
656 656
657 if is_process_running_by_pid(pid_data.pid) { 657 if is_process_running_by_pid(pid_file_data.pid) {
658 println!("Status: RUNNING"); 658 println!("Status: RUNNING");
659 659
660 // Show file information 660 // Show file information
@@ -696,13 +696,13 @@ fn clean_orphaned_files() -> Result<()> {
696 let id = path_str.strip_suffix(".pid").unwrap_or(&path_str); 696 let id = path_str.strip_suffix(".pid").unwrap_or(&path_str);
697 697
698 // Read PID data from file 698 // Read PID data from file
699 match PidFileData::read_from_file(&path) { 699 match PidFile::read_from_file(&path) {
700 Ok(pid_data) => { 700 Ok(pid_file_data) => {
701 // Check if process is still running 701 // Check if process is still running
702 if !is_process_running_by_pid(pid_data.pid) { 702 if !is_process_running_by_pid(pid_file_data.pid) {
703 println!( 703 println!(
704 "Cleaning up orphaned files for '{}' (PID: {})", 704 "Cleaning up orphaned files for '{}' (PID: {})",
705 id, pid_data.pid 705 id, pid_file_data.pid
706 ); 706 );
707 707
708 // Remove PID file 708 // Remove PID file
@@ -737,7 +737,7 @@ fn clean_orphaned_files() -> Result<()> {
737 tracing::info!( 737 tracing::info!(
738 "Skipping '{}' (PID: {}) - process is still running", 738 "Skipping '{}' (PID: {}) - process is still running",
739 id, 739 id,
740 pid_data.pid 740 pid_file_data.pid
741 ); 741 );
742 } 742 }
743 } 743 }