use crate::bootstrap::state::AppState;
use crate::features::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.maintenance_ctx()).await {
Ok(c) => c,
Err(e) => return RpResponse::err("DatabaseError", e.to_string()),
};
let last_run = maintenance::run::load_last_run(&state.maintenance_ctx()).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.maintenance_ctx(), &config).await {
return RpResponse::err("DatabaseError", e.to_string());
}
let last_run = maintenance::run::load_last_run(&state.maintenance_ctx()).await;
RpResponse::DockerCleanupConfig { config, last_run }
}
pub async fn run_now(state: AppState) -> RpResponse {
let config = match maintenance::run::load_config(&state.maintenance_ctx()).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 {
let permit = state.deploy_queue.acquire_job_slot().await;
maintenance::run::run_holding_slot(&state.maintenance_ctx(), config, permit).await;
});
RpResponse::Ok
}
pub async fn service_warm_status_list(state: AppState) -> RpResponse {
match maintenance::service_warm::status_all(&state.maintenance_ctx()).await {
Ok(statuses) => RpResponse::ServiceWarmStatuses(statuses),
Err(e) => RpResponse::err("DatabaseError", e.to_string()),
}
}
pub async fn service_warm(state: AppState, service_id: String, force: bool) -> RpResponse {
let svc = match crate::store::services::get(&state.db, &service_id).await {
Ok(Some(s)) => s,
Ok(None) => return RpResponse::err("NotFound", "service not found"),
Err(e) => return RpResponse::err("DatabaseError", e.to_string()),
};
match maintenance::service_warm::request(&state.maintenance_ctx(), svc, force).await {
Ok(already_warm) => RpResponse::ServiceWarmRequested { already_warm },
Err(message) => RpResponse::err("CannotWarm", message),
}
}