Skip to content

Commit 8c4bce6

Browse files
committed
Do not remove all plan working directories at startup
Instead, only remove the working directories of plans that are no longer configured. The working directories of configured plans are cleaned up regularly and shouldn't be removed at startup. SUP-23960
1 parent 52a8121 commit 8c4bce6

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

src/bin/scheduler/setup/general.rs

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ use std::collections::{HashMap, HashSet};
1414
use std::fs::{create_dir_all, remove_dir_all, remove_file};
1515

1616
pub fn setup(global_config: &GlobalConfig, plans: Vec<Plan>) -> AnyhowResult<Vec<Plan>> {
17-
if global_config.working_directory.exists() {
18-
remove_dir_all(&global_config.working_directory)
19-
.context("Failed to remove working directory")?;
20-
}
2117
create_dir_all(&global_config.working_directory)
2218
.context("Failed to create working directory")?;
2319
setup_results_directories(global_config, &plans)?;
@@ -31,6 +27,8 @@ fn setup_working_directories(
3127
global_config: &GlobalConfig,
3228
plans: Vec<Plan>,
3329
) -> AnyhowResult<Vec<Plan>> {
30+
clean_up_working_directories(global_config, &plans)?;
31+
3432
let (surviving_plans, plan_failures) = setup_plans_working_directory(plans);
3533
let (surviving_plans, rcc_failures) =
3634
setup_rcc_working_directories(&global_config.working_directory, surviving_plans);
@@ -49,6 +47,33 @@ fn setup_working_directories(
4947
Ok(surviving_plans)
5048
}
5149

50+
fn clean_up_working_directories(global_config: &GlobalConfig, plans: &[Plan]) -> AnyhowResult<()> {
51+
for dir_to_be_removed in [
52+
environment_building_working_directory(&global_config.working_directory),
53+
rcc_setup_working_directory(&global_config.working_directory),
54+
] {
55+
if dir_to_be_removed.exists() {
56+
remove_dir_all(dir_to_be_removed)?;
57+
}
58+
}
59+
60+
let plan_working_directory = &global_config.working_directory.join("plans");
61+
if !plan_working_directory.is_dir() {
62+
return Ok(());
63+
}
64+
65+
let plan_working_directories_to_keep =
66+
HashSet::<&Utf8PathBuf>::from_iter(plans.iter().map(|plan| &plan.working_directory));
67+
68+
for entry in top_level_directories(plan_working_directory)? {
69+
if !plan_working_directories_to_keep.contains(&entry) {
70+
remove_dir_all(&entry)?;
71+
}
72+
}
73+
74+
Ok(())
75+
}
76+
5277
fn setup_plans_working_directory(plans: Vec<Plan>) -> (Vec<Plan>, HashMap<String, String>) {
5378
let mut surviving_plans = Vec::new();
5479
let mut failures = HashMap::new();
@@ -235,26 +260,34 @@ fn clean_up_plan_results_directory(
235260
Ok(())
236261
}
237262

238-
fn top_level_files(directory: &Utf8Path) -> AnyhowResult<Vec<Utf8PathBuf>> {
239-
let mut result_files = vec![];
263+
fn top_level_directories(directory: &Utf8Path) -> anyhow::Result<Vec<Utf8PathBuf>> {
264+
Ok(top_level_directory_entries(directory)?
265+
.into_iter()
266+
.filter(|path| path.is_dir())
267+
.collect())
268+
}
240269

241-
for dir_entry in directory.read_dir_utf8().context(format!(
242-
"Failed to read entries of results directory {directory}",
243-
))? {
244-
let dir_entry = dir_entry.context(format!(
245-
"Failed to read entries of results directory {directory}",
246-
))?;
247-
if dir_entry
248-
.file_type()
249-
.context(format!(
250-
"Failed to determine file type of {}",
251-
dir_entry.path()
252-
))?
253-
.is_file()
254-
{
255-
result_files.push(dir_entry.path().to_path_buf())
256-
}
270+
fn top_level_files(directory: &Utf8Path) -> anyhow::Result<Vec<Utf8PathBuf>> {
271+
Ok(top_level_directory_entries(directory)?
272+
.into_iter()
273+
.filter(|path| path.is_file())
274+
.collect())
275+
}
276+
277+
fn top_level_directory_entries(directory: &Utf8Path) -> anyhow::Result<Vec<Utf8PathBuf>> {
278+
let mut entries = vec![];
279+
280+
for dir_entry in directory
281+
.read_dir_utf8()
282+
.context(format!("Failed to read entries of directory {directory}",))?
283+
{
284+
entries.push(
285+
dir_entry
286+
.context(format!("Failed to read entries of directory {directory}",))?
287+
.path()
288+
.to_path_buf(),
289+
)
257290
}
258291

259-
Ok(result_files)
292+
Ok(entries)
260293
}

tests/test_scheduler.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ use walkdir::WalkDir;
2222
#[ignore]
2323
async fn test_scheduler() -> AnyhowResult<()> {
2424
let test_dir = Utf8PathBuf::from(var("TEST_DIR")?);
25+
let unconfigured_plan_working_dir = test_dir
26+
.join("working")
27+
.join("plans")
28+
.join("should_be_removed_during_scheduler_setup");
29+
let configured_plan_previous_execution_dir = test_dir
30+
.join("working")
31+
.join("plans")
32+
.join("rcc_headless")
33+
.join("should_still_exist_after_scheduler_run");
2534
create_dir_all(&test_dir)?;
35+
create_dir_all(&unconfigured_plan_working_dir)?;
36+
create_dir_all(&configured_plan_previous_execution_dir)?;
2637
let current_user_name = var("UserName")?;
2738
let config = create_config(
2839
&test_dir,
@@ -39,6 +50,8 @@ async fn test_scheduler() -> AnyhowResult<()> {
3950
run_scheduler(&test_dir, &config, var("RUN_FOR")?.parse::<u64>()?).await?;
4051

4152
assert_working_directory(&config.working_directory, &current_user_name).await?;
53+
assert!(!unconfigured_plan_working_dir.exists());
54+
assert!(configured_plan_previous_execution_dir.is_dir());
4255
assert_results_directory(&config.results_directory);
4356
assert_rcc(&config.rcc_config, &current_user_name).await?;
4457
assert_tasks().await?;

0 commit comments

Comments
 (0)