Skip to content

Commit

Permalink
feat: Testing gracefully shutting down hyper server
Browse files Browse the repository at this point in the history
  • Loading branch information
sousandrei committed Sep 17, 2022
1 parent 1948d15 commit 23a4c37
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use hyper::Server;
use serde::{Deserialize, Serialize};
use std::env;
use std::sync::Arc;
use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::Mutex;
use tracing::info;

Expand Down Expand Up @@ -98,7 +99,31 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

info!("Listening on http://{}", addr);

server.await?;
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
let graceful = server.with_graceful_shutdown(async {
rx.await.ok();
});

tokio::task::spawn(async move {
let kind = SignalKind::interrupt();
let mut stream = signal(kind).expect("error opening signal stream");

loop {
stream.recv().await;
info!("Termination initiated");
break;
}
tx.send(()).expect("error sending shutdown signal");
});

graceful.await?;

//TODO: function to clear state
// let vms = state.vms.lock().unwrap();
// for a in vms.iter() {
// println!("{}", a.name);
// vm::terminate(&a.name).await?;
// }

Ok(())
}

0 comments on commit 23a4c37

Please sign in to comment.