public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup] api: make prune-group a real workertask
Date: Wed, 24 Jan 2024 11:34:57 +0100	[thread overview]
Message-ID: <20240124103459.74843-1-g.goller@proxmox.com> (raw)

`prune-group` is currently not a real workertask, ie it behaves like one
but doesn't start a thread nor a task to do its work.

Changed it to start a tokio-task, so that we can delete snapshots
asynchronously. The `dry-run` feature still behaves in the same way and
returns early.

This paves the way for the new logging infra (which uses `task_local` to
define a logger) and improves performance of bigger backup-groups.

Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
 src/api2/admin/datastore.rs | 104 ++++++++++++++++++------------------
 1 file changed, 53 insertions(+), 51 deletions(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index a95031e7..4801eb1a 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -1007,62 +1007,64 @@ pub fn prune(
         }
         return Ok(json!(prune_result));
     }
+    let upid = WorkerTask::spawn(
+        "prune",
+        Some(worker_id),
+        auth_id.to_string(),
+        true,
+        move |worker| async move {
+            if keep_all {
+                task_log!(worker, "No prune selection - keeping all files.");
+            } else {
+                let mut opts = Vec::new();
+                if !ns.is_root() {
+                    opts.push(format!("--ns {ns}"));
+                }
+                crate::server::cli_keep_options(&mut opts, &keep_options);
 
-    // We use a WorkerTask just to have a task log, but run synchrounously
-    let worker = WorkerTask::new("prune", Some(worker_id), auth_id.to_string(), true)?;
-
-    if keep_all {
-        task_log!(worker, "No prune selection - keeping all files.");
-    } else {
-        let mut opts = Vec::new();
-        if !ns.is_root() {
-            opts.push(format!("--ns {ns}"));
-        }
-        crate::server::cli_keep_options(&mut opts, &keep_options);
-
-        task_log!(worker, "retention options: {}", opts.join(" "));
-        task_log!(
-            worker,
-            "Starting prune on {} group \"{}\"",
-            print_store_and_ns(&store, &ns),
-            group.group(),
-        );
-    }
-
-    for (info, mark) in prune_info {
-        let keep = keep_all || mark.keep();
-
-        let backup_time = info.backup_dir.backup_time();
-        let timestamp = info.backup_dir.backup_time_string();
-        let group: &pbs_api_types::BackupGroup = info.backup_dir.as_ref();
-
-        let msg = format!("{}/{}/{} {}", group.ty, group.id, timestamp, mark,);
-
-        task_log!(worker, "{}", msg);
-
-        prune_result.push(json!({
-            "backup-type": group.ty,
-            "backup-id": group.id,
-            "backup-time": backup_time,
-            "keep": keep,
-            "protected": mark.protected(),
-        }));
-
-        if !(dry_run || keep) {
-            if let Err(err) = info.backup_dir.destroy(false) {
-                task_warn!(
+                task_log!(worker, "retention options: {}", opts.join(" "));
+                task_log!(
                     worker,
-                    "failed to remove dir {:?}: {}",
-                    info.backup_dir.relative_path(),
-                    err,
+                    "Starting prune on {} group \"{}\"",
+                    print_store_and_ns(&store, &ns),
+                    group.group(),
                 );
             }
-        }
-    }
 
-    worker.log_result(&Ok(()));
-
-    Ok(json!(prune_result))
+            for (info, mark) in prune_info {
+                let keep = keep_all || mark.keep();
+
+                let backup_time = info.backup_dir.backup_time();
+                let timestamp = info.backup_dir.backup_time_string();
+                let group: &pbs_api_types::BackupGroup = info.backup_dir.as_ref();
+
+                let msg = format!("{}/{}/{} {}", group.ty, group.id, timestamp, mark,);
+
+                task_log!(worker, "{}", msg);
+
+                prune_result.push(json!({
+                    "backup-type": group.ty,
+                    "backup-id": group.id,
+                    "backup-time": backup_time,
+                    "keep": keep,
+                    "protected": mark.protected(),
+                }));
+
+                if !(dry_run || keep) {
+                    if let Err(err) = info.backup_dir.destroy(false) {
+                        task_warn!(
+                            worker,
+                            "failed to remove dir {:?}: {}",
+                            info.backup_dir.relative_path(),
+                            err,
+                        );
+                    }
+                }
+            }
+            Ok(())
+        },
+    )?;
+    Ok(json!(upid))
 }
 
 #[api(
-- 
2.43.0





             reply	other threads:[~2024-01-24 10:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-24 10:34 Gabriel Goller [this message]
2024-01-24 13:17 ` Fiona Ebner
2024-01-24 13:28   ` Thomas Lamprecht
2024-01-24 13:42     ` Gabriel Goller
2024-01-25 11:49       ` Gabriel Goller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240124103459.74843-1-g.goller@proxmox.com \
    --to=g.goller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal