Make repo optional

This commit is contained in:
Joscha 2023-08-09 14:13:17 +02:00
parent a261bfdd26
commit 04212dff1c
4 changed files with 28 additions and 13 deletions

View file

@ -7,9 +7,8 @@ pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_
pub struct ServerCommand { pub struct ServerCommand {
/// Path to the repo's tablejohn database. /// Path to the repo's tablejohn database.
pub db: PathBuf, pub db: PathBuf,
// TODO Make repo optional
/// Path to the git repo. /// Path to the git repo.
pub repo: PathBuf, pub repo: Option<PathBuf>,
} }
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]

View file

@ -102,8 +102,11 @@ impl ConfigFile {
return Ok(name.clone()); return Ok(name.clone());
} }
if let Command::Server(ServerCommand { repo, .. }) = &args.command { if let Command::Server(ServerCommand {
if let Some(name) = repo.canonicalize()?.file_name() { repo: Some(path), ..
}) = &args.command
{
if let Some(name) = path.canonicalize()?.file_name() {
let name = name.to_string_lossy(); let name = name.to_string_lossy();
let name = name.strip_suffix(".git").unwrap_or(&name).to_string(); let name = name.strip_suffix(".git").unwrap_or(&name).to_string();
return Ok(name); return Ok(name);

View file

@ -58,22 +58,32 @@ fn open_repo(repo_path: &Path) -> somehow::Result<ThreadSafeRepository> {
pub struct Server { pub struct Server {
config: &'static Config, config: &'static Config,
db: SqlitePool, db: SqlitePool,
repo: Arc<ThreadSafeRepository>, repo: Option<Arc<ThreadSafeRepository>>,
} }
impl Server { impl Server {
pub async fn new(config: &'static Config, command: ServerCommand) -> somehow::Result<Self> { pub async fn new(config: &'static Config, command: ServerCommand) -> somehow::Result<Self> {
let repo = if let Some(repo) = command.repo.as_ref() {
Some(Arc::new(open_repo(repo)?))
} else {
None
};
Ok(Self { Ok(Self {
config, config,
db: open_db(&command.db).await?, db: open_db(&command.db).await?,
repo: Arc::new(open_repo(&command.repo)?), repo,
}) })
} }
pub async fn run(&self) -> somehow::Result<()> { pub async fn run(&self) -> somehow::Result<()> {
select! { if let Some(repo) = self.repo.clone() {
e = web::run(self.clone()) => e, select! {
() = recurring::run(self.clone()) => Ok(()), e = web::run(self.clone()) => e,
() = recurring::run(self.clone(), repo) => Ok(()),
}
} else {
web::run(self.clone()).await
} }
} }

View file

@ -6,13 +6,16 @@
mod queue; mod queue;
mod repo; mod repo;
use std::sync::Arc;
use gix::ThreadSafeRepository;
use tracing::{debug_span, error, Instrument}; use tracing::{debug_span, error, Instrument};
use super::Server; use super::Server;
async fn recurring_task(state: &Server) { async fn recurring_task(state: &Server, repo: Arc<ThreadSafeRepository>) {
async { 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:?}"); error!("Error updating repo:\n{e:?}");
}; };
} }
@ -28,9 +31,9 @@ async fn recurring_task(state: &Server) {
.await; .await;
} }
pub async fn run(server: Server) { pub async fn run(server: Server, repo: Arc<ThreadSafeRepository>) {
loop { loop {
recurring_task(&server).await; recurring_task(&server, repo.clone()).await;
tokio::time::sleep(server.config.repo_update_delay).await; tokio::time::sleep(server.config.repo_update_delay).await;
} }
} }