diff --git a/v2/robotmk/Cargo.lock b/v2/robotmk/Cargo.lock index 308fd88b..56d8192e 100644 --- a/v2/robotmk/Cargo.lock +++ b/v2/robotmk/Cargo.lock @@ -271,15 +271,6 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" -[[package]] -name = "clokwerk" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd108d365fcb6d7eddf17a6718eb6a33db18ba4178f8cc6b667f480710f10d76" -dependencies = [ - "chrono", -] - [[package]] name = "colorchoice" version = "1.0.0" @@ -817,7 +808,6 @@ dependencies = [ "camino", "chrono", "clap", - "clokwerk", "ctrlc", "flexi_logger", "fs4", diff --git a/v2/robotmk/Cargo.toml b/v2/robotmk/Cargo.toml index 8be929b1..973394c9 100644 --- a/v2/robotmk/Cargo.toml +++ b/v2/robotmk/Cargo.toml @@ -11,7 +11,6 @@ base64 = "*" camino = { version = "1.1.6", features = ["serde1"] } chrono = "0.4.31" clap = { version = "*", features = ["derive"] } -clokwerk = "*" ctrlc = "*" flexi_logger = "*" fs4 = "0.7.0" diff --git a/v2/robotmk/src/bin/scheduler/internal_config.rs b/v2/robotmk/src/bin/scheduler/internal_config.rs index 563043aa..a91d3c08 100644 --- a/v2/robotmk/src/bin/scheduler/internal_config.rs +++ b/v2/robotmk/src/bin/scheduler/internal_config.rs @@ -26,7 +26,7 @@ pub struct Suite { pub id: String, pub working_directory: Utf8PathBuf, pub results_file: Utf8PathBuf, - pub execution_interval_seconds: u32, + pub execution_interval_seconds: u64, pub timeout: u64, pub robot: Robot, pub environment: Environment, diff --git a/v2/robotmk/src/bin/scheduler/results.rs b/v2/robotmk/src/bin/scheduler/results.rs index c6d1b198..2617a61e 100644 --- a/v2/robotmk/src/bin/scheduler/results.rs +++ b/v2/robotmk/src/bin/scheduler/results.rs @@ -145,7 +145,7 @@ pub struct RebotResult { #[derive(Serialize)] pub struct AttemptsConfig { - pub interval: u32, + pub interval: u64, pub timeout: u64, pub n_attempts_max: usize, } diff --git a/v2/robotmk/src/bin/scheduler/scheduling/scheduler.rs b/v2/robotmk/src/bin/scheduler/scheduling/scheduler.rs index 8be420a2..e7debf7e 100644 --- a/v2/robotmk/src/bin/scheduler/scheduling/scheduler.rs +++ b/v2/robotmk/src/bin/scheduler/scheduling/scheduler.rs @@ -4,45 +4,54 @@ use crate::internal_config::{GlobalConfig, Suite}; use crate::logging::log_and_return_error; use anyhow::{bail, Result}; -use clokwerk::{Scheduler, TimeUnits}; use log::error; -use std::thread::{sleep, spawn}; +use std::thread::sleep; use std::time::Duration; use tokio::task::spawn_blocking; +use tokio::time::interval; -pub fn run_suites_and_cleanup(global_config: &GlobalConfig, suites: &[Suite]) -> Result<()> { - let mut scheduler = Scheduler::new(); +#[tokio::main] +pub async fn run_suites_and_cleanup(global_config: &GlobalConfig, suites: &[Suite]) -> Result<()> { let suites_for_scheduling: Vec = suites.to_vec(); for suite in suites_for_scheduling { - scheduler - .every(suite.execution_interval_seconds.seconds()) - .run(move || run_suite_in_new_thread(suite.clone())); + tokio::spawn(run_scheduler(suite)); } - let suites_for_cleanup: Vec = suites.to_vec(); - scheduler - .every(5.minutes()) - .run(move || run_cleanup_working_directories_in_new_thread(suites_for_cleanup.clone())); + tokio::spawn(run_cleanup_job(suites.to_vec())); - loop { - if global_config.cancellation_token.is_cancelled() { - error!("Received termination signal while scheduling, waiting for suites to terminate"); - wait_until_all_suites_have_terminated(suites); - bail!("Terminated"); - } - scheduler.run_pending(); - sleep(Duration::from_millis(250)); - } + global_config.cancellation_token.cancelled().await; + error!("Received termination signal while scheduling, waiting for suites to terminate"); + wait_until_all_suites_have_terminated(suites); + bail!("Terminated"); } -#[tokio::main] -async fn run_suite_in_new_thread(suite: Suite) { - spawn_blocking(move || run_suite(&suite).map_err(log_and_return_error)); +async fn run_scheduler(suite: Suite) { + let mut clock = interval(Duration::from_secs(suite.execution_interval_seconds)); + loop { + let suite = suite.clone(); + tokio::select! { + _ = clock.tick() => { } + _ = suite.cancellation_token.cancelled() => { return } + }; + spawn_blocking(move || run_suite(&suite).map_err(log_and_return_error)); + } } -fn run_cleanup_working_directories_in_new_thread(suites: Vec) { - spawn(move || cleanup_working_directories(suites.iter())); +async fn run_cleanup_job(suites: Vec) { + let mut clock = interval(Duration::from_secs(300)); + let token = suites + .first() + .map(|s| s.cancellation_token.clone()) + .unwrap(); + loop { + let suites = suites.clone(); + tokio::select! { + _ = clock.tick() => { } + _ = token.cancelled() => { return } + }; + spawn_blocking(move || cleanup_working_directories(suites.iter())); + } } fn wait_until_all_suites_have_terminated(suites: &[Suite]) { diff --git a/v2/robotmk/src/config.rs b/v2/robotmk/src/config.rs index 27e59965..686d42e0 100644 --- a/v2/robotmk/src/config.rs +++ b/v2/robotmk/src/config.rs @@ -38,7 +38,7 @@ pub struct RobotFrameworkConfig { pub struct ExecutionConfig { pub n_attempts_max: usize, pub retry_strategy: RetryStrategy, - pub execution_interval_seconds: u32, + pub execution_interval_seconds: u64, pub timeout: u64, }