Skip to content

Commit 1b3f5ff

Browse files
committed
Add graceful shutdown and dynamic healthcheck port
1 parent 0a6f049 commit 1b3f5ff

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

Dockerfile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,19 @@ ENV STATIC_FILES_DIR=/app/static
7070
ENV RUST_LOG=info,dynamight=debug
7171
ENV DYNAMIGHT_CONFIG=/app/config/dynamight.toml
7272

73-
# Expose port
73+
# Expose default port
7474
EXPOSE 8080
7575

76-
# Health check
76+
# Health check script (reads port from config file, falls back to 8080)
77+
COPY <<'EOF' /app/healthcheck.sh
78+
#!/bin/sh
79+
PORT=$(grep -E '^[[:space:]]*port[[:space:]]*=' /app/config/dynamight.toml 2>/dev/null | head -1 | tr -dc '0-9')
80+
wget -qO /dev/null "http://127.0.0.1:${PORT:-8080}/api/system/health"
81+
EOF
82+
RUN chmod +x /app/healthcheck.sh
83+
7784
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s \
78-
CMD wget -q --spider http://localhost:8080/api/system/health || exit 1
85+
CMD /app/healthcheck.sh
7986

8087
# Run as root (required for mount operations)
8188
ENTRYPOINT ["./dynamight"]

backend/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,29 @@ async fn main() -> anyhow::Result<()> {
451451
listener,
452452
app.into_make_service_with_connect_info::<SocketAddr>(),
453453
)
454+
.with_graceful_shutdown(shutdown_signal())
454455
.await?;
455456

457+
tracing::info!("Server shut down gracefully");
456458
Ok(())
457459
}
460+
461+
async fn shutdown_signal() {
462+
let ctrl_c = async {
463+
tokio::signal::ctrl_c()
464+
.await
465+
.expect("failed to install Ctrl+C handler");
466+
};
467+
468+
let terminate = async {
469+
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
470+
.expect("failed to install SIGTERM handler")
471+
.recv()
472+
.await;
473+
};
474+
475+
tokio::select! {
476+
_ = ctrl_c => tracing::info!("Received Ctrl+C, shutting down..."),
477+
_ = terminate => tracing::info!("Received SIGTERM, shutting down..."),
478+
}
479+
}

0 commit comments

Comments
 (0)