use crate::bootstrap::state::AppState;
use shared::Response as RpResponse;
pub async fn handle(state: AppState, job_run_id: String) -> RpResponse {
if let Some(cancelar) = state.deploy_queue.cancel_queued(&job_run_id) {
cancelar.await;
state.bus.publish(shared::Event::DeployQueueChanged);
return RpResponse::Ok;
}
let sent = match state.active_jobs.lock() {
Ok(map) => match map.get(&job_run_id) {
Some(tx) => tx.send(true).is_ok(),
None => false,
},
Err(_) => false,
};
if sent {
RpResponse::Ok
} else {
RpResponse::err(
"NotFound",
"job_run não está em execução (já terminou ou o id é inválido)",
)
}
}
pub async fn abort_item(state: &AppState, id: &str) -> Option<RpResponse> {
if id.starts_with("jrun_") {
return Some(handle(state.clone(), id.to_string()).await);
}
if id.starts_with("jwarm_") {
return Some(match state.deploy_queue.cancel_queued(id) {
Some(cancelar) => {
cancelar.await;
state.bus.publish(shared::Event::DeployQueueChanged);
RpResponse::Ok
}
None => RpResponse::err(
"InvalidState",
"o aquecimento já está em execução e não pode ser interrompido",
),
});
}
None
}