From 402b8e3a5758fa14e98031c2eb29165c7679a243 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 10 Jul 2024 20:56:48 +0200 Subject: [PATCH] shut down on ctrl+c --- Cargo.lock | 10 ++++++++++ Containerfile | 8 ++++---- app/Cargo.toml | 2 +- app/src/lib.rs | 14 +++++++++++--- app/src/main.rs | 3 ++- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ddae3fc..7a2f4ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2684,6 +2684,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "2.2.0" @@ -3164,6 +3173,7 @@ dependencies = [ "mio", "num_cpus", "pin-project-lite", + "signal-hook-registry", "socket2 0.5.7", "tokio-macros", "windows-sys 0.48.0", diff --git a/Containerfile b/Containerfile index 8e9d9f8..36f58d1 100644 --- a/Containerfile +++ b/Containerfile @@ -1,6 +1,6 @@ FROM docker.io/rust:1-alpine3.20 AS builder -RUN mkdir /tmp/tmp +RUN mkdir /db RUN echo "hesinde:x:2222:2222:Linux User,,,:/:/app" > /passwd RUN apk --no-cache add \ @@ -20,10 +20,10 @@ FROM scratch COPY --from=builder /passwd /etc/passwd COPY --from=builder /app /app -COPY --from=builder --chown=2222: /tmp/tmp /tmp +COPY --from=builder --chown=2222: /db /db USER hesinde -CMD ["/app", "--address", "[::]:3000"] - +ENTRYPOINT ["/app", "--address", "[::]:3000"] EXPOSE 3000 +VOLUME ["/db"] diff --git a/app/Cargo.toml b/app/Cargo.toml index b35778d..5feec0b 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -17,7 +17,7 @@ sea-orm = { workspace = true, features = ["with-time", "sqlx-sqlite", "sqlx-post serde = { workspace = true, features = ["derive"] } thiserror = "1.0.61" time = { workspace = true, features = ["macros", "serde", "formatting", "parsing" ] } -tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread", "signal"] } tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } uuid = { version = "1.10.0", features = ["v4", "fast-rng"] } diff --git a/app/src/lib.rs b/app/src/lib.rs index 712fe36..a946bc2 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -12,6 +12,8 @@ use poem::{ }; use poem_openapi::OpenApiService; use std::sync::Arc; +use tokio::signal; +use tracing::info; pub mod api; pub mod app_state; @@ -43,7 +45,13 @@ pub async fn run(args: &Config, db_url: &str) -> Result<()> { .data(app_state) .with(Tracing); - Ok(poem::Server::new(TcpListener::bind(&args.address)) - .run(app) - .await?) + let server = poem::Server::new(TcpListener::bind(&args.address)).run(app); + + tokio::select! { + _ = server => {}, + _ = signal::ctrl_c() => { + info!("Received Ctrl+C, shutting down..."); + }, + } + Ok(()) } diff --git a/app/src/main.rs b/app/src/main.rs index f4fd279..320ad7a 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -15,8 +15,9 @@ async fn main() -> Result<()> { tracing_subscriber::fmt::init(); let args = Config::parse(); - println!("{}", args.allow_registration); + println!("{}", args.db_connection); let db_url = read_db_url(&args.db_connection)?; + println!("{db_url}"); hesinde_sync::run(&args, &db_url).await }