Added Chrome Job Profiling and Handling
This commit is contained in:
parent
5b99621044
commit
e4e454960e
5 changed files with 90 additions and 40 deletions
|
|
@ -14,6 +14,7 @@ use axum::{
|
|||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
use axum::Json as AxumJson;
|
||||
use futures::StreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::{sync::{broadcast, Mutex}};
|
||||
|
|
@ -22,9 +23,12 @@ use tokio_stream::wrappers::BroadcastStream;
|
|||
use tower_http::services::ServeDir;
|
||||
use uuid::Uuid;
|
||||
|
||||
use assets::{DEFAULT_APP_JS, DEFAULT_INDEX_HTML, DEFAULT_STYLES_CSS};
|
||||
use crate::chromium::profiles;
|
||||
use crate::{headless, state::SharedState};
|
||||
|
||||
use assets::{DEFAULT_APP_JS, DEFAULT_INDEX_HTML, DEFAULT_STYLES_CSS};
|
||||
|
||||
|
||||
const WEB_DIR: &str = "web";
|
||||
|
||||
fn exe_dir() -> PathBuf {
|
||||
|
|
@ -65,41 +69,57 @@ struct StartResponse {
|
|||
job_id: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LogsQuery {
|
||||
job: String,
|
||||
}
|
||||
|
||||
|
||||
async fn start_job(
|
||||
axum::extract::State(st): axum::extract::State<HttpState>,
|
||||
Json(req): Json<StartRequest>,
|
||||
) -> axum::Json<StartResponse> {
|
||||
use axum::Json as AxumJson;
|
||||
|
||||
) -> AxumJson<StartResponse> {
|
||||
let ip = req.ip.trim().to_string();
|
||||
|
||||
// neuen Job anlegen
|
||||
// Neuen Job anlegen
|
||||
let job_id = uuid::Uuid::new_v4().to_string();
|
||||
let (tx, _rx) = tokio::sync::broadcast::channel::<String>(200);
|
||||
|
||||
{
|
||||
// in Registry eintragen
|
||||
let mut map = st.jobs.lock().await;
|
||||
map.insert(job_id.clone(), tx.clone());
|
||||
}
|
||||
|
||||
// Klone für den Task
|
||||
// Profilordner erzeugen (unter <exe>/runtime/chrome-profiles/job-<id>)
|
||||
let exe_dir = exe_dir(); // du hast die Funktion schon
|
||||
let profile_dir = profiles::create_job_profile(&exe_dir, &job_id)
|
||||
.expect("profile dir create failed");
|
||||
|
||||
let job_id_for_task = job_id.clone();
|
||||
let jobs_map = st.jobs.clone();
|
||||
|
||||
// Optional: kleiner Delay, damit der Client Zeit hat den SSE-Stream zu verbinden
|
||||
tokio::spawn(async move {
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
tx.send(format!("[Job {job_id_for_task}] Starte Setup für {ip}")).ok();
|
||||
use tokio::time::{sleep, Duration};
|
||||
sleep(Duration::from_millis(250)).await;
|
||||
|
||||
// WICHTIG: Ergebnis NICHT in Variable halten, sondern sofort matchen,
|
||||
// damit kein non-Send Error über das spätere .await (Mutex) "lebt".
|
||||
if let Err(e) = crate::headless::run_test(&ip, tx.clone()).await {
|
||||
tx.send(format!("[Job {job_id_for_task}] ❌ Fehler: {e}")).ok();
|
||||
} else {
|
||||
tx.send(format!("[Job {job_id_for_task}] ✅ Fertig")).ok();
|
||||
tx.send(format!("[Job {job_id_for_task}] starte Setup für {ip}")).ok();
|
||||
|
||||
// --- Wichtig: Ergebnis sofort in String-Nachricht umwandeln,
|
||||
// damit kein `Box<dyn Error>` über ein späteres .await "lebt".
|
||||
let result_msg = match crate::headless::run_test(&ip, tx.clone(), &profile_dir).await {
|
||||
Ok(_) => format!("[Job {job_id_for_task}] ✅ fertig"),
|
||||
Err(e) => format!("[Job {job_id_for_task}] ❌ fehler: {e}"),
|
||||
};
|
||||
tx.send(result_msg).ok();
|
||||
// --- ab hier existiert kein non-Send Error mehr
|
||||
|
||||
// Cleanup: Profilordner löschen (optional)
|
||||
sleep(Duration::from_millis(250)).await;
|
||||
if let Err(e) = std::fs::remove_dir_all(&profile_dir) {
|
||||
tx.send(format!("[Job {job_id_for_task}] ⚠ cleanup fehlgeschlagen: {e}")).ok();
|
||||
}
|
||||
|
||||
// Erst NACH dem match den Mutex awaiten -> der Error ist schon gedroppt
|
||||
// Job deregistrieren
|
||||
let mut m = jobs_map.lock().await;
|
||||
m.remove(&job_id_for_task);
|
||||
});
|
||||
|
|
@ -107,11 +127,6 @@ async fn start_job(
|
|||
AxumJson(StartResponse { job_id })
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LogsQuery {
|
||||
job: String,
|
||||
}
|
||||
|
||||
async fn stream_logs(
|
||||
AxumState(st): AxumState<HttpState>,
|
||||
Query(q): Query<LogsQuery>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue