m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-17 17:43:37 -05:00

Implement refresh schedule

This commit is contained in:
Sergio
2025-01-02 14:37:33 +02:00
parent d3b18a6587
commit 9d628e3ab2
4 changed files with 82 additions and 13 deletions

View File

@@ -40,13 +40,14 @@ pub struct ImageConfig {
pub struct Config {
version: u8,
pub agent: bool,
pub registries: FxHashMap<String, RegistryConfig>,
pub images: ImageConfig,
pub theme: Theme,
pub socket: Option<String>,
pub servers: Vec<String>,
#[serde(skip_deserializing)]
pub debug: bool,
pub images: ImageConfig,
pub refresh_interval: Option<String>,
pub registries: FxHashMap<String, RegistryConfig>,
pub servers: Vec<String>,
pub socket: Option<String>,
pub theme: Theme,
}
impl Config {
@@ -54,12 +55,13 @@ impl Config {
Self {
version: 3,
agent: false,
registries: FxHashMap::default(),
images: ImageConfig::default(),
theme: Theme::Default,
socket: None,
servers: Vec::new(),
debug: false,
images: ImageConfig::default(),
refresh_interval: None,
registries: FxHashMap::default(),
servers: Vec::new(),
socket: None,
theme: Theme::Default,
}
}

View File

@@ -4,6 +4,7 @@ use chrono::Local;
use liquid::{object, Object, ValueView};
use serde_json::Value;
use tokio::sync::Mutex;
use tokio_cron_scheduler::{Job, JobScheduler};
use xitca_web::{
body::ResponseBody,
error::Error,
@@ -47,9 +48,27 @@ const SORT_ORDER: [&str; 8] = [
pub async fn serve(port: &u16, config: &Config) -> std::io::Result<()> {
info!("Starting server, please wait...");
let data = ServerData::new(config).await;
let scheduler = JobScheduler::new().await.unwrap();
let data = Arc::new(Mutex::new(data));
let data_copy = data.clone();
if let Some(interval) = &config.refresh_interval {
scheduler
.add(
Job::new_async(interval, move |_uuid, _lock| {
let data_copy = data_copy.clone();
Box::pin(async move {
data_copy.lock().await.refresh().await;
})
})
.unwrap(),
)
.await
.unwrap();
}
scheduler.start().await.unwrap();
info!("Ready to start!");
let mut app_builder = App::new()
.with_state(Arc::new(Mutex::new(data)))
.with_state(data)
.at("/api/v2/json", get(handler_service(api_simple)))
.at("/api/v3/json", get(handler_service(api_full)))
.at("/api/v2/refresh", get(handler_service(refresh)))