diff options
author | Teddy Wing | 2017-11-14 23:46:27 +0100 |
---|---|---|
committer | Teddy Wing | 2017-11-14 23:46:27 +0100 |
commit | f9b13604484fa7924efb8cde0dd26de0276065ff (patch) | |
tree | c30b02f2fdade2465f5a6756ad6dde44ca1af62a | |
parent | d5f33a9a986f21c8b660eb1d3faa9953f2b8bba7 (diff) | |
download | kipper-f9b13604484fa7924efb8cde0dd26de0276065ff.tar.bz2 |
find_and_track_build_and_update_status(): Move thread outside function
Instead of wrapping the inside of the function in a thread, put the
thread on the outside, in `main()`. This is an effort to try to get
around the `move`/lifetime issues I was having with the previous
version.
Add some extra logging so we have a better idea what's going on when.
Remove the `expects` I had added and put the `Result` returns back.
In `main()`, wrap the call to `find_and_track_build_and_update_status()`
in a thread and sleep before calling it. This allows us to give Jenkins
some time to warm up and create a job for our commit before we go ahead
and try to request it from its API. Otherwise, if we kick off the
Jenkins fetch too soon, our statuses aren't going to get updated because
the job won't have been created and thus won't have been found on the
`get_jobs()` call.
Had to add some ugly `clone()`s to get around `move` compiler errors
here. Next step is figuring out how to clean that up.
-rw-r--r-- | src/jenkins.rs | 57 | ||||
-rw-r--r-- | src/main.rs | 40 |
2 files changed, 54 insertions, 43 deletions
diff --git a/src/jenkins.rs b/src/jenkins.rs index d507f00..d61e7d0 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -43,7 +43,6 @@ extern crate reqwest; extern crate url; use std::error::Error; -use std::thread; use std::thread::sleep; use std::time::{Duration, Instant}; @@ -93,46 +92,46 @@ impl Job { pub fn find_and_track_build_and_update_status( commit_ref: CommitRef, - jenkins_url: String, - jenkins_user_id: &String, - jenkins_token: &String, - github_token: String, -// ) -> Result<(), Box<Error>> { -// ) -> thread::JoinHandle { -) -> thread::JoinHandle<()> { - thread::spawn(move || { - // Wait for Jenkins to warm up - sleep(Duration::from_secs(10)); + // jenkins_url: String, + // jenkins_user_id: &String, + // jenkins_token: &String, + // github_token: String, + jenkins_url: &str, + jenkins_user_id: &str, + jenkins_token: &str, + github_token: &str, +) -> Result<(), Box<Error>> { + // TODO: Remove this, rest should take same type as arguments maybe + let jenkins_url = jenkins_url.to_owned(); + let jenkins_user_id = &jenkins_user_id.to_owned(); + let jenkins_token = &jenkins_token.to_owned(); + let github_token = github_token.to_owned(); let jenkins_client = jenkins_request_client( &jenkins_user_id, &jenkins_token - ).expect("Failed to initialise Jenkins HTTP client"); + )?; let jobs = get_jobs( &jenkins_url, &jenkins_client, commit_ref.repo.as_ref() - ).expect( - format!( - "Failed to request Jenkins jobs for {}", - commit_ref.repo - ).as_ref() - ); + )?; let t20_minutes = 60 * 20; for job_url in jobs { + info!("Looking for job: {}", job_url); + let mut job = request_job( &jenkins_url, &jenkins_client, job_url.as_ref() - ).expect( - format!("Failed to request Jenkins job {}", job_url).as_ref() - ); + )?; // Does `displayName` match if job_for_commit(&job, &commit_ref) { - thread::spawn(move || { + info!("Job found: {}", job_url); + // thread::spawn(move || { // Start timer let now = Instant::now(); @@ -165,6 +164,8 @@ pub fn find_and_track_build_and_update_status( // call github::update_commit_status // stop + info!("Waiting for job to finish"); + if now.elapsed().as_secs() == t20_minutes { github::update_commit_status( &github_token, @@ -182,7 +183,7 @@ pub fn find_and_track_build_and_update_status( ).as_ref() ); - return + return Ok(()) } sleep(Duration::from_secs(30)); @@ -212,20 +213,18 @@ pub fn find_and_track_build_and_update_status( ).as_ref() ); - return + return Ok(()) } job = updated_job; } - }); + // }); - // return Ok(()) - return + return Ok(()) } } - }) - // Ok(()) + Ok(()) } pub fn auth_credentials(user_id: String, token: String) -> header::Basic { diff --git a/src/main.rs b/src/main.rs index 05ca775..c93d616 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,9 @@ extern crate rouille; extern crate kipper; use std::env; +use std::thread; use std::io::Read; +use std::time::Duration; use getopts::Options; @@ -137,20 +139,30 @@ fn main() { }, }; - match jenkins::find_and_track_build_and_update_status( - commit_ref, - jenkins_url.clone(), - &jenkins_user_id, - &jenkins_token, - github_token.clone(), - ) { - Ok(_) => {}, - Err(e) => { - error!("{}", e.to_string()); - - return internal_server_error() - }, - }; + // TODO: Get rid of clones + let jenkins_url = jenkins_url.clone(); + let jenkins_user_id = jenkins_user_id.clone(); + let jenkins_token = jenkins_token.clone(); + let github_token = github_token.clone(); + + thread::spawn(move || { + thread::sleep(Duration::from_secs(30)); + + match jenkins::find_and_track_build_and_update_status( + commit_ref, + &jenkins_url, + &jenkins_user_id, + &jenkins_token, + &github_token, + ) { + Ok(_) => {}, + Err(e) => { + error!("{}", e.to_string()); + + // return internal_server_error() + }, + }; + }); rouille::Response::text("202 Accepted") .with_status_code(202) |