v0.2.2
This commit is contained in:
32
src/web.rs
32
src/web.rs
@@ -12,6 +12,7 @@ use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
@@ -99,6 +100,9 @@ pub async fn run_web_server(config: Config) -> anyhow::Result<()> {
|
||||
.route("/api/generate", post(generate_jobs))
|
||||
.route("/api/cleanup", post(cleanup_jobs))
|
||||
.route("/api/status", get(get_status))
|
||||
.route("/api/jobs/stop-all", post(stop_all_jobs))
|
||||
.nest_service("/static", ServeDir::new("src/static"))
|
||||
.nest_service("/assets", ServeDir::new("assets"))
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.with_state(state);
|
||||
|
||||
@@ -176,6 +180,34 @@ async fn get_status(State(state): State<AppState>) -> Result<Json<serde_json::Va
|
||||
Ok(Json(status))
|
||||
}
|
||||
|
||||
async fn stop_all_jobs(State(state): State<AppState>) -> Result<impl IntoResponse, AppError> {
|
||||
use reqwest::Client;
|
||||
|
||||
let client = Client::new();
|
||||
let api_url = &state.config.nexrender_api_url;
|
||||
|
||||
// Получаем все задания
|
||||
let jobs = fetch_all_jobs(api_url)
|
||||
.await
|
||||
.map_err(|e| AppError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
let mut stopped = 0;
|
||||
for job in jobs {
|
||||
let state = job.get("state").and_then(|s| s.as_str()).unwrap_or("");
|
||||
if state == "queued" || state == "started" || state == "processing" {
|
||||
if let Some(uid) = job.get("uid").and_then(|u| u.as_str()) {
|
||||
// Отправляем DELETE запрос для остановки задания
|
||||
let _ = client.delete(&format!("{}/{}", api_url, uid)).send().await;
|
||||
stopped += 1;
|
||||
log::info!("Stopped job: {}", uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("Stopped {} active jobs", stopped);
|
||||
Ok((StatusCode::OK, format!("Stopped {} jobs", stopped)))
|
||||
}
|
||||
|
||||
// Error handling
|
||||
struct AppError(StatusCode, String);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user