m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-17 09:33:38 -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

47
Cargo.lock generated
View File

@@ -248,8 +248,10 @@ checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-targets 0.52.6",
]
@@ -327,6 +329,15 @@ dependencies = [
"libc",
]
[[package]]
name = "croner"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38fd53511eaf0b00a185613875fee58b208dfce016577d0ad4bb548e1c4fb3ee"
dependencies = [
"chrono",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
@@ -360,6 +371,7 @@ dependencies = [
"serde_json",
"termsize",
"tokio",
"tokio-cron-scheduler",
"xitca-web",
]
@@ -980,6 +992,17 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-derive"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -1706,6 +1729,21 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-cron-scheduler"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a5597b569b4712cf78aa0c9ae29742461b7bda1e49c2a5fdad1d79bf022f8f0"
dependencies = [
"chrono",
"croner",
"num-derive",
"num-traits",
"tokio",
"tracing",
"uuid",
]
[[package]]
name = "tokio-macros"
version = "2.3.0"
@@ -1873,6 +1911,15 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
dependencies = [
"getrandom",
]
[[package]]
name = "version_check"
version = "0.9.4"

View File

@@ -11,7 +11,7 @@ xitca-web = { version = "0.5.0", optional = true }
liquid = { version = "0.26.6", optional = true }
bollard = "0.16.1"
once_cell = "1.19.0"
http-auth = { version = "0.1.9", default-features = false, features = [] }
http-auth = { version = "0.1.9", default-features = false }
termsize = { version = "0.1.8", optional = true }
regex = { version = "1.10.5", default-features = false, features = ["perf"] }
chrono = { version = "0.4.38", default-features = false, features = ["std", "alloc", "clock"], optional = true }
@@ -24,10 +24,11 @@ http-link = "1.0.1"
itertools = "0.13.0"
serde_json = "1.0.133"
serde = "1.0.215"
tokio-cron-scheduler = { version = "0.13.0", default-features = false, optional = true }
[features]
default = ["server", "cli"]
server = ["dep:xitca-web", "dep:liquid", "dep:chrono"]
server = ["dep:xitca-web", "dep:liquid", "dep:chrono", "dep:tokio-cron-scheduler"]
cli = ["dep:indicatif", "dep:termsize"]
[profile.release]

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