From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id D2EE46577E for ; Thu, 23 Jul 2020 15:20:16 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id BF05129723 for ; Thu, 23 Jul 2020 15:20:16 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 277872970E for ; Thu, 23 Jul 2020 15:20:15 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E92EE4333A for ; Thu, 23 Jul 2020 15:20:14 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 23 Jul 2020 15:20:13 +0200 Message-Id: <20200723132013.22398-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200723132013.22398-1-d.csapak@proxmox.com> References: <20200723132013.22398-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.001 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.379 Envelope sender has no MX or A DNS records RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pbs-devel] [RFC PATCH proxmox-backup 2/2] server/state: add spawn_internal_task and use it for websockets X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jul 2020 13:20:16 -0000 is a helper to spawn an internal tokio task without it showing up in the task list it is still tracked for reload and notifies the last_worker_listeners this enables the console to survive a reload of proxmox-backup-proxy Signed-off-by: Dominik Csapak --- i am not completely sure if this is the right way... alternatively, we could of course have WorkerTasks that simply do not show up in the task list... though for what we are doing here, it is much overhead thats not really needed src/api2/node.rs | 2 +- src/server/state.rs | 36 +++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/api2/node.rs b/src/api2/node.rs index b572a9c..b80a661 100644 --- a/src/api2/node.rs +++ b/src/api2/node.rs @@ -267,7 +267,7 @@ fn upgrade_to_websocket( let (ws, response) = WebSocket::new(parts.headers)?; - tokio::spawn(async move { + crate::server::spawn_internal_task(async move { let conn: Upgraded = match req_body.on_upgrade().map_err(Error::from).await { Ok(upgraded) => upgraded, _ => bail!("error"), diff --git a/src/server/state.rs b/src/server/state.rs index 41e910d..d073243 100644 --- a/src/server/state.rs +++ b/src/server/state.rs @@ -19,6 +19,7 @@ pub struct ServerState { pub shutdown_listeners: BroadcastData<()>, pub last_worker_listeners: BroadcastData<()>, pub worker_count: usize, + pub task_count: usize, pub reload_request: bool, } @@ -28,6 +29,7 @@ lazy_static! { shutdown_listeners: BroadcastData::new(), last_worker_listeners: BroadcastData::new(), worker_count: 0, + task_count: 0, reload_request: false, }); } @@ -101,20 +103,40 @@ pub fn last_worker_future() -> impl Future> { } pub fn set_worker_count(count: usize) { + SERVER_STATE.lock().unwrap().worker_count = count; + + check_last_worker(); +} + +pub fn check_last_worker() { let mut data = SERVER_STATE.lock().unwrap(); - data.worker_count = count; - if !(data.mode == ServerMode::Shutdown && data.worker_count == 0) { return; } + if !(data.mode == ServerMode::Shutdown && data.worker_count == 0 && data.task_count == 0) { return; } data.last_worker_listeners.notify_listeners(Ok(())); } - -pub fn check_last_worker() { - +/// Spawns a tokio task that will be tracked for reload +/// and if it is finished, notify the last_worker_listener if we +/// are in shutdown mode +pub fn spawn_internal_task(task: T) +where + T: Future + Send + 'static, + T::Output: Send + 'static, +{ let mut data = SERVER_STATE.lock().unwrap(); + data.task_count += 1; - if !(data.mode == ServerMode::Shutdown && data.worker_count == 0) { return; } + tokio::spawn(async move { + let _ = tokio::spawn(task).await; // ignore errors - data.last_worker_listeners.notify_listeners(Ok(())); + { // drop mutex + let mut data = SERVER_STATE.lock().unwrap(); + if data.task_count > 0 { + data.task_count -= 1; + } + } + + check_last_worker(); + }); } -- 2.20.1