Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add graceful shutdown signal handling #53

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ emulator-ui = { path = "crates/emulator-ui" }
hybrid-axum-tonic = { path = "crates/hybrid-axum-tonic" }
tikv-jemallocator = "0.5.4"
time = { workspace = true, optional = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal"] }
tracing = { workspace = true }
tracing-subscriber = { version = "0.3.18", optional = true }

Expand Down
28 changes: 20 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
use std::net::SocketAddr;
use std::{future::Future, net::SocketAddr};

use axum::Router;
use hybrid_axum_tonic::{NestTonic, RestGrpcService};
use tracing::info;
use tracing::{enabled, info, Level};

#[tokio::main]
pub async fn run(host_port: SocketAddr) -> color_eyre::Result<()> {
pub async fn run(
host_port: SocketAddr,
shutdown: impl Future<Output = ()>,
) -> color_eyre::Result<()> {
let rest_router = emulator_ui::router();
let grpc_router = Router::new().nest_tonic(emulator_grpc::service());
let combined = RestGrpcService::new(rest_router, grpc_router).into_make_service();
let server = axum::Server::bind(&host_port).serve(combined);
let server = axum::Server::bind(&host_port)
.serve(combined)
.with_graceful_shutdown(shutdown);

info!("Firestore listening on {}", host_port);

#[cfg(not(feature = "tracing"))]
eprintln!("Firestore listening on {}", host_port);
if enabled!(Level::INFO) {
info!("Firestore emulator listening on {}", host_port);
} else {
eprintln!("Firestore emulator listening on {}", host_port);
}

server.await?;

if enabled!(Level::INFO) {
info!("Firestore emulator stopped");
} else {
eprintln!("Firestore emulator stopped");
}

Ok(())
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::net::SocketAddr;
use clap::Parser;
use firestore_emulator::run;
use tikv_jemallocator::Jemalloc;
use tokio::signal::ctrl_c;

#[global_allocator]
static GLOBAL_ALLOC: Jemalloc = Jemalloc;
Expand Down Expand Up @@ -44,5 +45,7 @@ fn main() -> color_eyre::Result<()> {
registry.init();
}

run(Args::parse().host_port)
let ctrl_c_listener = async { ctrl_c().await.expect("failed to listen for ctrl-c event") };

run(Args::parse().host_port, ctrl_c_listener)
}