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:
parent
1e4c0ac41a
commit
a63cbe2bd9
@ -10,7 +10,7 @@ use std::thread;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() {
|
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_name = "com.herocode.examples.spaghetti";
|
||||||
|
|
||||||
let service_config = ServiceConfig {
|
let service_config = ServiceConfig {
|
||||||
@ -30,39 +30,38 @@ fn main() {
|
|||||||
|
|
||||||
// Cleanup from previous runs to ensure a clean slate
|
// Cleanup from previous runs to ensure a clean slate
|
||||||
if let Ok(true) = manager.exists(service_name) {
|
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.stop(service_name);
|
||||||
let _ = manager.remove(service_name);
|
let _ = manager.remove(service_name);
|
||||||
println!("Cleanup complete.");
|
println!("Cleanup complete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Create the service
|
// 1. Start the service (creates and starts in one step)
|
||||||
println!("\n1. Creating the service...");
|
println!("\n1. Starting the service for the first time...");
|
||||||
match manager.create(&service_config) {
|
match manager.start(&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) {
|
|
||||||
Ok(()) => println!(" -> Success: Service '{}' started.", service_name),
|
Ok(()) => println!(" -> Success: Service '{}' started.", service_name),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!(" -> Error: Failed to start service: {}. Halting example.", e);
|
eprintln!(
|
||||||
|
" -> Error: Failed to start service: {}. Halting example.",
|
||||||
|
e
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
thread::sleep(Duration::from_secs(2));
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
|
||||||
// 3. Try to start the service again while it's already running
|
// 2. Try to start the service again while it's already running
|
||||||
println!("\n3. Trying to start the *same service* again...");
|
println!("\n2. Trying to start the *same service* again...");
|
||||||
match manager.start(service_name) {
|
match manager.start(&service_config) {
|
||||||
Ok(()) => println!(" -> Unexpected Success: Service started again."),
|
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
|
// 3. Let it run for a bit
|
||||||
@ -81,14 +80,20 @@ fn main() {
|
|||||||
println!("\n5. Trying to stop the service that was just removed...");
|
println!("\n5. Trying to stop the service that was just removed...");
|
||||||
match manager.stop(service_name) {
|
match manager.stop(service_name) {
|
||||||
Ok(()) => println!(" -> Unexpected Success: Stopped a removed service."),
|
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
|
// 6. Try to remove the service again
|
||||||
println!("\n6. Trying to remove the service again...");
|
println!("\n6. Trying to remove the service again...");
|
||||||
match manager.remove(service_name) {
|
match manager.remove(service_name) {
|
||||||
Ok(()) => println!(" -> Unexpected Success: Removed a non-existent service."),
|
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 ---");
|
println!("\n--- Spaghetti Example Finished ---");
|
||||||
|
@ -5,13 +5,7 @@ use std::time::Duration;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// 1. Create a service manager for the current platform
|
// 1. Create a service manager for the current platform
|
||||||
let manager = match create_service_manager(None) {
|
let manager = create_service_manager();
|
||||||
Ok(manager) => manager,
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error: Failed to create service manager: {}", e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 2. Define the configuration for our new service
|
// 2. Define the configuration for our new service
|
||||||
let service_name = "com.herocode.examples.simpleservice";
|
let service_name = "com.herocode.examples.simpleservice";
|
||||||
@ -32,9 +26,15 @@ fn main() {
|
|||||||
|
|
||||||
// Cleanup from previous runs, if necessary
|
// Cleanup from previous runs, if necessary
|
||||||
if let Ok(true) = manager.exists(service_name) {
|
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) {
|
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) {
|
if let Err(e) = manager.remove(service_name) {
|
||||||
eprintln!("Error: failed to remove existing service: {}", e);
|
eprintln!("Error: failed to remove existing service: {}", e);
|
||||||
@ -43,19 +43,9 @@ fn main() {
|
|||||||
println!("Cleanup complete.");
|
println!("Cleanup complete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Create the service
|
// 3. Start the service (creates and starts in one step)
|
||||||
println!("\n1. Creating service: '{}'", service_name);
|
println!("\n1. Starting service: '{}'", service_name);
|
||||||
match manager.create(&service_config) {
|
match manager.start(&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) {
|
|
||||||
Ok(()) => println!("Service '{}' started successfully.", service_name),
|
Ok(()) => println!("Service '{}' started successfully.", service_name),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Error: Failed to start service '{}': {}", service_name, e);
|
eprintln!("Error: Failed to start service '{}': {}", service_name, e);
|
||||||
@ -71,7 +61,10 @@ fn main() {
|
|||||||
println!("\n2. Checking service status...");
|
println!("\n2. Checking service status...");
|
||||||
match manager.status(service_name) {
|
match manager.status(service_name) {
|
||||||
Ok(status) => println!("Service status: {:?}", status),
|
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.");
|
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...");
|
println!("\n4. Checking status after stopping...");
|
||||||
match manager.status(service_name) {
|
match manager.status(service_name) {
|
||||||
Ok(status) => println!("Service status: {:?}", status),
|
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
|
// 6. Remove the service
|
||||||
|
Loading…
Reference in New Issue
Block a user