Skip to content

Commit

Permalink
Add example for graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
silvestrpredko committed Jun 13, 2024
1 parent ace7535 commit 8f49e14
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rumqttd/examples/graceful_shutdown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use rumqttd::{Broker, Config};
use tracing::Level;

use core::time::Duration;
use std::thread;

fn main() {
let builder = tracing_subscriber::fmt()
.pretty()
.with_max_level(Level::DEBUG)
.with_line_number(false)
.with_file(false)
.with_thread_ids(false)
.with_thread_names(false);

builder
.try_init()
.expect("initialized subscriber succesfully");

// As examples are compiled as seperate binary so this config is current path dependent. Run it
// from root of this crate
let config = config::Config::builder()
.add_source(config::File::with_name("rumqttd.toml"))
.build()
.unwrap();

let config: Config = config.try_deserialize().unwrap();

dbg!(&config);

let mut broker = Broker::new(config);

let handler = broker.shutdown_handler();

thread::spawn(move || {
broker.start().unwrap();
println!("Shutdown handler called");
});

thread::sleep(Duration::from_secs(1));
handler.shutdown_broker();
thread::sleep(Duration::from_secs(1));
}

0 comments on commit 8f49e14

Please sign in to comment.