Skip to content

Commit

Permalink
toII
Browse files Browse the repository at this point in the history
  • Loading branch information
SoloJacobs committed Dec 5, 2023
1 parent 3e5709a commit 4428ba9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
10 changes: 0 additions & 10 deletions v2/robotmk/Cargo.lock

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

1 change: 0 additions & 1 deletion v2/robotmk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion v2/robotmk/src/bin/scheduler/internal_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion v2/robotmk/src/bin/scheduler/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
59 changes: 34 additions & 25 deletions v2/robotmk/src/bin/scheduler/scheduling/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Suite> = 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<Suite> = 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<Suite>) {
spawn(move || cleanup_working_directories(suites.iter()));
async fn run_cleanup_job(suites: Vec<Suite>) {
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]) {
Expand Down
2 changes: 1 addition & 1 deletion v2/robotmk/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down

0 comments on commit 4428ba9

Please sign in to comment.