[snowpatch] [PATCH] jenkins: add wait_build() method to poll Jenkins build status at intervals
Andrew Donnellan
andrew.donnellan at au1.ibm.com
Fri Apr 8 17:34:04 AEST 2016
Right now, after the Jenkins build is triggered, we query the build status
continuously. This is bad, and it works the CPU rather hard.
Instead, add a new method to poll every 5 seconds and put the thread to
sleep in the meantime.
Closes: #16 ("Don't spin when waiting for Jenkins results")
Signed-off-by: Andrew Donnellan <andrew.donnellan at au1.ibm.com>
---
Probably a couple of other places where we need to do something similar.
---
src/jenkins.rs | 16 ++++++++++++++++
src/main.rs | 11 ++---------
2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/src/jenkins.rs b/src/jenkins.rs
index 9983396..38a8664 100644
--- a/src/jenkins.rs
+++ b/src/jenkins.rs
@@ -25,11 +25,16 @@ extern crate url;
extern crate rustc_serialize;
use std::io::Read;
+use std::time::Duration;
+use std::thread::sleep;
use hyper::Client;
use hyper::header::Location;
use rustc_serialize::json::Json;
+// Constants
+const JENKINS_POLLING_INTERVAL: u64 = 5000; // Polling interval in milliseconds
+
// Jenkins API definitions
pub trait CIBackend { // TODO: Separate out
@@ -98,4 +103,15 @@ impl<'a> JenkinsBackend<'a> {
false => JenkinsBuildStatus::Done,
}
}
+
+ pub fn wait_build(&self, build_url: &str) -> JenkinsBuildStatus {
+ // TODO: Implement a timeout?
+ loop {
+ match self.get_build_status(&build_url) {
+ JenkinsBuildStatus::Done => return JenkinsBuildStatus::Done,
+ _ => { },
+ }
+ sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL));
+ }
+ }
}
diff --git a/src/main.rs b/src/main.rs
index 01524dd..b18206c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -43,7 +43,7 @@ mod patchwork;
use patchwork::{PatchworkServer, TestState, TestResult};
mod jenkins;
-use jenkins::{JenkinsBackend, CIBackend, JenkinsBuildStatus};
+use jenkins::{JenkinsBackend, CIBackend};
mod settings;
use settings::{Config, Project};
@@ -109,14 +109,7 @@ fn run_tests(settings: &Config, project: &Project, tag: &str) -> Vec<TestResult>
}
}
println!("Build URL: {}", build_url_real);
-
- loop {
- let status = jenkins.get_build_status(&build_url_real);
- match status {
- JenkinsBuildStatus::Done => break,
- _ => {}
- }
- }
+ jenkins.wait_build(&build_url_real);
println!("Job done!");
results.push(TestResult {
test_name: job_name.to_string(),
--
Andrew Donnellan Software Engineer, OzLabs
andrew.donnellan at au1.ibm.com Australia Development Lab, Canberra
+61 2 6201 8874 (work) IBM Australia Limited
More information about the snowpatch
mailing list