return status messages to gearman client

This checkin is to return a correct job status messages to the gearman
client.  It wasn't working before due to this gearman-java issue
https://bugs.launchpad.net/gearman-java/+bug/1126496

src/main/java/hudson/plugins/gearman/StartJobWorker.java
src/main/java/hudson/plugins/gearman/StopJobWorker.java
    Updates to return job error, warning, and success results to gearman client.
    Would like to point out that gearman java<->python translation doesn't work quite
    right.  I believe the python implementation of the gearman worker never
    sends exception messages back to the client

src/main/java/hudson/plugins/gearman/example/StartJobClient.py
src/main/java/hudson/plugins/gearman/example/StopJobClient.py
    Update examples to show how to extract messages

pom.xml
    Updated developer info

Change-Id: Ie8d82be8a8e7c34bc368efda953d5ddfb9547e01
This commit is contained in:
zaro0508 2013-03-05 14:53:07 -08:00
parent 8ee129e6be
commit 0bf4a7d2ff
5 changed files with 52 additions and 57 deletions

View File

@ -44,9 +44,9 @@
<developers>
<developer>
<id>khaido</id>
<id>zaro0508</id>
<name>Khai Do</name>
<email>do.khai@gmail.com</email>
<email>zaro0508@gmail.com</email>
</developer>
</developers>

View File

@ -124,18 +124,17 @@ public class StartJobWorker extends AbstractGearmanFunction {
Action params = new NodeParametersAction(buildParams, decodedUniqueId);
Action [] actions = {runNode, params};
// schedule jenkins to build project
logger.info("Scheduling "+project.getName()+" build #" +
project.getNextBuildNumber()+" on " + runNodeName
+ " with UUID " + decodedUniqueId + " and build params " + buildParams);
Future<?> future = project.scheduleBuild2(0, new Cause.UserIdCause(), actions);
String jobException = "";
String jobResultMsg = "";
// jobResult does not change otherwise no results are returned:
// https://answers.launchpad.net/gearman-java/+question/221348
// check build and pass results back to client
boolean jobResult = true;
String jobFailureMsg = "";
String jobWarningMsg = "";
String jobResultMsg = "";
try {
// wait for jenkins build to complete
@ -152,30 +151,32 @@ public class StartJobWorker extends AbstractGearmanFunction {
} else if (result == Result.ABORTED) {
jobResultMsg = "Build Aborted : "+buildNum+": "+buildId+" on " + runNodeName
+ " with UUID " + decodedUniqueId + " and build params " + buildParams;
jobException = jobResultMsg;
} else if (result == Result.UNSTABLE) {
jobResultMsg = "Build Unstable : "+buildNum+": "+buildId+" on " + runNodeName
jobFailureMsg = "Build Unstable : "+buildNum+": "+buildId+" on " + runNodeName
+ " with UUID " + decodedUniqueId + " and build params " + buildParams;
jobException = jobResultMsg;
jobResult = false;
} else if (result == Result.FAILURE) {
jobResultMsg = "Build failed : "+buildNum+": "+buildId+" on " + runNodeName
jobFailureMsg = "Build failed : "+buildNum+": "+buildId+" on " + runNodeName
+ " with UUID " + decodedUniqueId + " and build params " + buildParams;
jobException = jobResultMsg;
jobResult = false;
} else if (result == Result.NOT_BUILT) {
jobResultMsg = "Build not done : "+buildNum+": "+buildId+" on " + runNodeName
jobWarningMsg = "Build not done : "+buildNum+": "+buildId+" on " + runNodeName
+ " with UUID " + decodedUniqueId + " and build params " + buildParams;
jobException = jobResultMsg;
jobResult = false;
}
} catch (InterruptedException e) {
jobException = e.getMessage();
jobFailureMsg = e.getMessage();
jobResult = false;
} catch (ExecutionException e) {
jobException = e.getMessage();
jobFailureMsg = e.getMessage();
jobResult = false;
}
// return result to client
GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, jobResult,
jobResultMsg.getBytes(), new byte[0], jobException.getBytes(), 0, 0);
jobResultMsg.getBytes(), jobWarningMsg.getBytes(),
jobFailureMsg.getBytes(), 0, 0);
return gjr;
}
}

View File

@ -64,46 +64,35 @@ public class StopJobWorker extends AbstractGearmanFunction {
}
}
boolean abortResult = false;
if (decodedUniqueId != null) {
// Abort running jenkins build that contain matching uuid
abortResult = abortBuild(decodedUniqueId);
}
//TODO: build might be on gearman queue if it's not currently
// running by jenkins, need to check the gearman queue for the
// job and remove it.
String jobResultMsg = "";
String jobResultEx = "";
// check build and pass results back to client
boolean jobResult = true;
if (abortResult){
jobResultMsg = "Canceled jenkins build " + decodedUniqueId;
String jobFailureMsg = "";
String jobWarningMsg = "";
String jobResultMsg = "";
if (decodedUniqueId.isEmpty() || decodedUniqueId == null){
logger.info("Client passed in an invalid UUID");
jobFailureMsg = "I need the job Id please";
jobResult = false;
} else {
jobResultMsg = "Did not cancel jenkins build " + decodedUniqueId;
jobResultEx = "Could not cancel build " + decodedUniqueId;
// Abort running jenkins build that contain matching uuid
jobResult = abortBuild(decodedUniqueId);
if (jobResult){
jobResultMsg = "Canceled jenkins build " + decodedUniqueId;
} else {
jobFailureMsg = "Could not cancel build " + decodedUniqueId;
jobResult = false;
}
}
GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, jobResult,
jobResultMsg.getBytes(), new byte[0], jobResultEx.getBytes(), 0, 0);
jobResultMsg.getBytes(), jobWarningMsg.getBytes(),
jobFailureMsg.getBytes(), 0, 0);
return gjr;
}
/**
* Function to cancel a Gearman job from the Gearman queue
*
* @param uuid
* The build uuid
* @return
* true if job was cancel, otherwise false
*/
private boolean cancelJob (String uuid) {
//TODO: Need to cancel job from gearman queue, not sure how to
// do it yet.
return false;
}
/**
* Function to abort a currently running Jenkins build
* Running Jenkins builds are builds that actively being
@ -116,10 +105,6 @@ public class StopJobWorker extends AbstractGearmanFunction {
*/
private boolean abortBuild (String uuid) {
if (uuid.isEmpty() || uuid == null){ //NOOP
return false;
}
/*
* iterate over the executors on master and slave nodes to find the
* build on the executor with the matching uuid

View File

@ -41,5 +41,9 @@ request = client.submit_job(function,
poll_timeout=60,
unique=build_id)
print request.result
if (request.exception) :
print request.exception
else:
print ', '.join(request.warning_updates)
print request.result
print 'Work complete with state %s' % request.state

View File

@ -40,4 +40,9 @@ request = client.submit_job(function,
poll_timeout=60,
unique=build_id)
print request.result
if (request.exception) :
print request.exception
else:
print ', '.join(request.warning_updates)
print request.result
print 'Work complete with state %s' % request.state