diff --git a/src/args.rs b/src/args.rs index 15988b5..6df348e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -7,14 +7,20 @@ pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_ pub struct ServerCommand { /// Path to the repo's tablejohn database. pub db: PathBuf, + /// Path to the git repo. pub repo: Option, + + /// Path to the bench repo. + #[arg(long, short)] + pub bench_repo: Option, } #[derive(Debug, clap::Parser)] pub enum Command { Server(ServerCommand), Runner, + // TODO bench script command? } #[derive(Debug, clap::Parser)] @@ -23,9 +29,11 @@ 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 7cf839a..a101cc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,8 @@ mod args; mod config; mod runner; mod server; -mod somehow; mod shared; +mod somehow; use std::{io, process}; diff --git a/src/server.rs b/src/server.rs index 2e70275..ae0553d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -49,22 +49,34 @@ async fn open_db(db_path: &Path) -> sqlx::Result { Ok(pool) } -fn open_repo(repo_path: &Path) -> somehow::Result { - info!(path = %repo_path.display(), "Opening repo"); - Ok(ThreadSafeRepository::open(repo_path)?) -} +#[derive(Clone)] +pub(self) struct Repo(Arc); + +#[derive(Clone)] +pub(self) struct BenchRepo(Arc); #[derive(Clone, FromRef)] pub struct Server { config: &'static Config, db: SqlitePool, - repo: Option>, + repo: Option, + bench_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)?)) + let repo = if let Some(path) = command.repo.as_ref() { + info!(path = %path.display(), "Opening repo"); + let repo = ThreadSafeRepository::open(path)?; + Some(Repo(Arc::new(repo))) + } else { + None + }; + + let bench_repo = if let Some(path) = command.bench_repo.as_ref() { + info!(path = %path.display(), "Opening repo"); + let repo = ThreadSafeRepository::open(path)?; + Some(BenchRepo(Arc::new(repo))) } else { None }; @@ -73,6 +85,7 @@ impl Server { config, db: open_db(&command.db).await?, repo, + bench_repo, }) } @@ -80,7 +93,7 @@ impl Server { if let Some(repo) = self.repo.clone() { select! { e = web::run(self.clone()) => e, - () = recurring::run(self.clone(), repo) => Ok(()), + () = recurring::run(self.clone(), repo, self.bench_repo.clone()) => Ok(()), } } else { web::run(self.clone()).await diff --git a/src/server/recurring.rs b/src/server/recurring.rs index 00213f3..567fde8 100644 --- a/src/server/recurring.rs +++ b/src/server/recurring.rs @@ -6,14 +6,11 @@ mod queue; mod repo; -use std::sync::Arc; - -use gix::ThreadSafeRepository; use tracing::{debug_span, error, Instrument}; -use super::Server; +use super::{BenchRepo, Repo, Server}; -async fn recurring_task(state: &Server, repo: Arc) { +async fn recurring_task(state: &Server, repo: Repo) { async { if let Err(e) = repo::update(&state.db, repo).await { error!("Error updating repo:\n{e:?}"); @@ -31,7 +28,7 @@ async fn recurring_task(state: &Server, repo: Arc) { .await; } -pub async fn run(server: Server, repo: Arc) { +pub(super) async fn run(server: Server, repo: Repo, bench_repo: Option) { loop { recurring_task(&server, repo.clone()).await; tokio::time::sleep(server.config.repo_update_delay).await; diff --git a/src/server/recurring/repo.rs b/src/server/recurring/repo.rs index 03fea48..f3bd8eb 100644 --- a/src/server/recurring/repo.rs +++ b/src/server/recurring/repo.rs @@ -1,15 +1,16 @@ //! Add new commits to the database and update the tracked refs. -use std::{collections::HashSet, sync::Arc}; +use std::collections::HashSet; use futures::TryStreamExt; -use gix::{ - objs::Kind, prelude::ObjectIdExt, refs::Reference, ObjectId, Repository, ThreadSafeRepository, -}; +use gix::{objs::Kind, prelude::ObjectIdExt, refs::Reference, ObjectId, Repository}; use sqlx::{Acquire, SqliteConnection, SqlitePool}; use tracing::{debug, info}; -use crate::{server::util, somehow}; +use crate::{ + server::{util, Repo}, + somehow, +}; async fn get_all_commit_hashes_from_db( conn: &mut SqliteConnection, @@ -227,9 +228,9 @@ async fn update_commit_tracked_status(conn: &mut SqliteConnection) -> somehow::R Ok(()) } -pub async fn update(db: &SqlitePool, repo: Arc) -> somehow::Result<()> { +pub(super) async fn update(db: &SqlitePool, repo: Repo) -> somehow::Result<()> { debug!("Updating repo"); - let thread_local_repo = repo.to_thread_local(); + let thread_local_repo = repo.0.to_thread_local(); let mut tx = db.begin().await?; let conn = tx.acquire().await?; @@ -244,7 +245,7 @@ pub async fn update(db: &SqlitePool, repo: Arc) -> somehow // This can take a while for larger repos. Running it via spawn_blocking // keeps it from blocking the entire tokio worker. let (refs, new) = tokio::task::spawn_blocking(move || { - get_all_refs_and_new_commits_from_repo(&repo.to_thread_local(), &old) + get_all_refs_and_new_commits_from_repo(&repo.0.to_thread_local(), &old) }) .await??; debug!("Found {} new commits in repo", new.len());