Skip to content

Commit

Permalink
clear logfile after it has been sent and fix the corresponding test
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Guggi committed Oct 15, 2023
1 parent 81b3dfa commit 605e702
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/command/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<bool, std::io::Error> {
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
Expand Down Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion tests/simulation/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 605e702

Please sign in to comment.