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"), ")");
#[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<PathBuf>,
/// Enable increasingly more verbose output
#[arg(long, short, action = clap::ArgAction::Count)]
pub verbose: u8,
#[command(subcommand)]
pub command: Command,
}

View file

@ -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(())

View file

@ -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<SqlitePool> {
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<Self> {
pub async fn new(config: &'static Config, command: ServerCommand) -> somehow::Result<Self> {
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)?),
})
}