Skip to content

Commit

Permalink
Rust scheduler: Implement setup step upon startup
Browse files Browse the repository at this point in the history
CMK-14522
  • Loading branch information
jherbel committed Sep 21, 2023
1 parent 156c16c commit 81fe93e
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 8 deletions.
83 changes: 80 additions & 3 deletions v2/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions v2/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
anyhow = { version = "*", features = ["backtrace"] }
atomicwrites = "*"
chrono = "0.4.31"
clap = { version = "*", features = ["derive"] }
flexi_logger = "*"
Expand Down
4 changes: 2 additions & 2 deletions v2/rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ impl Config {
}

#[cfg(test)]
mod tests {
pub mod tests {
use super::*;

fn create_suite_config(suite_name: &str) -> SuiteConfig {
pub fn create_suite_config(suite_name: &str) -> SuiteConfig {
SuiteConfig {
robot_framework_config: RobotFrameworkConfig {
robot_target: PathBuf::from(format!("/suite/{}/tasks.robot", suite_name)),
Expand Down
5 changes: 5 additions & 0 deletions v2/rust/src/environment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::path::{Path, PathBuf};

pub fn environment_building_stdio_directory(working_directory: &Path) -> PathBuf {
working_directory.join("environment_building_stdio")
}
14 changes: 11 additions & 3 deletions v2/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
pub mod attempt;
mod cli;
mod config;
mod environment;
mod logging;
pub mod parse_xml;
mod results;
mod setup;

use anyhow::Context;
use anyhow::{Context, Result};
use clap::Parser;
use log::{debug, info};
use logging::log_and_return_error;

fn main() -> anyhow::Result<()> {
fn main() -> Result<()> {
let args = cli::Args::parse();
logging::init(args.log_specification(), &args.log_path)?;
info!("Program started and logging set up");

let _config = config::load(&args.config_path)
let conf = config::load(&args.config_path)
.context("Configuration loading failed")
.map_err(log_and_return_error)?;
debug!("Configuration loaded");

setup::setup(&conf)
.context("Setup failed")
.map_err(log_and_return_error)?;
debug!("Setup completed");

Ok(())
}
9 changes: 9 additions & 0 deletions v2/rust/src/results.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::path::{Path, PathBuf};

pub fn suite_results_directory(results_directory: &Path) -> PathBuf {
results_directory.join("suites")
}

pub fn suite_result_file(suite_results_dir: &Path, suite_name: &str) -> PathBuf {
suite_results_dir.join(format!("{}.json", suite_name))
}
78 changes: 78 additions & 0 deletions v2/rust/src/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use anyhow::{Context, Result};
use atomicwrites::replace_atomic;
use std::collections::HashSet;
use std::fs::{create_dir_all, read_dir, remove_file};
use std::path::{Path, PathBuf};

use super::config::Config;
use super::environment::environment_building_stdio_directory;
use super::results::{suite_result_file, suite_results_directory};

pub fn setup(config: &Config) -> Result<()> {
create_dir_all(&config.working_directory).context("Failed to create working directory")?;
create_dir_all(environment_building_stdio_directory(
&config.working_directory,
))
.context("Failed to create environment building stdio directory")?;
create_dir_all(&config.results_directory).context("Failed to create results directory")?;
create_dir_all(suite_results_directory(&config.results_directory))
.context("Failed to create suite results directory")?;
clean_up_results_directory_atomic(config)
}

fn clean_up_results_directory_atomic(config: &Config) -> Result<()> {
let suite_results_directory = suite_results_directory(&config.results_directory);
let result_files_to_keep = config
.suites()
.into_iter()
.map(|(suite_name, _suite_config)| suite_result_file(&suite_results_directory, suite_name));
let currently_present_result_files = currently_present_result_files(&suite_results_directory)?;
remove_files_atomic(
&suite_results_directory.join("deprecated_result"),
HashSet::<PathBuf>::from_iter(currently_present_result_files)
.difference(&HashSet::from_iter(result_files_to_keep)),
)
}

fn currently_present_result_files(suite_results_directory: &Path) -> Result<Vec<PathBuf>> {
let mut result_files = vec![];

for dir_entry in read_dir(suite_results_directory).context(format!(
"Failed to read entries of results directory {}",
suite_results_directory.display()
))? {
let dir_entry = dir_entry.context(format!(
"Failed to read entries of results directory {}",
suite_results_directory.display()
))?;
if dir_entry
.file_type()
.context(format!(
"Failed to determine file type of {}",
dir_entry.path().display()
))?
.is_file()
{
result_files.push(dir_entry.path())
}
}

Ok(result_files)
}

fn remove_files_atomic<'a>(
intermediate_path_for_move: &Path,
files_to_be_removed: impl Iterator<Item = &'a PathBuf>,
) -> Result<()> {
for path in files_to_be_removed {
replace_atomic(path, intermediate_path_for_move).context(format!(
"Failed to move {} to {}",
path.display(),
intermediate_path_for_move.display()
))?;
}

let _ = remove_file(intermediate_path_for_move);

Ok(())
}

0 comments on commit 81fe93e

Please sign in to comment.