From 980e84b0f6e91833436e4f4ae11900cefc9883dd Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 4 Aug 2023 20:05:38 +0200 Subject: [PATCH] Update repo repeatedly --- Cargo.lock | 34 +++++++++++++++++----------------- Cargo.toml | 7 ++++++- src/config.rs | 34 ++++++++++++++++++++++++++++------ src/main.rs | 2 ++ src/update.rs | 19 +++++++++++++++++++ 5 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 src/update.rs diff --git a/Cargo.lock b/Cargo.lock index 802a055..6a4ee71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,12 +328,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" -[[package]] -name = "bytesize" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38fcc2979eff34a4b84e1cf9a1e3da42a7d44b3b690a40cdcb23e3d556cfb2e5" - [[package]] name = "cc" version = "1.0.81" @@ -1029,7 +1023,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "882695cccf38da4c3cc7ee687bdb412cf25e37932d7f8f2c306112ea712449f1" dependencies = [ "bytes", - "bytesize", "crc32fast", "crossbeam-channel", "flate2", @@ -1585,12 +1578,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" -[[package]] -name = "human_format" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86cce260d758a9aa3d7c4b99d55c815a540f8a37514ba6046ab6be402a157cb0" - [[package]] name = "humansize" version = "2.1.3" @@ -1600,6 +1587,22 @@ dependencies = [ "libm", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "humantime-serde" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a3db5ea5923d99402c94e9feb261dc5ee9b4efa158b0315f788cf549cc200c" +dependencies = [ + "humantime", + "serde", +] + [[package]] name = "hyper" version = "0.14.27" @@ -2080,10 +2083,6 @@ name = "prodash" version = "25.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c236e70b7f9b9ea00d33c69f63ec1ae6e9ae96118923cd37bd4e9c7396f0b107" -dependencies = [ - "bytesize", - "human_format", -] [[package]] name = "quote" @@ -2733,6 +2732,7 @@ dependencies = [ "clap", "directories", "gix", + "humantime-serde", "mime_guess", "rust-embed", "serde", diff --git a/Cargo.toml b/Cargo.toml index 262be5e..5f1efdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ askama_axum = "0.3.0" axum = { version = "0.6.19", features = ["macros"] } clap = { version = "4.3.19", features = ["derive", "deprecated"] } directories = "5.0.1" -gix = "0.51.0" +humantime-serde = "1.1.1" mime_guess = "2.0.4" rust-embed = "6.8.1" serde = { version = "1.0.181", features = ["derive"] } @@ -20,6 +20,11 @@ toml = "0.7.6" tracing = "0.1.37" tracing-subscriber = "0.3.17" +[dependencies.gix] +version = "0.51.0" +default-features = false +features = ["max-performance-safe", "extras"] + [build-dependencies] vergen = { version = "8.2.4", features = ["git", "gitcl"] } walkdir = "2.3.3" diff --git a/src/config.rs b/src/config.rs index f3134e7..4ec26ff 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,23 +1,45 @@ //! Configuration from a file. -use std::{fs, io::ErrorKind, path::Path}; +use std::{fs, io::ErrorKind, path::Path, time::Duration}; use serde::Deserialize; -use tracing::info; +use tracing::{debug, info}; -#[derive(Debug, Default, Deserialize)] -pub struct Config {} +mod default { + use std::time::Duration; + + pub fn repo_update_delay() -> Duration { + Duration::from_secs(60) + } +} + +#[derive(Debug, Deserialize)] +pub struct Config { + #[serde(default = "default::repo_update_delay", with = "humantime_serde")] + pub repo_update_delay: Duration, +} + +impl Default for Config { + fn default() -> Self { + Self { + repo_update_delay: default::repo_update_delay(), + } + } +} impl Config { pub fn load(path: &Path) -> anyhow::Result { info!(path = %path.display(), "Loading config"); - Ok(match fs::read_to_string(path) { + let config = match fs::read_to_string(path) { Ok(str) => toml::from_str(&str)?, Err(e) if e.kind() == ErrorKind::NotFound => { info!("No config file found, using default config"); Self::default() } Err(e) => Err(e)?, - }) + }; + + debug!("Loaded config:\n{config:#?}"); + Ok(config) } } diff --git a/src/main.rs b/src/main.rs index 2cc5795..4fc373d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod config; mod state; mod r#static; +mod update; use std::{io, path::PathBuf}; @@ -115,6 +116,7 @@ async fn run() -> anyhow::Result<()> { select! { _ = wait_for_signal() => {}, _ = server.serve(app) => {}, + _ = update::repeatedly(state.clone()) => {}, } state.shut_down().await; diff --git a/src/update.rs b/src/update.rs new file mode 100644 index 0000000..63777dc --- /dev/null +++ b/src/update.rs @@ -0,0 +1,19 @@ +//! Repeatedly update the db from the repo. + +use tracing::{warn, debug}; + +use crate::state::AppState; + +async fn update_repo(state: &AppState) -> anyhow::Result<()> { + debug!("Updating repo"); + Ok(()) +} + +pub async fn repeatedly(state: AppState) { + loop { + if let Err(e) = update_repo(&state).await { + warn!("Error while updating repo: {e:?}"); + } + tokio::time::sleep(state.config.repo_update_delay).await; + } +}