From 6815c36f5bc8808247b408837ea5b0c8ef23b59d Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 7 Aug 2023 14:49:09 +0200 Subject: [PATCH] Start server via subcommand --- src/args.rs | 15 +++++++++++++-- src/main.rs | 42 +++++++++++++++++++++++------------------- src/server.rs | 12 ++++-------- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/args.rs b/src/args.rs index ebeefd4..ad261bf 100644 --- a/src/args.rs +++ b/src/args.rs @@ -4,16 +4,27 @@ pub const NAME: &str = env!("CARGO_PKG_NAME"); pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")"); #[derive(Debug, clap::Parser)] -#[command(name = NAME, version = VERSION)] -pub struct Args { +pub struct ServerCommand { /// Path to the repo's tablejohn database. pub db: PathBuf, /// Path to the git repo. pub repo: PathBuf, +} + +#[derive(Debug, clap::Parser)] +pub enum Command { + Server(ServerCommand), +} + +#[derive(Debug, clap::Parser)] +#[command(name = NAME, version = VERSION)] +pub struct Args { /// Path to the config file. #[arg(long, short)] pub config: Option, /// Enable increasingly more verbose output #[arg(long, short, action = clap::ArgAction::Count)] pub verbose: u8, + #[command(subcommand)] + pub command: Command, } diff --git a/src/main.rs b/src/main.rs index 0fe1fbd..d05487f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use tracing_subscriber::{ }; use crate::{ - args::{Args, NAME, VERSION}, + args::{Args, Command, NAME, VERSION}, config::Config, server::Server, }; @@ -98,26 +98,30 @@ async fn run() -> somehow::Result<()> { info!("You are running {NAME} {VERSION}"); let config = load_config(args.config)?; - let server = Server::new(config, &args.db, &args.repo).await?; - info!("Startup complete, running"); - select! { - _ = wait_for_signal() => {} - _ = server.run() => {} - } + match args.command { + Command::Server(command) => { + let server = Server::new(config, command).await?; - select! { - _ = die_on_signal() => {} - // For some reason, the thread pool shutting down seems to block - // receiving further signals if a heavy sql operation is currently - // running. Maybe this is due to the thread pool not deferring blocking - // work to a separate thread? In any case, replacing it with a sleep - // doesn't block the signals. - // - // In order to fix this, I could maybe register a bare signal handler - // (instead of using tokio streams) that just calls process::exit(1) and - // nothing else? - _ = server.shut_down() => {} + select! { + _ = wait_for_signal() => {} + _ = server.run() => {} + } + + select! { + _ = die_on_signal() => {} + // For some reason, the thread pool shutting down seems to block + // receiving further signals if a heavy sql operation is currently + // running. Maybe this is due to the thread pool not deferring blocking + // work to a separate thread? In any case, replacing it with a sleep + // doesn't block the signals. + // + // In order to fix this, I could maybe register a bare signal handler + // (instead of using tokio streams) that just calls process::exit(1) and + // nothing else? + _ = server.shut_down() => {} + } + } } Ok(()) diff --git a/src/server.rs b/src/server.rs index 8bb9f45..a9c78c5 100644 --- a/src/server.rs +++ b/src/server.rs @@ -12,7 +12,7 @@ use sqlx::{ use tokio::select; use tracing::{debug, info}; -use crate::{config::Config, somehow}; +use crate::{args::ServerCommand, config::Config, somehow}; async fn open_db(db_path: &Path) -> sqlx::Result { let options = SqliteConnectOptions::new() @@ -61,15 +61,11 @@ pub struct Server { } impl Server { - pub async fn new( - config: &'static Config, - db_path: &Path, - repo_path: &Path, - ) -> somehow::Result { + pub async fn new(config: &'static Config, command: ServerCommand) -> somehow::Result { Ok(Self { config, - db: open_db(db_path).await?, - repo: Arc::new(open_repo(repo_path)?), + db: open_db(&command.db).await?, + repo: Arc::new(open_repo(&command.repo)?), }) }