From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id C9DD31FF13C for ; Thu, 11 Jun 2026 14:04:19 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id ABE1B4099; Thu, 11 Jun 2026 14:04:19 +0200 (CEST) From: Shannon Sterz To: pdm-devel@lists.proxmox.com Subject: [PATCH datacenter-manager 16/17] server: api: tasks: move `spawn_aborted_on_shutdown()` to super module Date: Thu, 11 Jun 2026 14:03:26 +0200 Message-ID: <20260611120327.257523-17-s.sterz@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260611120327.257523-1-s.sterz@proxmox.com> References: <20260611120327.257523-1-s.sterz@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1781179365010 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.108 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: CJA6FHTXHRJCC65A5XQCEV2YET32RUQH X-Message-ID-Hash: CJA6FHTXHRJCC65A5XQCEV2YET32RUQH X-MailFrom: s.sterz@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: so other new tasks can use it. Signed-off-by: Shannon Sterz --- Notes: i wonder if we should create a trait here that handles this. we seem to repeat quite a bit of code here and imo the pattern of: `start_task` -> `run(_loop)` -> `run_single` can be moved into a trait implementation and most tasks will then only need to implement `run_single` and specify the interval for the loop. here a small example of how this could be done (only threw this together in a couple of minutes, so can be improved for sure): ```rs pub trait ServerTask { const INTERVAL: u64; fn start() -> JoinHandle<()> { tokio::spawn(async move { let future = pin!(Self::run()); let abort_future = pin!(proxmox_daemon::shutdown_future()); futures::future::select(future, abort_future).await; }) } fn run() -> impl Future + Send + 'static { async { loop { Self::run_once().await; let delay_target = task_utils::next_aligned_instant(Self::INTERVAL); tokio::time::sleep_until(tokio::time::Instant::from_std(delay_target)).await; } } } fn run_once() -> impl Future + Send + 'static; } struct CephTask; impl ServerTask for CephTask { const INTERVAL: u64 = 300; #[tracing::instrument(skip_all, name = "ceph_detection")] #[allow(refining_impl_trait)] async fn run_once() { if let Err(err) = server::ceph::sweep::sweep().await { log::warn!("ceph auto-detection sweep failed: {err:#}"); } } } ``` .../tasks/ceph_detection.rs | 18 +----------------- .../bin/proxmox-datacenter-api/tasks/mod.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/server/src/bin/proxmox-datacenter-api/tasks/ceph_detection.rs b/server/src/bin/proxmox-datacenter-api/tasks/ceph_detection.rs index fde171ad..990e15aa 100644 --- a/server/src/bin/proxmox-datacenter-api/tasks/ceph_detection.rs +++ b/server/src/bin/proxmox-datacenter-api/tasks/ceph_detection.rs @@ -6,29 +6,13 @@ //! daemon version), and refreshes the cached `ceph status` as a side effect so //! the cluster-list overview keeps showing health without a live fetch. -use std::future::Future; -use std::pin::pin; - -use tokio::task::JoinHandle; - use server::task_utils; /// How often to sweep all PVE remotes for Ceph clusters, in seconds. const SWEEP_INTERVAL: u64 = 300; -fn spawn_aborted_on_shutdown(future: F) -> JoinHandle<()> -where - F: Future + Send + 'static, -{ - tokio::spawn(async move { - let future = pin!(future); - let abort_future = pin!(proxmox_daemon::shutdown_future()); - futures::future::select(future, abort_future).await; - }) -} - pub fn start_task() { - spawn_aborted_on_shutdown(run()); + super::spawn_aborted_on_shutdown(run()); } async fn run() { diff --git a/server/src/bin/proxmox-datacenter-api/tasks/mod.rs b/server/src/bin/proxmox-datacenter-api/tasks/mod.rs index 34e4559d..2a05e9b3 100644 --- a/server/src/bin/proxmox-datacenter-api/tasks/mod.rs +++ b/server/src/bin/proxmox-datacenter-api/tasks/mod.rs @@ -4,3 +4,19 @@ pub mod ceph_detection; pub mod remote_node_mapping; pub mod remote_tasks; pub mod remote_updates; + +use std::future::Future; +use std::pin::pin; + +use tokio::task::JoinHandle; + +fn spawn_aborted_on_shutdown(future: F) -> JoinHandle<()> +where + F: Future + Send + 'static, +{ + tokio::spawn(async move { + let future = pin!(future); + let abort_future = pin!(proxmox_daemon::shutdown_future()); + futures::future::select(future, abort_future).await; + }) +} -- 2.47.3