[pbs-devel] applied: [PATCH backup 4/4] log rotate: move basic rotation logic into module for reuse
Thomas Lamprecht
t.lamprecht at proxmox.com
Tue Oct 20 11:10:44 CEST 2020
Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
src/server/worker_task.rs | 24 ++++--------------------
src/tools/logrotate.rs | 31 ++++++++++++++++++++++++++++++-
2 files changed, 34 insertions(+), 21 deletions(-)
diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs
index ab8e2bf6..8ef0fde7 100644
--- a/src/server/worker_task.rs
+++ b/src/server/worker_task.rs
@@ -1,6 +1,5 @@
use std::collections::{HashMap, VecDeque};
use std::fs::File;
-use std::path::Path;
use std::io::{Read, Write, BufRead, BufReader};
use std::panic::UnwindSafe;
use std::sync::atomic::{AtomicBool, Ordering};
@@ -349,26 +348,11 @@ fn lock_task_list_files(exclusive: bool) -> Result<std::fs::File, Error> {
/// rotates it if it is
pub fn rotate_task_log_archive(size_threshold: u64, compress: bool, max_files: Option<usize>) -> Result<bool, Error> {
let _lock = lock_task_list_files(true)?;
- let path = Path::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN);
- let metadata = match path.metadata() {
- Ok(metadata) => metadata,
- Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
- Err(err) => bail!("unable to open task archive - {}", err),
- };
- if metadata.len() > size_threshold {
- let mut logrotate = LogRotate::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN, compress).ok_or_else(|| format_err!("could not get archive file names"))?;
- let backup_user = crate::backup::backup_user()?;
- logrotate.rotate(
- CreateOptions::new()
- .owner(backup_user.uid)
- .group(backup_user.gid),
- max_files,
- )?;
- Ok(true)
- } else {
- Ok(false)
- }
+ let mut logrotate = LogRotate::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN, compress)
+ .ok_or(format_err!("could not get archive file names"))?;
+
+ logrotate.rotate(size_threshold, None, max_files)
}
// atomically read/update the task list, update status of finished tasks
diff --git a/src/tools/logrotate.rs b/src/tools/logrotate.rs
index 54fe4bfe..dc703922 100644
--- a/src/tools/logrotate.rs
+++ b/src/tools/logrotate.rs
@@ -83,7 +83,7 @@ impl LogRotate {
/// foo.1.zst => foo.2.zst
/// foo => foo.1.zst
/// => foo
- pub fn rotate(&mut self, options: CreateOptions, max_files: Option<usize>) -> Result<(), Error> {
+ pub fn do_rotate(&mut self, options: CreateOptions, max_files: Option<usize>) -> Result<(), Error> {
let mut filenames: Vec<PathBuf> = self.file_names().collect();
if filenames.is_empty() {
return Ok(()); // no file means nothing to rotate
@@ -123,6 +123,35 @@ impl LogRotate {
Ok(())
}
+
+ pub fn rotate(
+ &mut self,
+ max_size: u64,
+ options: Option<CreateOptions>,
+ max_files: Option<usize>
+ ) -> Result<bool, Error> {
+
+ let options = match options {
+ Some(options) => options,
+ None => {
+ let backup_user = crate::backup::backup_user()?;
+ CreateOptions::new().owner(backup_user.uid).group(backup_user.gid)
+ },
+ };
+
+ let metadata = match self.base_path.metadata() {
+ Ok(metadata) => metadata,
+ Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
+ Err(err) => bail!("unable to open task archive - {}", err),
+ };
+
+ if metadata.len() > max_size {
+ self.do_rotate(options, max_files)?;
+ Ok(true)
+ } else {
+ Ok(false)
+ }
+ }
}
/// Iterator over logrotated file names
--
2.27.0
More information about the pbs-devel
mailing list