public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks
@ 2021-10-07 12:03 Dominik Csapak
  2021-10-07 12:03 ` [pbs-devel] [PATCH proxmox-backup 2/2] proxmox-backup-proxy: clean up old tasks when the task log was rotated Dominik Csapak
  2021-10-08  4:49 ` [pbs-devel] applied: [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks Dietmar Maurer
  0 siblings, 2 replies; 4+ messages in thread
From: Dominik Csapak @ 2021-10-07 12:03 UTC (permalink / raw)
  To: pbs-devel

this is a helper that removes task log files that are not referenced
by the task archive anymore

it gets the oldest task archive file, gets the first endtime (the
oldest) and removes all files in the taskdir where the mtime is older
than that

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
we could check if the filename is a valid UPID, but thought that
would only add unnecessary overhead...

 proxmox-rest-server/src/worker_task.rs | 58 ++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/proxmox-rest-server/src/worker_task.rs b/proxmox-rest-server/src/worker_task.rs
index 0ac3f431..51394549 100644
--- a/proxmox-rest-server/src/worker_task.rs
+++ b/proxmox-rest-server/src/worker_task.rs
@@ -5,6 +5,7 @@ use std::io::{Read, Write, BufRead, BufReader};
 use std::panic::UnwindSafe;
 use std::sync::atomic::{AtomicBool, Ordering};
 use std::sync::{Arc, Mutex};
+use std::time::{SystemTime, Duration};
 
 use anyhow::{bail, format_err, Error};
 use futures::*;
@@ -218,6 +219,63 @@ pub fn rotate_task_log_archive(size_threshold: u64, compress: bool, max_files: O
     logrotate.rotate(size_threshold, None, max_files)
 }
 
+/// removes all task logs that are older than the oldest task entry in the
+/// task archive
+pub fn cleanup_old_tasks(compressed: bool) -> Result<(), Error> {
+    let setup = worker_task_setup()?;
+
+    let _lock = setup.lock_task_list_files(true)?;
+
+    let logrotate = LogRotate::new(&setup.task_archive_fn, compressed)
+            .ok_or_else(|| format_err!("could not get archive file names"))?;
+
+    let mut timestamp = None;
+    if let Some(last_file) = logrotate.files().last() {
+        let reader = BufReader::new(last_file);
+        for line in reader.lines() {
+            let line = line?;
+            if let Ok((_, _, Some(state))) = parse_worker_status_line(&line) {
+                timestamp = Some(state.endtime());
+                break;
+            }
+        }
+    }
+
+    fn get_modified(entry: std::fs::DirEntry) -> Result<SystemTime, std::io::Error> {
+        entry.metadata()?.modified()
+    }
+
+    if let Some(timestamp) = timestamp {
+        let cutoff_time = if timestamp > 0 {
+            SystemTime::UNIX_EPOCH.checked_add(Duration::from_secs(timestamp as u64))
+        } else {
+            SystemTime::UNIX_EPOCH.checked_sub(Duration::from_secs(-timestamp as u64))
+        }.ok_or_else(|| format_err!("could not calculate cutoff time"))?;
+
+        for i in 0..256 {
+            let mut path = setup.taskdir.clone();
+            path.push(format!("{:02X}", i));
+            for file in std::fs::read_dir(path)? {
+                let file = file?;
+                let path = file.path();
+
+                let modified = get_modified(file)
+                    .map_err(|err| format_err!("error getting mtime for {:?}: {}", path, err))?;
+
+                if modified < cutoff_time {
+                    match std::fs::remove_file(path) {
+                        Ok(()) => {},
+                        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {},
+                        Err(err) => bail!("could not remove file: {}", err),
+                    }
+                }
+            }
+        }
+    }
+
+    Ok(())
+}
+
 
 /// Path to the worker log file
 pub fn upid_log_path(upid: &UPID) -> Result<std::path::PathBuf, Error> {
-- 
2.30.2





^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pbs-devel] [PATCH proxmox-backup 2/2] proxmox-backup-proxy: clean up old tasks when the task log was rotated
  2021-10-07 12:03 [pbs-devel] [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks Dominik Csapak
@ 2021-10-07 12:03 ` Dominik Csapak
  2021-10-08  4:49   ` [pbs-devel] applied: " Dietmar Maurer
  2021-10-08  4:49 ` [pbs-devel] applied: [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks Dietmar Maurer
  1 sibling, 1 reply; 4+ messages in thread
From: Dominik Csapak @ 2021-10-07 12:03 UTC (permalink / raw)
  To: pbs-devel

we maybe have old tasks when the task list was rotated, so clean them up

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
if we want to be *really* conservative with that call, we can modify the
rotate call so that it returns the info if we deleted a log file and
only call it then, but in my tests deleting thousands of files did not
even take a second (on a spinner)

 src/bin/proxmox-backup-proxy.rs | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs
index 9199ebae..4c879483 100644
--- a/src/bin/proxmox-backup-proxy.rs
+++ b/src/bin/proxmox-backup-proxy.rs
@@ -22,13 +22,13 @@ use proxmox::api::{RpcEnvironment, RpcEnvironmentType, UserInformation};
 use proxmox::sys::linux::socket::set_tcp_keepalive;
 use proxmox::tools::fs::CreateOptions;
 
-use pbs_tools::task_log;
+use pbs_tools::{task_log, task_warn};
 use pbs_datastore::DataStore;
 use proxmox_rrd::DST;
 
 use proxmox_rest_server::{
     rotate_task_log_archive, extract_cookie , AuthError, ApiConfig, RestServer, RestEnvironment,
-    ServerAdapter, WorkerTask,
+    ServerAdapter, WorkerTask, cleanup_old_tasks,
 };
 
 use proxmox_backup::{
@@ -827,6 +827,13 @@ async fn schedule_task_log_rotate() {
                     task_log!(worker, "API authentication log was not rotated");
                 }
 
+                if has_rotated {
+                    task_log!(worker, "cleaning up old task logs");
+                    if let Err(err) = cleanup_old_tasks(true) {
+                        task_warn!(worker, "could not completely cleanup old tasks: {}", err);
+                    }
+                }
+
                 Ok(())
             });
 
-- 
2.30.2





^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pbs-devel] applied: [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks
  2021-10-07 12:03 [pbs-devel] [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks Dominik Csapak
  2021-10-07 12:03 ` [pbs-devel] [PATCH proxmox-backup 2/2] proxmox-backup-proxy: clean up old tasks when the task log was rotated Dominik Csapak
@ 2021-10-08  4:49 ` Dietmar Maurer
  1 sibling, 0 replies; 4+ messages in thread
From: Dietmar Maurer @ 2021-10-08  4:49 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Dominik Csapak

applied




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pbs-devel] applied: [PATCH proxmox-backup 2/2] proxmox-backup-proxy: clean up old tasks when the task log was rotated
  2021-10-07 12:03 ` [pbs-devel] [PATCH proxmox-backup 2/2] proxmox-backup-proxy: clean up old tasks when the task log was rotated Dominik Csapak
@ 2021-10-08  4:49   ` Dietmar Maurer
  0 siblings, 0 replies; 4+ messages in thread
From: Dietmar Maurer @ 2021-10-08  4:49 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Dominik Csapak

applied




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-08  4:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-07 12:03 [pbs-devel] [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks Dominik Csapak
2021-10-07 12:03 ` [pbs-devel] [PATCH proxmox-backup 2/2] proxmox-backup-proxy: clean up old tasks when the task log was rotated Dominik Csapak
2021-10-08  4:49   ` [pbs-devel] applied: " Dietmar Maurer
2021-10-08  4:49 ` [pbs-devel] applied: [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks Dietmar Maurer

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