diff options
| -rw-r--r-- | src/jenkins.rs | 163 | ||||
| -rw-r--r-- | src/main.rs | 38 | 
2 files changed, 107 insertions, 94 deletions
| diff --git a/src/jenkins.rs b/src/jenkins.rs index 61d3bf1..273e26c 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}; @@ -111,6 +110,8 @@ pub fn find_and_track_build_and_update_status(      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, @@ -119,92 +120,94 @@ pub fn find_and_track_build_and_update_status(          // Does `displayName` match          if job_for_commit(&job, &commit_ref) { -            thread::spawn(move || { -                // Start timer -                let now = Instant::now(); - -                let commit_status = job.result.commit_status(); - -                github::update_commit_status( -                    &github_token, -                    &commit_ref, -                    &commit_status, -                    job_url.clone(), -                    None, -                    "continuous-integration/jenkins".to_owned() -                ).expect( -                    format!( -                        "GitHub pending status update failed for {}/{} {}.", -                        commit_ref.owner, -                        commit_ref.repo, -                        commit_ref.sha -                    ).as_ref() -                ); +            info!("Job found: {}", job_url); + +            // Start timer +            let now = Instant::now(); + +            let commit_status = job.result.commit_status(); + +            github::update_commit_status( +                &github_token, +                &commit_ref, +                &commit_status, +                job_url.clone(), +                None, +                "continuous-integration/jenkins".to_owned() +            ).expect( +                format!( +                    "GitHub pending status update failed for {}/{} {}.", +                    commit_ref.owner, +                    commit_ref.repo, +                    commit_ref.sha +                ).as_ref() +            ); + +            while job.result == JobStatus::Pending { +                // loop +                // if timer > 20 minutes +                //   call github::update_commit_status with timeout error +                //   return +                // wait 30 seconds +                // call request_job again +                // if the status is different +                //   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, +                        &commit_ref, +                        &github::CommitStatus::Error, +                        job_url.clone(), +                        Some("The status checker timed out.".to_owned()), +                        "continuous-integration/jenkins".to_owned() +                    ).expect( +                        format!( +                            "GitHub timeout error status update failed for {}/{} {}.", +                            commit_ref.owner, +                            commit_ref.repo, +                            commit_ref.sha +                        ).as_ref() +                    ); -                while job.result == JobStatus::Pending { -                    // loop -                    // if timer > 20 minutes -                    //   call github::update_commit_status with timeout error -                    //   return -                    // wait 30 seconds -                    // call request_job again -                    // if the status is different -                    //   call github::update_commit_status -                    //   stop - -                    if now.elapsed().as_secs() == t20_minutes { -                        github::update_commit_status( -                            &github_token, -                            &commit_ref, -                            &github::CommitStatus::Error, -                            job_url.clone(), -                            Some("The status checker timed out.".to_owned()), -                            "continuous-integration/jenkins".to_owned() -                        ).expect( -                            format!( -                                "GitHub timeout error status update failed for {}/{} {}.", -                                commit_ref.owner, -                                commit_ref.repo, -                                commit_ref.sha -                            ).as_ref() -                        ); - -                        return -                    } +                    return Ok(()) +                } -                    sleep(Duration::from_secs(30)); +                sleep(Duration::from_secs(30)); -                    let updated_job = request_job( -                        &jenkins_url, -                        &jenkins_client, -                        job_url.as_ref() +                let updated_job = request_job( +                    &jenkins_url, +                    &jenkins_client, +                    job_url.as_ref() +                ).expect( +                    format!("Failed to request job '{}'.", job_url).as_ref() +                ); + +                if job.result != updated_job.result { +                    github::update_commit_status( +                        &github_token, +                        &commit_ref, +                        &job.result.commit_status(), +                        job_url.clone(), +                        None, +                        "continuous-integration/jenkins".to_owned()                      ).expect( -                        format!("Failed to request job '{}'.", job_url).as_ref() +                        format!( +                            "GitHub status update failed for {}/{} {}.", +                            commit_ref.owner, +                            commit_ref.repo, +                            commit_ref.sha +                        ).as_ref()                      ); -                    if job.result != updated_job.result { -                        github::update_commit_status( -                            &github_token, -                            &commit_ref, -                            &job.result.commit_status(), -                            job_url.clone(), -                            None, -                            "continuous-integration/jenkins".to_owned() -                        ).expect( -                            format!( -                                "GitHub status update failed for {}/{} {}.", -                                commit_ref.owner, -                                commit_ref.repo, -                                commit_ref.sha -                            ).as_ref() -                        ); - -                        return -                    } - -                    job = updated_job; +                    return Ok(())                  } -            }); + +                job = updated_job; +            }              return Ok(())          } diff --git a/src/main.rs b/src/main.rs index 05ca775..6bb4594 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,28 @@ 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() -                            }, -                        }; +                        // Clone so we can use these values in the thread +                        // closure. Since both closures are required to be +                        // 'static, we can't use references to these values. +                        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()), +                            }; +                        });                          rouille::Response::text("202 Accepted")                              .with_status_code(202) | 
