public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v2 4/7] use new PruneJob in prune command
Date: Wed, 30 Nov 2022 16:00:59 +0100	[thread overview]
Message-ID: <20221130150102.242374-5-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20221130150102.242374-1-s.hanreich@proxmox.com>

The prune command has been slightly refactored in order to use the
functionality of the newly created PruneJob struct, instead of simply
calling compute_prune_info and the pruning the respective groups
manually.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
 src/api2/admin/datastore.rs | 69 ++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 39 deletions(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index 6797844e..90f9cb48 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -1,5 +1,6 @@
 //! Datastore Management
 
+use std::cell::RefCell;
 use std::collections::HashSet;
 use std::ffi::OsStr;
 use std::os::unix::ffi::OsStrExt;
@@ -26,7 +27,7 @@ use proxmox_sys::fs::{
     file_read_firstline, file_read_optional_string, replace_file, CreateOptions,
 };
 use proxmox_sys::sortable;
-use proxmox_sys::{task_log, task_warn};
+use proxmox_sys::task_log;
 
 use pxar::accessor::aio::Accessor;
 use pxar::EntryKind;
@@ -978,18 +979,18 @@ pub fn prune(
     let worker_id = format!("{}:{}:{}", store, ns, group);
     let group = datastore.backup_group(ns.clone(), group);
 
-    let mut prune_result = Vec::new();
+    // RefCell is okay to use here, since we access the Vec sequentially via the callback
+    let prune_result = RefCell::new(Vec::new());
 
     let list = group.list_backups()?;
 
-    let mut prune_info = PruneJob::compute_prune_info(list, &keep_options)?;
-
-    prune_info.reverse(); // delete older snapshots first
+    let mut prune_job = PruneJob::new(list, &keep_options)?;
+    prune_job.dry_run(dry_run);
 
     let keep_all = !keep_options.keeps_something();
 
     if dry_run {
-        for (info, mark) in prune_info {
+        prune_job.run_with_callback(|info, mark, _| {
             let keep = keep_all || mark.keep();
 
             let mut result = json!({
@@ -999,13 +1000,17 @@ pub fn prune(
                 "keep": keep,
                 "protected": mark.protected(),
             });
+
             let prune_ns = info.backup_dir.backup_ns();
+
             if !prune_ns.is_root() {
-                result["ns"] = serde_json::to_value(prune_ns)?;
+                result["ns"] = json!(prune_ns);
             }
-            prune_result.push(result);
-        }
-        return Ok(json!(prune_result));
+
+            prune_result.borrow_mut().push(result);
+        });
+
+        return Ok(json!(prune_result.into_inner()));
     }
 
     // We use a WorkerTask just to have a task log, but run synchrounously
@@ -1029,40 +1034,26 @@ pub fn prune(
         );
     }
 
-    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_job
+        .logging(worker.clone())
+        .run_with_callback(|info, mark, _| {
+            let keep = keep_all || mark.keep();
 
-        prune_result.push(json!({
-            "backup-type": group.ty,
-            "backup-id": group.id,
-            "backup-time": backup_time,
-            "keep": keep,
-            "protected": mark.protected(),
-        }));
+            let backup_time = info.backup_dir.backup_time();
+            let group: &pbs_api_types::BackupGroup = info.backup_dir.as_ref();
 
-        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,
-                );
-            }
-        }
-    }
+            prune_result.borrow_mut().push(json!({
+                "backup-type": group.ty,
+                "backup-id": group.id,
+                "backup-time": backup_time,
+                "keep": keep,
+                "protected": mark.protected(),
+            }));
+        });
 
     worker.log_result(&Ok(()));
 
-    Ok(json!(prune_result))
+    Ok(json!(prune_result.into_inner()))
 }
 
 #[api(
-- 
2.30.2




  parent reply	other threads:[~2022-11-30 15:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-30 15:00 [pbs-devel] [PATCH proxmox-backup v2 0/7] Add Prune Options to Sync Jobs Stefan Hanreich
2022-11-30 15:00 ` [pbs-devel] [PATCH proxmox-backup v2 1/7] Add KeepOptions parameters to pull & sync-job Stefan Hanreich
2022-11-30 15:00 ` [pbs-devel] [PATCH proxmox-backup v2 2/7] refactor prune.rs - add PruneJob struct for handling prune jobs Stefan Hanreich
2022-11-30 15:00 ` [pbs-devel] [PATCH proxmox-backup v2 3/7] Add pruning parameters to the pull command Stefan Hanreich
2022-11-30 15:00 ` Stefan Hanreich [this message]
2022-11-30 15:01 ` [pbs-devel] [PATCH proxmox-backup v2 5/7] use new PruneJob struct in Prune Jobs implementation Stefan Hanreich
2022-11-30 15:01 ` [pbs-devel] [PATCH proxmox-backup v2 6/7] add KeepOptions to Web UI of Sync Jobs Stefan Hanreich
2022-11-30 15:01 ` [pbs-devel] [PATCH proxmox-backup v2 7/7] Add documentation for prune options in " Stefan Hanreich
2022-11-30 16:23 ` [pbs-devel] [PATCH proxmox-backup v2 0/7] Add Prune Options to " Thomas Lamprecht
2022-12-01  9:50   ` Stefan Hanreich
2023-01-05  9:46     ` Thomas Lamprecht
2022-12-01 10:51 ` Lukas Wagner

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=20221130150102.242374-5-s.hanreich@proxmox.com \
    --to=s.hanreich@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