Skip to content

Commit

Permalink
Delete individual result files immediately after packing
Browse files Browse the repository at this point in the history
Closes: #153
  • Loading branch information
Florian Guggi authored and florg-32 committed Oct 9, 2024
1 parent 309195b commit 8f17cf2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 40 deletions.
11 changes: 0 additions & 11 deletions scheduler/src/command/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions scheduler/src/command/execute_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
21 changes: 2 additions & 19 deletions scheduler/src/command/return_result.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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) =
Expand All @@ -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(())
}
2 changes: 1 addition & 1 deletion scheduler/tests/simulation/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
9 changes: 3 additions & 6 deletions scheduler/tests/software_tests/execute_program.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -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(())
Expand Down

0 comments on commit 8f17cf2

Please sign in to comment.