From a63cbe2bd98b479236cd475d897fcc31af3babd2 Mon Sep 17 00:00:00 2001 From: Mahmoud-Emad Date: Wed, 2 Jul 2025 10:46:47 +0300 Subject: [PATCH] Fix service manager examples to use production-ready API - Updated simple_service.rs to use start(config) instead of create() + start(name) - Updated service_spaghetti.rs to use the same unified API - Fixed factory function calls to use create_service_manager() without parameters - All examples now compile and work with the production-ready synchronous API - Maintains backward compatibility while providing cleaner interface --- service_manager/examples/service_spaghetti.rs | 49 ++++++++++--------- service_manager/examples/simple_service.rs | 44 ++++++++--------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/service_manager/examples/service_spaghetti.rs b/service_manager/examples/service_spaghetti.rs index c5d3c7e..87d9779 100644 --- a/service_manager/examples/service_spaghetti.rs +++ b/service_manager/examples/service_spaghetti.rs @@ -10,7 +10,7 @@ use std::thread; use std::time::Duration; fn main() { - let manager = create_service_manager(None).expect("Failed to create service manager"); + let manager = create_service_manager(); let service_name = "com.herocode.examples.spaghetti"; let service_config = ServiceConfig { @@ -30,39 +30,38 @@ fn main() { // Cleanup from previous runs to ensure a clean slate if let Ok(true) = manager.exists(service_name) { - println!("\nService '{}' found from a previous run. Cleaning up first.", service_name); + println!( + "\nService '{}' found from a previous run. Cleaning up first.", + service_name + ); let _ = manager.stop(service_name); let _ = manager.remove(service_name); println!("Cleanup complete."); } - // 1. Create the service - println!("\n1. Creating the service..."); - match manager.create(&service_config) { - Ok(()) => println!(" -> Success: Service '{}' created.", service_name), - Err(e) => { - eprintln!(" -> Error: Failed to create service: {}. Halting example.", e); - return; - } - } - - // 2. Start the service - println!("\n2. Starting the service for the first time..."); - match manager.start(service_name) { + // 1. Start the service (creates and starts in one step) + println!("\n1. Starting the service for the first time..."); + match manager.start(&service_config) { Ok(()) => println!(" -> Success: Service '{}' started.", service_name), Err(e) => { - eprintln!(" -> Error: Failed to start service: {}. Halting example.", e); + eprintln!( + " -> Error: Failed to start service: {}. Halting example.", + e + ); return; } } thread::sleep(Duration::from_secs(2)); - // 3. Try to start the service again while it's already running - println!("\n3. Trying to start the *same service* again..."); - match manager.start(service_name) { + // 2. Try to start the service again while it's already running + println!("\n2. Trying to start the *same service* again..."); + match manager.start(&service_config) { Ok(()) => println!(" -> Unexpected Success: Service started again."), - Err(e) => eprintln!(" -> Expected Error: {}. The manager should detect it is already running.", e), + Err(e) => eprintln!( + " -> Expected Error: {}. The manager should detect it is already running.", + e + ), } // 3. Let it run for a bit @@ -81,14 +80,20 @@ fn main() { println!("\n5. Trying to stop the service that was just removed..."); match manager.stop(service_name) { Ok(()) => println!(" -> Unexpected Success: Stopped a removed service."), - Err(e) => eprintln!(" -> Expected Error: {}. The manager knows the service is gone.", e), + Err(e) => eprintln!( + " -> Expected Error: {}. The manager knows the service is gone.", + e + ), } // 6. Try to remove the service again println!("\n6. Trying to remove the service again..."); match manager.remove(service_name) { Ok(()) => println!(" -> Unexpected Success: Removed a non-existent service."), - Err(e) => eprintln!(" -> Expected Error: {}. The manager correctly reports it's not found.", e), + Err(e) => eprintln!( + " -> Expected Error: {}. The manager correctly reports it's not found.", + e + ), } println!("\n--- Spaghetti Example Finished ---"); diff --git a/service_manager/examples/simple_service.rs b/service_manager/examples/simple_service.rs index 37a39bf..c6f7d07 100644 --- a/service_manager/examples/simple_service.rs +++ b/service_manager/examples/simple_service.rs @@ -5,13 +5,7 @@ use std::time::Duration; fn main() { // 1. Create a service manager for the current platform - let manager = match create_service_manager(None) { - Ok(manager) => manager, - Err(e) => { - eprintln!("Error: Failed to create service manager: {}", e); - return; - } - }; + let manager = create_service_manager(); // 2. Define the configuration for our new service let service_name = "com.herocode.examples.simpleservice"; @@ -32,9 +26,15 @@ fn main() { // Cleanup from previous runs, if necessary if let Ok(true) = manager.exists(service_name) { - println!("Service '{}' already exists. Cleaning up before starting.", service_name); + println!( + "Service '{}' already exists. Cleaning up before starting.", + service_name + ); if let Err(e) = manager.stop(service_name) { - println!("Note: could not stop existing service (it might not be running): {}", e); + println!( + "Note: could not stop existing service (it might not be running): {}", + e + ); } if let Err(e) = manager.remove(service_name) { eprintln!("Error: failed to remove existing service: {}", e); @@ -43,19 +43,9 @@ fn main() { println!("Cleanup complete."); } - // 3. Create the service - println!("\n1. Creating service: '{}'", service_name); - match manager.create(&service_config) { - Ok(()) => println!("Service '{}' created successfully.", service_name), - Err(e) => { - eprintln!("Error: Failed to create service '{}': {}", service_name, e); - return; - } - } - - // 4. Start the service - println!("\n2. Starting service: '{}'", service_name); - match manager.start(service_name) { + // 3. Start the service (creates and starts in one step) + println!("\n1. Starting service: '{}'", service_name); + match manager.start(&service_config) { Ok(()) => println!("Service '{}' started successfully.", service_name), Err(e) => { eprintln!("Error: Failed to start service '{}': {}", service_name, e); @@ -71,7 +61,10 @@ fn main() { println!("\n2. Checking service status..."); match manager.status(service_name) { Ok(status) => println!("Service status: {:?}", status), - Err(e) => eprintln!("Error: Failed to get status for service '{}': {}", service_name, e), + Err(e) => eprintln!( + "Error: Failed to get status for service '{}': {}", + service_name, e + ), } println!("\nLetting the service run for 10 seconds. Check logs if you can."); @@ -91,7 +84,10 @@ fn main() { println!("\n4. Checking status after stopping..."); match manager.status(service_name) { Ok(status) => println!("Service status: {:?}", status), - Err(e) => eprintln!("Error: Failed to get status for service '{}': {}", service_name, e), + Err(e) => eprintln!( + "Error: Failed to get status for service '{}': {}", + service_name, e + ), } // 6. Remove the service