Skip to content

Commit

Permalink
cleanup old telemetry files
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Jan 25, 2024
1 parent 0cf64dc commit 38fcea4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/telemetry/basic_transmit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use tracing::{error, info};
use std::sync::Arc;
use std::sync::RwLock as StdRwLock;
use std::path::PathBuf;
use std::time::SystemTime;
use serde_json::json;

use tokio::sync::RwLock as ARwLock;
Expand All @@ -15,6 +16,7 @@ use crate::telemetry::basic_comp_counters;
use crate::telemetry::utils::{sorted_json_files, read_file, cleanup_old_files, telemetry_storage_dirs};


const TELEMETRY_CLEANUP_COOLDOWN_SECONDS: u64 = 3600 * 24 * 30;
const TELEMETRY_TRANSMIT_AFTER_START_SECONDS: u64 = 60;
const TELEMETRY_TRANSMIT_EACH_N_SECONDS: u64 = 3600;
const TELEMETRY_FILES_KEEP: i32 = 128;
Expand Down Expand Up @@ -144,11 +146,35 @@ pub async fn basic_telemetry_send(
cleanup_old_files(dir_sent, TELEMETRY_FILES_KEEP).await;
}

async fn cleanup_old_telemetry(global_context: Arc<ARwLock<GlobalContext>>) {
let cache_dir: PathBuf = {
let cx = global_context.write().await;
cx.cache_dir.clone()
};
let (dir_compressed, _) = telemetry_storage_dirs(&cache_dir).await;
let files = sorted_json_files(dir_compressed.clone()).await;
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
for file in &files {
match tokio::fs::metadata(file).await {
Ok(attr) => {
if let Ok(time) = attr.modified() {
let time = time.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
if (now - time) > TELEMETRY_CLEANUP_COOLDOWN_SECONDS {
let _ = tokio::fs::remove_file(file).await;
}
}
}
Err(_) => {}
}
}
}

pub async fn telemetry_background_task(
global_context: Arc<ARwLock<global_context::GlobalContext>>,
) -> () {
tokio::time::sleep(tokio::time::Duration::from_secs(TELEMETRY_TRANSMIT_AFTER_START_SECONDS)).await;
basic_telemetry_send(global_context.clone()).await;
cleanup_old_telemetry(global_context.clone()).await;
loop {
tokio::time::sleep(tokio::time::Duration::from_secs(TELEMETRY_TRANSMIT_EACH_N_SECONDS)).await;
basic_telemetry_compress(global_context.clone()).await;
Expand Down

0 comments on commit 38fcea4

Please sign in to comment.