Skip to content

Commit

Permalink
feat: added manual signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zleyyij committed Jun 6, 2024
1 parent d18dad7 commit 9157241
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
10 changes: 10 additions & 0 deletions backend/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 backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ reqwest = { version = "0.12.4", features = ["stream"] }
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
sqlx = { version = "0.7.4", features = ["sqlite", "runtime-tokio"] }
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread", "signal"] }
tower = "0.4.13"
tower-http = { version = "0.5.2", features = ["normalize-path", "fs", "cors"] }
url = "2.5.0"
32 changes: 27 additions & 5 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ use color_eyre::Result;
use db::DATABASE_URL;
use gh::GithubAccessToken;
use handlers_prelude::*;
use log::{info, LevelFilter};
use log::{error, debug, info, warn, LevelFilter};
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, TokenUrl};
use reqwest::Client;
use sqlx::SqlitePool;
use std::env::{self, current_exe};
use tokio::task;
use tokio::{
signal::unix::{signal, SignalKind},
task,
};
use tower_http::cors::CorsLayer;
use tower_http::{normalize_path::NormalizePathLayer, services::ServeDir};

Expand Down Expand Up @@ -55,12 +58,15 @@ async fn main() -> Result<()> {
// Parse command line arguments
let cli_args = Args::parse();
// Read environment variables from dotenv file
dotenvy::from_path("cms-data/.env")
.context("No .env file was found in `cms-data/`, or failed to read from it.")?;
let dotenv_path = "cms-data/.env";
// Initialize logging
env_logger::builder()
.filter(None, log::LevelFilter::Info)
.filter(None, cli_args.logging_level)
.init();
debug!("Initialized logging");
dotenvy::from_path(dotenv_path).unwrap_or_else(|_| {
warn!("Failed to read dotenv file located at {dotenv_path}, please ensure all config values are manually set");
});
if cfg!(debug_assertions) {
info!("Server running in development mode");
} else {
Expand All @@ -70,6 +76,22 @@ async fn main() -> Result<()> {
let state: AppState = init_state()
.await
.wrap_err("Failed to initialize app state")?;
debug!("Initialized app state");
// https://github.com/r-Techsupport/rts-cms/issues/27
// In docker, because the process is running with a PID of 1,
// we need to implement our own SIGINT/TERM handlers
#[cfg(target_family="unix")]
for sig in [SignalKind::interrupt(), SignalKind::terminate()] {
task::spawn(async move {
let mut listener = signal(sig)
.expect("Failed to initialize a signal handler");
listener.recv().await;
// At this point we've received SIGINT/SIGKILL and we can shut down
error!("SIGINT or SIGTERM received, terminating.");
std::process::exit(0);
});
}

// files are served relative to the location of the executable, not where the
// executable was run from
let mut frontend_dir = current_exe()?;
Expand Down

0 comments on commit 9157241

Please sign in to comment.