From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id B16F97362A for ; Thu, 7 Oct 2021 14:04:09 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id ADC3E19A1F for ; Thu, 7 Oct 2021 14:03:39 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 0731419A13 for ; Thu, 7 Oct 2021 14:03:39 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9443E458DA for ; Thu, 7 Oct 2021 14:03:38 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 7 Oct 2021 14:03:36 +0200 Message-Id: <20211007120337.3723803-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.304 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 1/2] rest-server: add cleanup_old_tasks X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Oct 2021 12:04:09 -0000 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 --- 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 { + 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 { -- 2.30.2