Skip to content

Commit

Permalink
Remove working directories of plans that are not configured anymore
Browse files Browse the repository at this point in the history
CMK-18459
  • Loading branch information
jherbel committed Aug 23, 2024
1 parent 5cb3fb9 commit eb6f1df
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 38 deletions.
10 changes: 6 additions & 4 deletions src/bin/scheduler/internal_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use robotmk::rf::robot::Robot;
use robotmk::section::Host;
use robotmk::session::Session;

use camino::Utf8PathBuf;
use camino::{Utf8Path, Utf8PathBuf};
use tokio_util::sync::CancellationToken;

pub struct GlobalConfig {
Expand Down Expand Up @@ -80,9 +80,7 @@ pub fn from_external_config(
plans.push(Plan {
id: plan_config.id.clone(),
source,
working_directory: external_config
.working_directory
.join("plans")
working_directory: plans_working_directory(&external_config.working_directory)
.join(&plan_config.id),
results_file: plan_results_directory(&external_config.results_directory)
.join(format!("{}.json", plan_config.id)),
Expand Down Expand Up @@ -155,6 +153,10 @@ pub fn sort_plans_by_grouping(plans: &mut [Plan]) {
});
}

pub fn plans_working_directory(working_directory: &Utf8Path) -> Utf8PathBuf {
working_directory.join("plans")
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
106 changes: 72 additions & 34 deletions src/bin/scheduler/setup/general.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::plans_by_sessions;
use super::rcc::rcc_setup_working_directory;
use crate::build::environment_building_working_directory;
use crate::internal_config::{sort_plans_by_grouping, GlobalConfig, Plan, Source};
use crate::internal_config::{
plans_working_directory, sort_plans_by_grouping, GlobalConfig, Plan, Source,
};

use anyhow::{anyhow, Context, Result as AnyhowResult};
use camino::{Utf8Path, Utf8PathBuf};
Expand All @@ -26,6 +28,10 @@ pub fn setup(
}
create_dir_all(&working_sub_dir)?;
}
clean_up_plan_working_directories(
&plans_working_directory(&global_config.working_directory),
&plans,
)?;
if global_config.managed_directory.exists() {
remove_dir_all(&global_config.managed_directory)?;
}
Expand All @@ -46,6 +52,23 @@ pub fn setup(
))
}

fn clean_up_plan_working_directories(
plans_working_directory: &Utf8Path,
plans: &[Plan],
) -> AnyhowResult<()> {
let plan_working_dirs_to_keep =
HashSet::<Utf8PathBuf>::from_iter(plans.iter().map(|plan| plan.working_directory.clone()));
let currently_present_plan_working_dirs =
HashSet::from_iter(top_level_directories(plans_working_directory)?);
for path in currently_present_plan_working_dirs.difference(&plan_working_dirs_to_keep) {
remove_dir_all(path)?;
}
clean_up_directory(
plans.iter().map(|plan| &plan.working_directory),
top_level_directories(plans_working_directory)?.iter(),
)
}

fn setup_working_directories(
global_config: &GlobalConfig,
plans: Vec<Plan>,
Expand Down Expand Up @@ -337,47 +360,62 @@ fn clean_up_results_directory(
for path in top_level_files(&global_config.results_directory)? {
remove_file(path)?;
}
clean_up_plan_results_directory(
&plan_results_directory(&global_config.results_directory),
plans,
clean_up_directory(
plans.iter().map(|plan| &plan.results_file),
top_level_files(&plan_results_directory(&global_config.results_directory))?.iter(),
)?;
Ok(results_directory_lock.release()?)
}

fn clean_up_plan_results_directory(
plan_results_directory: &Utf8Path,
plans: &[Plan],
) -> AnyhowResult<()> {
let result_files_to_keep =
HashSet::<Utf8PathBuf>::from_iter(plans.iter().map(|plan| plan.results_file.clone()));
let currently_present_result_files =
HashSet::<Utf8PathBuf>::from_iter(top_level_files(plan_results_directory)?);
for path in currently_present_result_files.difference(&result_files_to_keep) {
remove_file(path)?;
}
Ok(())
fn top_level_directories(directory: &Utf8Path) -> AnyhowResult<Vec<Utf8PathBuf>> {
Ok(top_level_directory_entries(directory)?
.into_iter()
.filter(|path| path.is_dir())
.collect())
}

fn top_level_files(directory: &Utf8Path) -> AnyhowResult<Vec<Utf8PathBuf>> {
let mut result_files = vec![];
Ok(top_level_directory_entries(directory)?
.into_iter()
.filter(|path| path.is_file())
.collect())
}

for dir_entry in directory.read_dir_utf8().context(format!(
"Failed to read entries of results directory {directory}",
))? {
let dir_entry = dir_entry.context(format!(
"Failed to read entries of results directory {directory}",
))?;
if dir_entry
.file_type()
.context(format!(
"Failed to determine file type of {}",
dir_entry.path()
))?
.is_file()
{
result_files.push(dir_entry.path().to_path_buf())
}
fn top_level_directory_entries(directory: &Utf8Path) -> AnyhowResult<Vec<Utf8PathBuf>> {
let mut entries = vec![];

for dir_entry in directory
.read_dir_utf8()
.context(format!("Failed to read entries of directory {directory}",))?
{
entries.push(
dir_entry
.context(format!("Failed to read entries of directory {directory}",))?
.path()
.to_path_buf(),
)
}

Ok(result_files)
Ok(entries)
}

fn clean_up_directory<P>(
entries_to_keep: impl IntoIterator<Item = P>,
currently_present_entries: impl IntoIterator<Item = P>,
) -> AnyhowResult<()>
where
P: AsRef<Utf8Path>,
P: std::cmp::Eq,
P: std::hash::Hash,
{
for entry in HashSet::<P>::from_iter(currently_present_entries)
.difference(&HashSet::from_iter(entries_to_keep))
{
if entry.as_ref().is_file() {
remove_file(entry)?
} else {
remove_dir_all(entry)?
}
}
Ok(())
}

0 comments on commit eb6f1df

Please sign in to comment.