diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-11-14 18:44:07 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-11-14 18:44:07 +0200 |
commit | 1991ca7cdb3b42cab7324eede22b79f129855a8e (patch) | |
tree | afe3b4bd18b0fe8281565b69f37ee8dabce79993 | |
parent | 9dab750f02dec6c641320dba244a491bf5137919 (diff) | |
download | native-ci-simulation-1991ca7cdb3b42cab7324eede22b79f129855a8e.tar.gz |
thread to report results
Signed-off-by: Lars Wirzenius <liw@liw.fi>
-rw-r--r-- | src/main.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index 1184454..b382070 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,16 +10,23 @@ use axum::{ }; use serde::{Deserialize, Serialize}; -use std::sync::{Arc, Mutex}; +use std::{ + sync::{Arc, Mutex}, + thread::sleep, + time::Duration, +}; #[tokio::main] async fn main() { - let counter = CiState::default(); + let ci = CiState::default(); + let cloned = ci.clone(); + + std::thread::spawn(move || report_finished(cloned)); let app = Router::new() .route("/", get(go_away)) .route("/runs", post(create_run)) - .with_state(counter); + .with_state(ci); axum::Server::bind(&"127.0.0.1:8000".parse().unwrap()) .serve(app.into_make_service()) @@ -63,3 +70,15 @@ async fn create_run( }), ) } + +fn report_finished(ci: CiState) { + let mut reported = 0; + loop { + sleep(Duration::from_secs(3)); + let next = ci.next.lock().expect("mutex was poisoned"); + if reported < *next { + println!("job {} finished", reported); + reported += 1; + } + } +} |