Start server via subcommand

This commit is contained in:
Joscha 2023-08-07 14:49:09 +02:00
parent 9bdfc79c8b
commit 6815c36f5b
3 changed files with 40 additions and 29 deletions

View file

@ -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"), ")"); pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")");
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]
#[command(name = NAME, version = VERSION)] pub struct ServerCommand {
pub struct Args {
/// Path to the repo's tablejohn database. /// Path to the repo's tablejohn database.
pub db: PathBuf, pub db: PathBuf,
/// Path to the git repo. /// Path to the git repo.
pub repo: PathBuf, 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. /// Path to the config file.
#[arg(long, short)] #[arg(long, short)]
pub config: Option<PathBuf>, pub config: Option<PathBuf>,
/// Enable increasingly more verbose output /// Enable increasingly more verbose output
#[arg(long, short, action = clap::ArgAction::Count)] #[arg(long, short, action = clap::ArgAction::Count)]
pub verbose: u8, pub verbose: u8,
#[command(subcommand)]
pub command: Command,
} }

View file

@ -15,7 +15,7 @@ use tracing_subscriber::{
}; };
use crate::{ use crate::{
args::{Args, NAME, VERSION}, args::{Args, Command, NAME, VERSION},
config::Config, config::Config,
server::Server, server::Server,
}; };
@ -98,26 +98,30 @@ async fn run() -> somehow::Result<()> {
info!("You are running {NAME} {VERSION}"); info!("You are running {NAME} {VERSION}");
let config = load_config(args.config)?; let config = load_config(args.config)?;
let server = Server::new(config, &args.db, &args.repo).await?;
info!("Startup complete, running"); match args.command {
select! { Command::Server(command) => {
_ = wait_for_signal() => {} let server = Server::new(config, command).await?;
_ = server.run() => {}
}
select! { select! {
_ = die_on_signal() => {} _ = wait_for_signal() => {}
// For some reason, the thread pool shutting down seems to block _ = server.run() => {}
// 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 select! {
// doesn't block the signals. _ = die_on_signal() => {}
// // For some reason, the thread pool shutting down seems to block
// In order to fix this, I could maybe register a bare signal handler // receiving further signals if a heavy sql operation is currently
// (instead of using tokio streams) that just calls process::exit(1) and // running. Maybe this is due to the thread pool not deferring blocking
// nothing else? // work to a separate thread? In any case, replacing it with a sleep
_ = server.shut_down() => {} // 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(()) Ok(())

View file

@ -12,7 +12,7 @@ use sqlx::{
use tokio::select; use tokio::select;
use tracing::{debug, info}; 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<SqlitePool> { async fn open_db(db_path: &Path) -> sqlx::Result<SqlitePool> {
let options = SqliteConnectOptions::new() let options = SqliteConnectOptions::new()
@ -61,15 +61,11 @@ pub struct Server {
} }
impl Server { impl Server {
pub async fn new( pub async fn new(config: &'static Config, command: ServerCommand) -> somehow::Result<Self> {
config: &'static Config,
db_path: &Path,
repo_path: &Path,
) -> somehow::Result<Self> {
Ok(Self { Ok(Self {
config, config,
db: open_db(db_path).await?, db: open_db(&command.db).await?,
repo: Arc::new(open_repo(repo_path)?), repo: Arc::new(open_repo(&command.repo)?),
}) })
} }