Skip to content

Commit 752e679

Browse files
committed
Keep files younger than days_to_keep
1 parent 2d86007 commit 752e679

6 files changed

Lines changed: 18 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "historyprovider"
3-
version = "2.2.1"
3+
version = "2.3.0"
44
edition = "2024"
55

66
[[bin]]

src/cleanup.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use tokio::io;
55
use std::collections::HashMap;
66
use std::path::{Path, PathBuf};
77

8+
use crate::util::msec_to_log2_filename;
9+
810
#[derive(Debug, Clone)]
911
pub(crate) struct LogFile {
1012
pub(crate) name: PathBuf,
@@ -40,11 +42,11 @@ pub(crate) async fn collect_log2_files(dir: impl AsRef<Path>) -> io::Result<Vec<
4042
}
4143

4244
/// Prune `.log2` files while keeping the newest one per directory
43-
pub(crate) async fn cleanup_log2_files(dir: impl AsRef<Path>, size_limit: u64) -> io::Result<()> {
45+
pub(crate) async fn cleanup_log2_files(dir: impl AsRef<Path>, size_limit: u64, days_to_keep: i64) -> io::Result<()> {
4446
let files = collect_log2_files(dir.as_ref()).await?;
4547
let mut files_size: u64 = files.iter().map(|f| f.size).sum();
4648

47-
info!("log2 files size: {files_size}, size limit: {size_limit}");
49+
info!("log2 files size: {files_size}, size limit: {size_limit}, days_to_keep: {days_to_keep}");
4850
if files_size < size_limit {
4951
info!("Size limit not hit, nothing to cleanup");
5052
return Ok(());
@@ -58,11 +60,15 @@ pub(crate) async fn cleanup_log2_files(dir: impl AsRef<Path>, size_limit: u64) -
5860

5961
let mut deletable_files = Vec::new();
6062

63+
// This file doesn't have to exist, I'm only constructing the filename for log retention.
64+
let oldest_file_to_keep = PathBuf::from(msec_to_log2_filename(shvproto::DateTime::now().add_days(-days_to_keep).epoch_msec()));
65+
info!("keeping files younger than {filename}", filename = oldest_file_to_keep.to_string_lossy());
66+
6167
for (_dir, mut group) in grouped {
6268
// Sort descending (newest first)
6369
group.sort_by(|a, b| b.name.cmp(&a.name));
64-
// Keep the newest file
65-
deletable_files.extend(group.into_iter().skip(1));
70+
// Keep the newest file, and keep files newer than oldest_file_to_delete
71+
deletable_files.extend(group.into_iter().skip(1).filter(|log_file| log_file.name < oldest_file_to_keep));
6672
}
6773

6874
// Sort deletable files (oldest first for deletion)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct HpConfig {
3838
max_sync_tasks: Option<usize>,
3939
max_journal_dir_size: Option<usize>,
4040
periodic_sync_interval: Option<u64>,
41+
days_to_keep: Option<i64>,
4142
}
4243

4344
impl HpConfig {

src/sync.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ pub(crate) async fn sync_task(
722722
// The download size limit should be lower than the max_journal_dir_size, because it doesn't
723723
// count in the files synced by getLog.
724724
let max_journal_dir_size = log_size_limit(&app_state.config);
725+
let days_to_keep = app_state.config.days_to_keep.unwrap_or(0);
725726

726727
while let Some(cmd) = sync_cmd_rx.next().await {
727728
match cmd {
@@ -817,7 +818,7 @@ pub(crate) async fn sync_task(
817818
panic!("Cannot send dirtylog Trim command for site {site}: {e}")
818819
)
819820
);
820-
match cleanup_log2_files(&app_state.config.journal_dir, max_journal_dir_size).await {
821+
match cleanup_log2_files(&app_state.config.journal_dir, max_journal_dir_size, days_to_keep).await {
821822
Ok(_) => info!("Cleanup journal dir done"),
822823
Err(err) => error!("Cleanup journal dir error: {err}"),
823824
}
@@ -881,7 +882,7 @@ pub(crate) async fn sync_task(
881882
.unwrap_or_else(|e|
882883
panic!("Cannot send dirtylog Trim command for site {site}: {e}")
883884
);
884-
match cleanup_log2_files(&app_state.config.journal_dir, max_journal_dir_size).await {
885+
match cleanup_log2_files(&app_state.config.journal_dir, max_journal_dir_size, days_to_keep).await {
885886
Ok(_) => info!("Cleanup journal dir done"),
886887
Err(err) => error!("Cleanup journal dir error: {err}"),
887888
}
@@ -891,7 +892,7 @@ pub(crate) async fn sync_task(
891892
}
892893
SyncCommand::Cleanup => {
893894
info!("Cleanup journal dir start");
894-
match cleanup_log2_files(&app_state.config.journal_dir, max_journal_dir_size).await {
895+
match cleanup_log2_files(&app_state.config.journal_dir, max_journal_dir_size, days_to_keep).await {
895896
Ok(_) => info!("Cleanup journal dir done"),
896897
Err(err) => error!("Cleanup journal dir error: {err}"),
897898
}

src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ pub mod testing {
406406
journal_dir: journal_dir.path().to_str().expect("path must work").to_string(),
407407
max_sync_tasks: None,
408408
max_journal_dir_size: None,
409+
days_to_keep: None,
409410
periodic_sync_interval: Some(3),
410411
},
411412
dirtylog_cmd_tx,

0 commit comments

Comments
 (0)