From 1ebc6fddeeadc88d402802e06e1c1aa4d30649fa Mon Sep 17 00:00:00 2001 From: Florian Guggi Date: Sun, 15 Sep 2024 19:27:53 +0200 Subject: [PATCH] Delete individual result files immediately after packing Closes: #153 --- scheduler/src/command/common.rs | 11 ---------- scheduler/src/command/execute_program.rs | 10 ++++++--- scheduler/src/command/return_result.rs | 21 ++----------------- scheduler/tests/simulation/logging.rs | 2 +- .../tests/software_tests/execute_program.rs | 9 +++----- 5 files changed, 13 insertions(+), 40 deletions(-) diff --git a/scheduler/src/command/common.rs b/scheduler/src/command/common.rs index 0e275d8..d6c6e1f 100644 --- a/scheduler/src/command/common.rs +++ b/scheduler/src/command/common.rs @@ -19,17 +19,6 @@ pub fn check_length( } } -/// Truncates the files to at most `n_bytes` -pub fn truncate_to_size(file: &mut std::fs::File, n_bytes: u64) -> Result<(), std::io::Error> { - let size = file.metadata()?.len(); - if size > n_bytes { - file.set_len(n_bytes)?; - file.sync_all()?; - } - - Ok(()) -} - /// If no program is currently running, this function simply returns. Otherwise it signals the /// supervisor thread to kill the student program and waits for a maximum of 2s before returning /// and error diff --git a/scheduler/src/command/execute_program.rs b/scheduler/src/command/execute_program.rs index dc9378b..5cb25ed 100644 --- a/scheduler/src/command/execute_program.rs +++ b/scheduler/src/command/execute_program.rs @@ -153,9 +153,13 @@ fn build_result_archive(res: ResultId) -> Result<(), std::io::Error> { let student_log_path = PathBuf::from(format!("./data/{res}.log")); let log_path = PathBuf::from("./log"); - add_to_archive_if_exists(&mut archive, &res.to_string(), res_path, Compression::None)?; - add_to_archive_if_exists(&mut archive, "student_log", student_log_path, Compression::Zopfli)?; - add_to_archive_if_exists(&mut archive, "log", log_path, Compression::Zopfli)?; + add_to_archive_if_exists(&mut archive, &res.to_string(), &res_path, Compression::None)?; + add_to_archive_if_exists(&mut archive, "student_log", &student_log_path, Compression::Zopfli)?; + add_to_archive_if_exists(&mut archive, "log", &log_path, Compression::Zopfli)?; + + let _ = std::fs::remove_file(res_path); + let _ = std::fs::remove_file(student_log_path); + let _ = std::fs::OpenOptions::new().write(true).truncate(true).open(log_path); Ok(()) } diff --git a/scheduler/src/command/return_result.rs b/scheduler/src/command/return_result.rs index 9262621..b7d4555 100644 --- a/scheduler/src/command/return_result.rs +++ b/scheduler/src/command/return_result.rs @@ -1,4 +1,4 @@ -use super::{truncate_to_size, CommandResult, SyncExecutionContext}; +use super::{CommandResult, SyncExecutionContext}; use crate::{ command::{check_length, CommandError, Event, ResultId, COMMAND_TIMEOUT}, communication::{CEPPacket, CommunicationHandle}, @@ -31,7 +31,7 @@ pub fn return_result( com.await_ack(COMMAND_TIMEOUT)?; let result_id = ResultId { program_id, timestamp }; - delete_result(result_id)?; + let _ = std::fs::remove_file(format!("./data/{result_id}")); let mut l_exec = exec.lock().unwrap(); if let Some(event_index) = @@ -45,20 +45,3 @@ pub fn return_result( l_exec.configure_update_pin(); Ok(()) } - -/// Deletes the result archive corresponding to the next element in the result queue and removes -/// that element from the queue. The update pin is updated accordingly -fn delete_result(res: ResultId) -> CommandResult { - let res_path = format!("./archives/{}/results/{}", res.program_id, res.timestamp); - let log_path = format!("./data/{}_{}.log", res.program_id, res.timestamp); - let out_path = format!("./data/{}_{}", res.program_id, res.timestamp); - let _ = std::fs::remove_file(res_path); - let _ = std::fs::remove_file(log_path); - let _ = std::fs::remove_file(out_path); - - if let Ok(mut file) = std::fs::File::options().write(true).open("log") { - truncate_to_size(&mut file, 0)?; - } - - Ok(()) -} diff --git a/scheduler/tests/simulation/logging.rs b/scheduler/tests/simulation/logging.rs index 2a31746..f441869 100644 --- a/scheduler/tests/simulation/logging.rs +++ b/scheduler/tests/simulation/logging.rs @@ -23,7 +23,7 @@ fn logfile_is_cleared_after_sent() -> std::io::Result<()> { std::thread::sleep(std::time::Duration::from_millis(100)); let log_metadata = std::fs::metadata("./tests/tmp/log_is_cleared_after_sent/log")?; - assert!(log_metadata.len() < 50, "Logfile is not empty"); + assert!(log_metadata.len() < 100, "Logfile is not empty"); Ok(()) } diff --git a/scheduler/tests/software_tests/execute_program.rs b/scheduler/tests/software_tests/execute_program.rs index 4b3c8f8..0c23577 100644 --- a/scheduler/tests/software_tests/execute_program.rs +++ b/scheduler/tests/software_tests/execute_program.rs @@ -1,8 +1,7 @@ -use std::io::Read; - use crate::software_tests::common; use crate::software_tests::common::ComEvent::*; use common::*; +use std::io::Read; use STS1_EDU_Scheduler::command::{self}; use STS1_EDU_Scheduler::communication::CEPPacket::*; @@ -22,10 +21,8 @@ fn execute_program_normal() -> TestResult { assert!(com.is_complete()); std::thread::sleep(std::time::Duration::from_millis(500)); - let mut res = String::new(); - std::fs::File::open("./archives/1/results/0")?.read_to_string(&mut res)?; - - assert_eq!(res.replace('\r', ""), *"Some test results\nWith multiple lines\n".to_string()); + let result_file = std::fs::read("./data/1_0")?; + assert!(result_file.windows(38).any(|w| w == b"Some test results\nWith multiple lines\n")); common::cleanup("1"); Ok(())