diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-11-24 14:42:34 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-11-24 14:42:34 +0200 |
commit | 88941483712cc22c8a485945315b785896e14c1a (patch) | |
tree | 9b5297bbe573a7b18bbfe59186edad6bd4810159 | |
parent | 2b8d777f1039598335240bdac3f9c5f98e3d2cc6 (diff) | |
download | native-ci-simulation-88941483712cc22c8a485945315b785896e14c1a.tar.gz |
logging, don't panic if HTTP request fails
Signed-off-by: Lars Wirzenius <liw@liw.fi>
-rw-r--r-- | Cargo.lock | 98 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 18 |
3 files changed, 116 insertions, 2 deletions
@@ -18,6 +18,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] name = "async-trait" version = "0.1.74" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -169,6 +178,19 @@ dependencies = [ ] [[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] name = "errno" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -345,6 +367,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] name = "hyper" version = "0.14.27" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -408,6 +436,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" [[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + +[[package]] name = "itoa" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -489,6 +528,8 @@ name = "native-ci-simulation" version = "0.1.0" dependencies = [ "axum", + "log", + "pretty_env_logger", "rand", "reqwest", "serde", @@ -633,6 +674,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] +name = "pretty_env_logger" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c" +dependencies = [ + "env_logger", + "log", +] + +[[package]] name = "proc-macro2" version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -690,6 +741,35 @@ dependencies = [ ] [[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] name = "reqwest" version = "0.11.22" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -924,6 +1004,15 @@ dependencies = [ ] [[package]] +name = "termcolor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + +[[package]] name = "tinyvec" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1190,6 +1279,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -5,6 +5,8 @@ edition = "2021" [dependencies] axum = "0.6.20" +log = "0.4.20" +pretty_env_logger = "0.5.0" rand = "0.8.5" reqwest = { version = "0.11.22", features = ["blocking"] } serde = { version = "1.0.192", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 7ebac60..7549c93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use axum::{ routing::get, Json, Router, }; +use log::{debug, info}; use serde::Serialize; const SUCCESS: &str = "http://127.0.0.1:9000/succeeded"; @@ -21,16 +22,21 @@ const FAIL: &str = "http://127.0.0.1:9000/failed"; #[tokio::main] async fn main() { + pretty_env_logger::init(); + info!("native CI simulator starts"); + let ci = CiState::default(); let cloned = ci.clone(); std::thread::spawn(move || report_finished(cloned)); + debug!("spawned thread to simulate running jobs"); let app = Router::new() .route("/", get(go_away)) .route("/trigger/:commit", get(trigger_run)) .with_state(ci); + debug!("starting to server requests"); axum::Server::bind(&"127.0.0.1:8000".parse().unwrap()) .serve(app.into_make_service()) .await @@ -38,6 +44,7 @@ async fn main() { } async fn go_away() -> (StatusCode, impl IntoResponse) { + info!("request for / denied"); (StatusCode::FORBIDDEN, ()) } @@ -60,6 +67,8 @@ async fn trigger_run( let id = *next; *next += 1; + info!("CI run for commit {} triggered: {}", commit, id); + ( StatusCode::CREATED, Json(Run { @@ -70,6 +79,7 @@ async fn trigger_run( } fn report_finished(ci: CiState) { + debug!("this is the thread for reporting CI runs finishing"); let mut reported = 0; loop { sleep(Duration::from_secs(3)); @@ -80,8 +90,12 @@ fn report_finished(ci: CiState) { } else { format!("{}/{}", FAIL, reported) }; - println!("URL: {}", url); - reqwest::blocking::get(&url).expect("reporting works"); + info!("CI run finished: {}", url); + if let Err(e) = reqwest::blocking::get(&url) { + info!("FAILED to report CI run finished: {}", e); + } else { + info!("reported that CI run finished: {}", url); + } reported += 1; } } |