[pbs-devel] [PATCH proxmox-backup v2 4/7] use new PruneJob in prune command

Stefan Hanreich s.hanreich at proxmox.com
Wed Nov 30 16:00:59 CET 2022


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 at 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





More information about the pbs-devel mailing list