From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pdm-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id D40EE1FF164
	for <inbox@lore.proxmox.com>; Fri, 14 Feb 2025 14:16:47 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 2DC381A343;
	Fri, 14 Feb 2025 14:16:48 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Date: Fri, 14 Feb 2025 14:06:51 +0100
Message-Id: <20250214130653.283012-27-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250214130653.283012-1-l.wagner@proxmox.com>
References: <20250214130653.283012-1-l.wagner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.009 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
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pdm-devel] [PATCH proxmox-datacenter-manager v2 26/28] metric
 collection: factor out handle_tick and handle_control_message fns
X-BeenThere: pdm-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Datacenter Manager development discussion
 <pdm-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pdm-devel>, 
 <mailto:pdm-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pdm-devel/>
List-Post: <mailto:pdm-devel@lists.proxmox.com>
List-Help: <mailto:pdm-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel>, 
 <mailto:pdm-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Datacenter Manager development discussion
 <pdm-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pdm-devel-bounces@lists.proxmox.com
Sender: "pdm-devel" <pdm-devel-bounces@lists.proxmox.com>

No functional changes, just moving some code into seperate methods for
better readability.

Suggested-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---

Notes:
    New in v2.

 .../src/metric_collection/collection_task.rs  | 99 ++++++++++---------
 1 file changed, 54 insertions(+), 45 deletions(-)

diff --git a/server/src/metric_collection/collection_task.rs b/server/src/metric_collection/collection_task.rs
index 2ea735c5..954df0d6 100644
--- a/server/src/metric_collection/collection_task.rs
+++ b/server/src/metric_collection/collection_task.rs
@@ -89,53 +89,12 @@ impl MetricCollectionTask {
                 _ = timer.tick() => {
                     // Reload settings in case they have changed in the meanwhile
                     self.settings = Self::get_settings_or_default();
-
-                    log::debug!("starting metric collection from all remotes - triggered by timer");
-                    Self::sleep_for_random_millis(
-                        self.settings.min_interval_offset_or_default() * 1000,
-                        self.settings.max_interval_offset_or_default() * 1000,
-                        "interval-offset",
-                    ).await;
-
-                    if let Some(remotes) = Self::load_remote_config() {
-                        self.cleanup_removed_remotes_from_state(&remotes);
-
-                        let now = Instant::now();
-                        let to_fetch = remotes.order.as_slice();
-                        self.fetch_remotes(&remotes, to_fetch).await;
-                        let elapsed = now.elapsed();
-
-                        if let Err(err) = self.metric_data_tx.send(
-                            RrdStoreRequest::CollectionStats {
-                                timestamp: proxmox_time::epoch_i64(),
-                                stats: CollectionStats {
-                                    // TODO: use as_millis_f64 once stabilized
-                                    total_time: elapsed.as_secs_f64() * 1000.
-                                }
-                            }).await {
-                            log::error!("could not send collection stats to rrd task: {err}");
-                        }
-                    }
+                    self.handle_tick().await;
                 }
-
-                val = self.control_message_rx.recv() => {
-                    // Reload settings in case they have changed in the meanwhile
+                Some(message) = self.control_message_rx.recv() => {
+                    // Reload settings in case they have changed in the meanwhile.
                     self.settings = Self::get_settings_or_default();
-                    match val {
-                        Some(ControlMsg::CollectSingleRemote(remote)) => {
-                            if let Some(remotes) = Self::load_remote_config() {
-                                log::debug!("starting metric collection for remote '{remote}'- triggered by control message");
-                                self.fetch_remotes(&remotes, &[remote]).await;
-                            }
-                        }
-                        Some(ControlMsg::CollectAllRemotes) => {
-                            if let Some(remotes) = Self::load_remote_config() {
-                                log::debug!("starting metric collection from all remotes - triggered by control message");
-                                self.fetch_remotes(&remotes, &remotes.order).await;
-                            }
-                        }
-                        _ => {},
-                    }
+                    self.handle_control_message(message).await;
                 }
             }
 
@@ -158,6 +117,56 @@ impl MetricCollectionTask {
         }
     }
 
+    /// Handle a timer tick.
+    async fn handle_tick(&mut self) {
+        log::debug!("starting metric collection from all remotes - triggered by timer");
+        Self::sleep_for_random_millis(
+            self.settings.min_interval_offset_or_default() * 1000,
+            self.settings.max_interval_offset_or_default() * 1000,
+            "interval-offset",
+        )
+        .await;
+
+        if let Some(remotes) = Self::load_remote_config() {
+            self.cleanup_removed_remotes_from_state(&remotes);
+
+            let now = Instant::now();
+            let to_fetch = remotes.order.as_slice();
+            self.fetch_remotes(&remotes, to_fetch).await;
+            let elapsed = now.elapsed();
+
+            if let Err(err) = self
+                .metric_data_tx
+                .send(RrdStoreRequest::CollectionStats {
+                    timestamp: proxmox_time::epoch_i64(),
+                    stats: CollectionStats {
+                        // TODO: use as_millis_f64 once stabilized
+                        total_time: elapsed.as_secs_f64() * 1000.,
+                    },
+                })
+                .await
+            {
+                log::error!("could not send collection stats to rrd task: {err}");
+            }
+        }
+    }
+
+    /// Handle a control message for force-triggered collection.
+    async fn handle_control_message(&mut self, message: ControlMsg) {
+        if let Some(remotes) = Self::load_remote_config() {
+            match message {
+                ControlMsg::CollectSingleRemote(remote) => {
+                    log::debug!("starting metric collection for remote '{remote}'- triggered by control message");
+                    self.fetch_remotes(&remotes, &[remote]).await;
+                }
+                ControlMsg::CollectAllRemotes => {
+                    log::debug!("starting metric collection from all remotes - triggered by control message");
+                    self.fetch_remotes(&remotes, &remotes.order).await;
+                }
+            }
+        }
+    }
+
     /// Sleep between `min` and `max` milliseconds.
     ///
     /// If `min` is larger than `max`, `min` will be set to `max` and a log message
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel