From ba758b19ab6885715134867b896577c3db6c6ff3 Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 6 Nov 2018 21:52:57 -0600 Subject: [PATCH] Make example wait so it can process before getting the shutdown signal --- examples/process-jobs/src/main.rs | 100 ++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 34 deletions(-) diff --git a/examples/process-jobs/src/main.rs b/examples/process-jobs/src/main.rs index a804965..5e64d37 100644 --- a/examples/process-jobs/src/main.rs +++ b/examples/process-jobs/src/main.rs @@ -1,6 +1,8 @@ #[macro_use] extern crate serde_derive; +use std::time::Duration; + use failure::Error; use futures::{ future::{lazy, IntoFuture}, @@ -42,42 +44,72 @@ fn main() { dotenv::dotenv().ok(); env_logger::init(); - tokio::run(lazy(|| { - let mut runner = JobRunner::new(1234, 8, "example-db"); - runner.register_processor(MyProcessor); + tokio::run( + lazy(|| { + let mut runner = JobRunner::new(1234, 4, "example-db"); + runner.register_processor(MyProcessor); - let handle = runner.spawn(); + let handle = runner.spawn(); - let jobs = vec![ - MyJobArguments { - some_usize: 0, - other_usize: 1, - }, - MyJobArguments { - some_usize: 1, - other_usize: 2, - }, - MyJobArguments { - some_usize: 3, - other_usize: 5, - }, - MyJobArguments { - some_usize: 8, - other_usize: 13, - }, - ]; + let jobs = vec![ + MyJobArguments { + some_usize: 0, + other_usize: 1, + }, + MyJobArguments { + some_usize: 1, + other_usize: 2, + }, + MyJobArguments { + some_usize: 3, + other_usize: 5, + }, + MyJobArguments { + some_usize: 8, + 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 - .into_iter() - .map(|job| { - tokio::spawn( - handle - .queue(MyProcessor::new_job(job, None, None).unwrap()) - .then(|_| Ok(())), - ); - }) - .collect(); + let _: Vec<_> = jobs + .into_iter() + .map(|job| { + tokio::spawn( + handle + .queue(MyProcessor::new_job(job, None, None).unwrap()) + .then(|_| Ok(())), + ); + }) + .collect(); - Ok(()) - })); + Ok(handle) + }) + .and_then(|handle| { + tokio::timer::Delay::new(tokio::clock::now() + Duration::from_secs(2)) + .map(move |_| { + let _ = handle; + () + }) + .map_err(|_| ()) + }), + ); }