diff --git a/proxmox-rest-server/src/worker_task.rs b/proxmox-rest-server/src/worker_task.rs index 44d81119..6263ce70 100644 --- a/proxmox-rest-server/src/worker_task.rs +++ b/proxmox-rest-server/src/worker_task.rs @@ -14,6 +14,7 @@ use nix::fcntl::OFlag; use once_cell::sync::OnceCell; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; +use tokio::signal::unix::SignalKind; use tokio::sync::oneshot; use proxmox_lang::try_block; @@ -1056,3 +1057,25 @@ pub fn abort_local_worker(upid: UPID) { worker.request_abort(); } } + +/// Wait for locally running worker, responding to SIGINT properly +pub async fn handle_worker(upid_str: &str) -> Result<(), Error> { + let upid: UPID = upid_str.parse()?; + let mut signal_stream = tokio::signal::unix::signal(SignalKind::interrupt())?; + let abort_future = async move { + while signal_stream.recv().await.is_some() { + println!("got shutdown request (SIGINT)"); + abort_local_worker(upid.clone()); + } + Ok::<_, Error>(()) + }; + + let result_future = wait_for_local_worker(upid_str); + + futures::select! { + result = result_future.fuse() => result?, + abort = abort_future.fuse() => abort?, + }; + + Ok(()) +}