From 04212dff1c92dfeb427e3c7442da36eed90d8065 Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 9 Aug 2023 14:13:17 +0200 Subject: [PATCH] Make repo optional --- src/args.rs | 3 +-- src/config.rs | 7 +++++-- src/server.rs | 20 +++++++++++++++----- src/server/recurring.rs | 11 +++++++---- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/args.rs b/src/args.rs index e1b0b4a..15988b5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -7,9 +7,8 @@ pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_ pub struct ServerCommand { /// Path to the repo's tablejohn database. pub db: PathBuf, - // TODO Make repo optional /// Path to the git repo. - pub repo: PathBuf, + pub repo: Option, } #[derive(Debug, clap::Parser)] diff --git a/src/config.rs b/src/config.rs index b86baae..5b6347b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -102,8 +102,11 @@ impl ConfigFile { return Ok(name.clone()); } - if let Command::Server(ServerCommand { repo, .. }) = &args.command { - if let Some(name) = repo.canonicalize()?.file_name() { + if let Command::Server(ServerCommand { + repo: Some(path), .. + }) = &args.command + { + if let Some(name) = path.canonicalize()?.file_name() { let name = name.to_string_lossy(); let name = name.strip_suffix(".git").unwrap_or(&name).to_string(); return Ok(name); diff --git a/src/server.rs b/src/server.rs index ca4c7c7..2e70275 100644 --- a/src/server.rs +++ b/src/server.rs @@ -58,22 +58,32 @@ fn open_repo(repo_path: &Path) -> somehow::Result { pub struct Server { config: &'static Config, db: SqlitePool, - repo: Arc, + repo: Option>, } impl Server { pub async fn new(config: &'static Config, command: ServerCommand) -> somehow::Result { + let repo = if let Some(repo) = command.repo.as_ref() { + Some(Arc::new(open_repo(repo)?)) + } else { + None + }; + Ok(Self { config, db: open_db(&command.db).await?, - repo: Arc::new(open_repo(&command.repo)?), + repo, }) } pub async fn run(&self) -> somehow::Result<()> { - select! { - e = web::run(self.clone()) => e, - () = recurring::run(self.clone()) => Ok(()), + if let Some(repo) = self.repo.clone() { + select! { + e = web::run(self.clone()) => e, + () = recurring::run(self.clone(), repo) => Ok(()), + } + } else { + web::run(self.clone()).await } } diff --git a/src/server/recurring.rs b/src/server/recurring.rs index 62252a7..00213f3 100644 --- a/src/server/recurring.rs +++ b/src/server/recurring.rs @@ -6,13 +6,16 @@ mod queue; mod repo; +use std::sync::Arc; + +use gix::ThreadSafeRepository; use tracing::{debug_span, error, Instrument}; use super::Server; -async fn recurring_task(state: &Server) { +async fn recurring_task(state: &Server, repo: Arc) { async { - if let Err(e) = repo::update(&state.db, state.repo.clone()).await { + if let Err(e) = repo::update(&state.db, repo).await { error!("Error updating repo:\n{e:?}"); }; } @@ -28,9 +31,9 @@ async fn recurring_task(state: &Server) { .await; } -pub async fn run(server: Server) { +pub async fn run(server: Server, repo: Arc) { loop { - recurring_task(&server).await; + recurring_task(&server, repo.clone()).await; tokio::time::sleep(server.config.repo_update_delay).await; } }