Skip to content

Commit

Permalink
Added longer thread sleep to rotator loop
Browse files Browse the repository at this point in the history
  • Loading branch information
okynos committed Apr 18, 2024
1 parent 7691d3e commit 3d247d7
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions src/rotator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
use std::fs::{metadata, File, copy, read_to_string, remove_file, create_dir};
use std::io::Write;
use std::path::Path;
use std::time::{SystemTime, Duration, UNIX_EPOCH};
use std::time::Duration;
use std::thread;
use log::{debug, error, info};
use std::ptr::addr_of_mut;

use crate::config;
use crate::utils;
Expand Down Expand Up @@ -175,54 +176,50 @@ fn rotate_file(filepath: &str, iteration: u32, lock: &mut bool){
#[cfg(not(tarpaulin_include))]
pub fn rotator(){
let config = unsafe { super::GCONFIG.clone().unwrap() };
let mut start_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();

loop{
if (start_time + Duration::new(10, 0)).as_millis() <
SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_millis() {

let log_size = if Path::new(config.clone().log_file.as_str()).exists() {
metadata(config.clone().log_file).unwrap().len() as usize
}else{ 0 };

let events_size = if Path::new(config.clone().events_file.as_str()).exists() {
metadata(config.clone().events_file).unwrap().len() as usize
}else{ 0 };

if events_size >= config.events_max_file_size * 1000000 {
let events_path = Path::new(config.events_file.as_str());
let mut parent_path = events_path.parent().unwrap().to_path_buf();
parent_path.push("archive");

if ! parent_path.exists(){
match create_dir(parent_path.clone()){
Ok(_v) => debug!("Archive directory created successfully."),
Err(e) => error!("Cannot create archive directory, error: {}", e)
};
}

unsafe { rotate_file(config.clone().events_file.as_str(),
get_iteration(parent_path.to_str().unwrap()), &mut config::TMP_EVENTS) };
let log_size = if Path::new(config.clone().log_file.as_str()).exists() {
metadata(config.clone().log_file).unwrap().len() as usize
}else{ 0 };

let events_size = if Path::new(config.clone().events_file.as_str()).exists() {
metadata(config.clone().events_file).unwrap().len() as usize
}else{ 0 };

if events_size >= config.events_max_file_size * 1000000 {
let events_path = Path::new(config.events_file.as_str());
let mut parent_path = events_path.parent().unwrap().to_path_buf();
parent_path.push("archive");

if ! parent_path.exists(){
match create_dir(parent_path.clone()){
Ok(_v) => debug!("Archive directory created successfully."),
Err(e) => error!("Cannot create archive directory, error: {}", e)
};
}

if log_size >= config.log_max_file_size * 1000000 {
let log_path = Path::new(config.log_file.as_str());
let mut parent_path = log_path.parent().unwrap().to_path_buf();
parent_path.push("archive");
unsafe { rotate_file(config.clone().events_file.as_str(),
get_iteration(parent_path.to_str().unwrap()), &mut *addr_of_mut!(config::TMP_EVENTS)) };
}

if ! parent_path.exists(){
match create_dir(parent_path.clone()){
Ok(_v) => debug!("Archive directory created successfully."),
Err(e) => error!("Cannot create archive directory, error: {}", e)
};
}
if log_size >= config.log_max_file_size * 1000000 {
let log_path = Path::new(config.log_file.as_str());
let mut parent_path = log_path.parent().unwrap().to_path_buf();
parent_path.push("archive");

rotate_file(config.clone().log_file.as_str(),
get_iteration(parent_path.to_str().unwrap()), &mut true);
if ! parent_path.exists(){
match create_dir(parent_path.clone()){
Ok(_v) => debug!("Archive directory created successfully."),
Err(e) => error!("Cannot create archive directory, error: {}", e)
};
}

start_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
rotate_file(config.clone().log_file.as_str(),
get_iteration(parent_path.to_str().unwrap()), &mut true);
}

debug!("Sleeping rotator thread for 30 minutes");
thread::sleep(Duration::from_secs(1800));
}
}

Expand Down

0 comments on commit 3d247d7

Please sign in to comment.