Make example wait so it can process before getting the shutdown signal

This commit is contained in:
asonix 2018-11-06 21:52:57 -06:00
parent a142eb2d3f
commit ba758b19ab

View file

@ -1,6 +1,8 @@
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
use std::time::Duration;
use failure::Error; use failure::Error;
use futures::{ use futures::{
future::{lazy, IntoFuture}, future::{lazy, IntoFuture},
@ -42,42 +44,72 @@ fn main() {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
env_logger::init(); env_logger::init();
tokio::run(lazy(|| { tokio::run(
let mut runner = JobRunner::new(1234, 8, "example-db"); lazy(|| {
runner.register_processor(MyProcessor); let mut runner = JobRunner::new(1234, 4, "example-db");
runner.register_processor(MyProcessor);
let handle = runner.spawn(); let handle = runner.spawn();
let jobs = vec![ let jobs = vec![
MyJobArguments { MyJobArguments {
some_usize: 0, some_usize: 0,
other_usize: 1, other_usize: 1,
}, },
MyJobArguments { MyJobArguments {
some_usize: 1, some_usize: 1,
other_usize: 2, other_usize: 2,
}, },
MyJobArguments { MyJobArguments {
some_usize: 3, some_usize: 3,
other_usize: 5, other_usize: 5,
}, },
MyJobArguments { MyJobArguments {
some_usize: 8, some_usize: 8,
other_usize: 13, other_usize: 13,
}, },
]; MyJobArguments {
some_usize: 21,
other_usize: 34,
},
MyJobArguments {
some_usize: 55,
other_usize: 89,
},
MyJobArguments {
some_usize: 144,
other_usize: 233,
},
MyJobArguments {
some_usize: 377,
other_usize: 610,
},
MyJobArguments {
some_usize: 987,
other_usize: 1597,
},
];
let _: Vec<_> = jobs let _: Vec<_> = jobs
.into_iter() .into_iter()
.map(|job| { .map(|job| {
tokio::spawn( tokio::spawn(
handle handle
.queue(MyProcessor::new_job(job, None, None).unwrap()) .queue(MyProcessor::new_job(job, None, None).unwrap())
.then(|_| Ok(())), .then(|_| Ok(())),
); );
}) })
.collect(); .collect();
Ok(()) Ok(handle)
})); })
.and_then(|handle| {
tokio::timer::Delay::new(tokio::clock::now() + Duration::from_secs(2))
.map(move |_| {
let _ = handle;
()
})
.map_err(|_| ())
}),
);
} }