use crate::api::AppState;
use crate::maintenance;
use chrono::Utc;
use shared::{DockerCleanupConfig, Response as RpResponse};
pub async fn get_config(state: AppState) -> RpResponse {
let config = match maintenance::run::load_config(&state).await {
Ok(c) => c,
Err(e) => return RpResponse::err("DatabaseError", e.to_string()),
};
let last_run = maintenance::run::load_last_run(&state).await;
RpResponse::DockerCleanupConfig { config, last_run }
}
pub async fn set_config(state: AppState, mut config: DockerCleanupConfig) -> RpResponse {
config.recompute_next_run(Utc::now());
if let Err(e) = maintenance::run::save_config(&state, &config).await {
return RpResponse::err("DatabaseError", e.to_string());
}
let last_run = maintenance::run::load_last_run(&state).await;
RpResponse::DockerCleanupConfig { config, last_run }
}
pub async fn run_now(state: AppState) -> RpResponse {
let config = match maintenance::run::load_config(&state).await {
Ok(c) => c,
Err(e) => return RpResponse::err("DatabaseError", e.to_string()),
};
if !config.any_resource_enabled() {
return RpResponse::err(
"NoResourceSelected",
"marque pelo menos um recurso antes de executar",
);
}
tokio::spawn(async move {
maintenance::run::run(&state, config).await;
});
RpResponse::Ok
}