Update repo repeatedly

This commit is contained in:
Joscha 2023-08-04 20:05:38 +02:00
parent 0a555dd9b4
commit 980e84b0f6
5 changed files with 72 additions and 24 deletions

View file

@ -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<Self> {
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)
}
}

View file

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

19
src/update.rs Normal file
View file

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