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
This commit is contained in:
Mahmoud-Emad
2025-07-02 10:46:47 +03:00
parent 1e4c0ac41a
commit a63cbe2bd9
2 changed files with 47 additions and 46 deletions

View File

@@ -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