75 lines
1.7 KiB
Rust
75 lines
1.7 KiB
Rust
use std::time::Duration;
|
|
|
|
use comrade::service::ServiceManager;
|
|
|
|
fn main() {
|
|
env_logger::init();
|
|
|
|
log::info!("Running services example");
|
|
|
|
// persistent background services
|
|
let mut s_decay = ServiceManager::new().mode(comrade::service::ServiceMode::Decay);
|
|
|
|
s_decay = s_decay.register(
|
|
"myservice",
|
|
Box::new(|_| {
|
|
let mut c = 0;
|
|
loop {
|
|
// ...
|
|
println!("I am doing something!");
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
c += 1;
|
|
if c == 3 {
|
|
panic!("Oh no!");
|
|
}
|
|
}
|
|
}),
|
|
);
|
|
|
|
let s = s_decay.register(
|
|
"myservice2",
|
|
Box::new(|_| {
|
|
let mut c = 0;
|
|
loop {
|
|
// ...
|
|
println!("I am doing something! 2");
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
c += 1;
|
|
if c == 3 {
|
|
println!("Bye");
|
|
break;
|
|
}
|
|
}
|
|
}),
|
|
);
|
|
|
|
let st = s.spawn();
|
|
|
|
st.join().unwrap();
|
|
|
|
println!("Ended decaying ServiceManager");
|
|
|
|
// daemon mode
|
|
|
|
let mut s = ServiceManager::new().mode(comrade::service::ServiceMode::Daemon);
|
|
|
|
s = s.register(
|
|
"myservice",
|
|
Box::new(|_| {
|
|
let mut c = 0;
|
|
loop {
|
|
// ...
|
|
println!("I am doing something forever!");
|
|
std::thread::sleep(Duration::from_millis(400));
|
|
c += 1;
|
|
if c == 3 {
|
|
panic!("Oh no!");
|
|
}
|
|
}
|
|
}),
|
|
);
|
|
|
|
let st = s.spawn();
|
|
|
|
st.join().unwrap();
|
|
}
|