✨ worker
This commit is contained in:
parent
5cb4facc48
commit
e3393f1e09
11 changed files with 841 additions and 48 deletions
|
@ -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();
|
||||
|
||||
|
|
50
examples/work.rs
Normal file
50
examples/work.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use comrade::{
|
||||
job::{JobDispatcher, JobOrder},
|
||||
service::ServiceManager,
|
||||
worker,
|
||||
};
|
||||
use crossbeam::channel::Receiver;
|
||||
|
||||
#[worker]
|
||||
pub fn myfn(i: i32) -> i32 {
|
||||
i * 2
|
||||
}
|
||||
|
||||
#[worker]
|
||||
pub fn multiply(a: i32, b: i32) -> i32 {
|
||||
a * b
|
||||
}
|
||||
|
||||
fn do_work(multiply: multiply_Scoped, myfn: myfn_Scoped) {
|
||||
for i in 0..10 {
|
||||
let x = multiply.call(i, i);
|
||||
println!("myfn {i} -> {x}");
|
||||
let x = myfn.call(i);
|
||||
println!("myfn {i} -> {x}");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let s = ServiceManager::new().mode(comrade::service::ServiceMode::Decay);
|
||||
|
||||
let (s, multiply) = multiply_init_scoped(s);
|
||||
let (s, myfn_fn) = myfn_init_scoped(s);
|
||||
let s = s.spawn();
|
||||
|
||||
do_work(multiply, myfn_fn);
|
||||
|
||||
s.join().unwrap();
|
||||
|
||||
let s = ServiceManager::new().mode(comrade::service::ServiceMode::Decay);
|
||||
let s = myfn_init(s);
|
||||
let s = s.spawn();
|
||||
|
||||
let x = myfn(55);
|
||||
println!("myfn {x}");
|
||||
|
||||
myfn_shutdown();
|
||||
|
||||
s.join().unwrap();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue