From 605e7028d5e9cf13ca5fd6b5df186297f7b11bf9 Mon Sep 17 00:00:00 2001 From: Florian Guggi Date: Sun, 15 Oct 2023 22:51:12 +0200 Subject: [PATCH] clear logfile after it has been sent and fix the corresponding test --- src/command/handlers.rs | 20 +++++++++++++------- src/main.rs | 2 +- tests/simulation/logging.rs | 4 +++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/command/handlers.rs b/src/command/handlers.rs index 6c3af68..290b866 100644 --- a/src/command/handlers.rs +++ b/src/command/handlers.rs @@ -205,9 +205,11 @@ fn build_result_archive(res: ResultId) -> Result<(), std::io::Error> { let out_path = format!("./data/{}_{}.zip", res.program_id, res.timestamp); const MAXIMUM_FILE_SIZE: u64 = 1_000_000; - let _ = truncate_to_size(&log_path, MAXIMUM_FILE_SIZE); - let _ = truncate_to_size(&res_path, MAXIMUM_FILE_SIZE); - let _ = truncate_to_size("log", MAXIMUM_FILE_SIZE); + for path in [&res_path, &log_path, &out_path, &"log".into()] { + if let Ok(true) = truncate_to_size(path, MAXIMUM_FILE_SIZE) { + log::warn!("Truncating {} from {} bytes", path, MAXIMUM_FILE_SIZE); + } + } let _ = Command::new("zip") .arg("-0") @@ -221,16 +223,19 @@ fn build_result_archive(res: ResultId) -> Result<(), std::io::Error> { Ok(()) } -/// Truncates the file at `path` to the given size -fn truncate_to_size(path: &str, n_bytes: u64) -> Result<(), std::io::Error> { +/// Truncates the file at `path` to the given size. Returns wether it actually had to truncate. +fn truncate_to_size(path: &str, n_bytes: u64) -> Result { + log::info!("Truncating {:?}", &path); let file = std::fs::File::options().write(true).open(path)?; let size = file.metadata()?.len(); if size > n_bytes { - log::warn!("Truncating {} from {} bytes", path, size); file.set_len(n_bytes)?; file.sync_all()?; + Ok(true) + } + else { + Ok(false) } - Ok(()) } /// Stops the currently running student program @@ -357,6 +362,7 @@ fn delete_result(res: ResultId) -> CommandResult { let _ = std::fs::remove_file(res_path); let _ = std::fs::remove_file(log_path); let _ = std::fs::remove_file(out_path); + let _ = truncate_to_size("log", 0); Ok(()) } diff --git a/src/main.rs b/src/main.rs index be151bd..3677948 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ fn main() -> ! { let _ = sl::WriteLogger::init( sl::LevelFilter::Info, sl::Config::default(), - std::fs::File::create(&config.log_path).unwrap(), + std::fs::OpenOptions::new().create(true).append(true).open("log").unwrap(), ); log::info!("Scheduler started"); diff --git a/tests/simulation/logging.rs b/tests/simulation/logging.rs index 2dd61b1..6f51b95 100644 --- a/tests/simulation/logging.rs +++ b/tests/simulation/logging.rs @@ -20,11 +20,13 @@ fn logfile_is_cleared_after_sent() -> std::io::Result<()> { simulate_execute_program(&mut cobc_in, &mut cobc_out, 1, 0, 3)?; std::thread::sleep(std::time::Duration::from_millis(100)); let _ = simulate_return_result(&mut cobc_in, &mut cobc_out, 1, 0)?; + cobc_out.write_all(&CEPPacket::ACK.serialize())?; + std::thread::sleep(std::time::Duration::from_millis(100)); scheduler.kill().unwrap(); let log_metadata = std::fs::metadata("./tests/tmp/log_is_cleared_after_sent/log")?; - assert!(log_metadata.len() == 0, "Logfile is not empty"); + assert!(log_metadata.len() < 50, "Logfile is not empty"); Ok(()) }