From d696ad5bd1240dc0f79928e512b937b8f191b729 Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Thu, 9 Feb 2023 14:31:11 +0100 Subject: [PATCH] rest-server: add handle_worker from backup debug cli The function has now multiple users, so it is moved here. Signed-off-by: Lukas Wagner --- proxmox-rest-server/src/worker_task.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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(()) +}