worker

This commit is contained in:
JMARyA 2025-03-07 20:04:58 +01:00
parent 5cb4facc48
commit e3393f1e09
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
11 changed files with 841 additions and 48 deletions

View file

@ -8,20 +8,66 @@ fn main() {
log::info!("Running services example");
// persistent background services
let mut s = ServiceManager::new();
let mut s_decay = ServiceManager::new().mode(comrade::service::ServiceMode::Decay);
s.register("myservice", |_| {
let mut c = 0;
loop {
// ...
println!("I am doing something!");
std::thread::sleep(Duration::from_secs(1));
c += 1;
if c == 3 {
panic!("Oh no!");
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();