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 {
/// 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<PathBuf>,
}
#[derive(Debug, clap::Parser)]

View file

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

View file

@ -58,22 +58,32 @@ fn open_repo(repo_path: &Path) -> somehow::Result<ThreadSafeRepository> {
pub struct Server {
config: &'static Config,
db: SqlitePool,
repo: Arc<ThreadSafeRepository>,
repo: Option<Arc<ThreadSafeRepository>>,
}
impl Server {
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 {
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
}
}

View file

@ -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<ThreadSafeRepository>) {
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<ThreadSafeRepository>) {
loop {
recurring_task(&server).await;
recurring_task(&server, repo.clone()).await;
tokio::time::sleep(server.config.repo_update_delay).await;
}
}