Skip to content

Commit

Permalink
Fixed a hang-bug, added CICD mode !capture ctrlc
Browse files Browse the repository at this point in the history
  • Loading branch information
msmith-techempower committed Jan 7, 2021
1 parent 2d1ded6 commit 26cb5c6
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tfb_toolset"
version = "0.1.0"
version = "0.4.2"
authors = ["Mike Smith", "Nate Brady"]
edition = "2018"

Expand Down
92 changes: 45 additions & 47 deletions src/benchmarker.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::benchmarker::modes::CICD;
use crate::config::{Framework, Named, Project, Test};
use crate::docker::container::{
create_benchmarker_container, create_container, create_verifier_container,
Expand Down Expand Up @@ -34,6 +35,7 @@ use std::{thread, time};
pub mod modes {
pub const BENCHMARK: &str = "benchmark";
pub const VERIFY: &str = "verify";
pub const CICD: &str = "cicd";
pub const DEBUG: &str = "debug";
}

Expand Down Expand Up @@ -68,7 +70,7 @@ pub struct Benchmarker<'a> {
}

impl<'a> Benchmarker<'a> {
pub fn new(docker_config: DockerConfig<'a>, projects: Vec<Project>) -> Self {
pub fn new(docker_config: DockerConfig<'a>, projects: Vec<Project>, mode: &str) -> Self {
let application_container_id = Arc::new(Mutex::new(DockerContainerIdFuture::new(
&docker_config.server_docker_host,
)));
Expand All @@ -92,37 +94,39 @@ impl<'a> Benchmarker<'a> {
ctrlc_received: Arc::new(AtomicBool::new(false)),
};

let use_unix_socket = benchmarker.docker_config.use_unix_socket;
let application_container_id = Arc::clone(&benchmarker.application_container_id);
let database_container_id = Arc::clone(&benchmarker.database_container_id);
let verifier_container_id = Arc::clone(&benchmarker.verifier_container_id);
let benchmarker_container_id = Arc::clone(&benchmarker.benchmarker_container_id);
let ctrlc_received = Arc::clone(&benchmarker.ctrlc_received);
ctrlc::set_handler(move || {
let logger = Logger::default();
logger.log("Shutting down (may take a moment)").unwrap();
if ctrlc_received.load(Ordering::Acquire) {
logger
.log("Exiting immediately (there may still be running containers to stop)")
.unwrap();
std::process::exit(0);
} else {
let application_container_id = Arc::clone(&application_container_id);
let database_container_id = Arc::clone(&database_container_id);
let verifier_container_id = Arc::clone(&verifier_container_id);
let benchmarker_container_id = Arc::clone(&benchmarker_container_id);
let ctrlc_received = Arc::clone(&ctrlc_received);
thread::spawn(move || {
ctrlc_received.store(true, Ordering::Release);
stop_docker_container_future(use_unix_socket, &verifier_container_id);
stop_docker_container_future(use_unix_socket, &benchmarker_container_id);
stop_docker_container_future(use_unix_socket, &application_container_id);
stop_docker_container_future(use_unix_socket, &database_container_id);
if mode != CICD {
let use_unix_socket = benchmarker.docker_config.use_unix_socket;
let application_container_id = Arc::clone(&benchmarker.application_container_id);
let database_container_id = Arc::clone(&benchmarker.database_container_id);
let verifier_container_id = Arc::clone(&benchmarker.verifier_container_id);
let benchmarker_container_id = Arc::clone(&benchmarker.benchmarker_container_id);
let ctrlc_received = Arc::clone(&benchmarker.ctrlc_received);
ctrlc::set_handler(move || {
let logger = Logger::default();
logger.log("Shutting down (may take a moment)").unwrap();
if ctrlc_received.load(Ordering::Acquire) {
logger
.log("Exiting immediately (there may still be running containers to stop)")
.unwrap();
std::process::exit(0);
});
}
})
.unwrap();
} else {
let application_container_id = Arc::clone(&application_container_id);
let database_container_id = Arc::clone(&database_container_id);
let verifier_container_id = Arc::clone(&verifier_container_id);
let benchmarker_container_id = Arc::clone(&benchmarker_container_id);
let ctrlc_received = Arc::clone(&ctrlc_received);
thread::spawn(move || {
ctrlc_received.store(true, Ordering::Release);
stop_docker_container_future(use_unix_socket, &verifier_container_id);
stop_docker_container_future(use_unix_socket, &benchmarker_container_id);
stop_docker_container_future(use_unix_socket, &application_container_id);
stop_docker_container_future(use_unix_socket, &database_container_id);
std::process::exit(0);
});
}
})
.unwrap();
}

benchmarker
}
Expand Down Expand Up @@ -774,30 +778,24 @@ impl<'a> Benchmarker<'a> {
}
}

match self.docker_config.server_host {
"tfb-server" => easy.url(&format!("http://localhost:{}{}", host_port, endpoint))?,
_ => easy.url(&format!(
let url = match self.docker_config.server_host {
"tfb-server" => format!("http://localhost:{}{}", host_port, endpoint),
_ => format!(
"http://{}:{}{}",
&self.docker_config.server_host, host_port, endpoint
))?,
),
};
easy.url(&url)?;
easy.timeout(time::Duration::from_secs(1))?;
let _ = easy.perform();

match easy.response_code() {
Ok(code) => {
if code > 0 {
return Ok(());
} else {
slept_for += 1;
thread::sleep(Duration::from_secs(1));
}
}
_ => {
slept_for += 1;
thread::sleep(Duration::from_secs(1));
if let Ok(code) = easy.response_code() {
if code > 0 {
return Ok(());
}
}
slept_for += 1;
thread::sleep(Duration::from_secs(1));
}
}
}
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub fn run() -> ToolsetResult<()> {
} else if let Some(mode) = matches.value_of(options::args::MODE) {
let docker_config = DockerConfig::new(&matches);
let projects = metadata::list_projects_to_run(&matches);
let mut benchmarker = Benchmarker::new(docker_config, projects);
let mut benchmarker = Benchmarker::new(docker_config, projects, mode);
match mode {
modes::BENCHMARK => benchmarker.benchmark(),
modes::VERIFY => benchmarker.verify(),
modes::VERIFY | modes::CICD => benchmarker.verify(),
modes::DEBUG => benchmarker.debug(),
_ => Err(UnknownBenchmarkerModeError(mode.to_string())),
}
Expand Down
35 changes: 28 additions & 7 deletions src/docker/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::docker::{
};
use crate::error::ToolsetError::{
ContainerPortMappingInspectionError, ExposePortError, FailedBenchmarkCommandRetrievalError,
VerificationFailedException,
};
use crate::error::ToolsetResult;
use crate::io::Logger;
Expand All @@ -29,6 +30,7 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::task::Poll;
use std::thread;
use std::thread::sleep;
use std::time::Duration;

/// Note: this function makes the assumption that the image is already
Expand Down Expand Up @@ -367,14 +369,33 @@ pub fn start_verification_container(
docker_config.use_unix_socket,
Simple::new(),
)?;
let verifier = attach_to_container(
&container_id,
&docker_config.client_docker_host,
docker_config.use_unix_socket,
Verifier::new(project, test, test_type, logger),
)?;

Ok(verifier.verification)
let mut running = true;
let mut slept = 0;
let max_sleep = 10;
while running && slept < max_sleep {
let inspection = inspect_container(
&container_id,
&docker_config.client_docker_host,
docker_config.use_unix_socket,
Simple::new(),
)?;
running = inspection.state.running;
slept += 1;
sleep(Duration::from_secs(1));
}
if !running {
let verification = get_container_logs(
&container_id,
&docker_config.client_docker_host,
docker_config.use_unix_socket,
Verifier::new(project, test, test_type, logger),
)?;

return Ok(verification.verification);
}

Err(VerificationFailedException)
}

/// Polls until `container` is ready with either some `container_id` or `None`,
Expand Down
2 changes: 1 addition & 1 deletion src/docker/listener/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::io::Logger;
use curl::easy::{Handler, WriteError};
use serde::Deserialize;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Verifier {
pub verification: Verification,
logger: Logger,
Expand Down
2 changes: 1 addition & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn parse<'app>() -> App<'app> {
.long("mode")
.short('m')
.takes_value(true)
.possible_values(&[modes::BENCHMARK, modes::VERIFY, modes::DEBUG])
.possible_values(&[modes::BENCHMARK, modes::VERIFY, modes::CICD, modes::DEBUG])
)
.arg(
Arg::with_name(args::LIST_FRAMEWORKS)
Expand Down

0 comments on commit 26cb5c6

Please sign in to comment.