add gearman worker to set build descriptions

This commit adds another management worker for setting a build's descriptions.
The worker will lookup the build by it's build_id (jobName:jobId)
and if it can find it then will set the description and return with COMPLETED.
If it cannot find the build or cannot set the description for any reason
it will return FAILED.

This commit also adds teh build_id to the data returned by the StartJobWorker.

Usage:

  client = gear.Client()
  client.addServer('localhost')
  client.waitForServer()
  job_name = 'set_description:JenkinsHostName'
  data = {'build_id':"pep8:2013-05-15_17-32-07",'description':"<h1>My Test Build</h1>"}
  job = gear.Job(job_name, simplejson.dumps(data))
  client.submitJob(job)

Change-Id: I4990772d591d27bbb3b4f20abfb4d988077b4995
This commit is contained in:
zaro0508 2013-05-13 11:04:26 -07:00
parent 0de925f6d8
commit 42c95ac958
5 changed files with 158 additions and 20 deletions

View File

@ -130,8 +130,8 @@ public class GearmanProxy {
}
/*
* Spawn management executor worker. This worker does not need any
* executors. It only needs to work with gearman.
* Spawn management executor workers. This worker does not need any
* executors. It only needs to connect to gearman.
*/
public void createManagementWorker() {
@ -140,10 +140,8 @@ public class GearmanProxy {
GearmanPluginConfig.get().getPort(),
masterName + "_manager",
masterName);
//gwt.registerJobs();
gwt.start();
gmwtHandles.add(gwt);
}
/*

View File

@ -21,8 +21,8 @@ package hudson.plugins.gearman;
import java.util.HashSet;
import java.util.Set;
import org.gearman.worker.GearmanFunctionFactory;
import org.gearman.worker.DefaultGearmanFunctionFactory;
import org.gearman.worker.GearmanFunctionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,12 +56,12 @@ public class ManagementWorkerThread extends AbstractWorkerThread{
@Override
public void registerJobs(){
if (!registered) {
String jobFunctionName = "stop:"+masterName;
logger.info("---- Registering job "+jobFunctionName+" on "+host);
Set<GearmanFunctionFactory> functionSet = new HashSet<GearmanFunctionFactory>();
functionSet.add(new DefaultGearmanFunctionFactory(jobFunctionName,
functionSet.add(new DefaultGearmanFunctionFactory("stop:"+masterName,
StopJobWorker.class.getName()));
functionSet.add(new DefaultGearmanFunctionFactory("set_description:"+masterName,
SetDescriptionWorker.class.getName()));
updateJobs(functionSet);
registered = true;

View File

@ -0,0 +1,140 @@
/*
*
* Copyright 2013 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package hudson.plugins.gearman;
import hudson.model.AbstractProject;
import hudson.model.Run;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import jenkins.model.Jenkins;
import org.gearman.client.GearmanJobResult;
import org.gearman.client.GearmanJobResultImpl;
import org.gearman.worker.AbstractGearmanFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* This is a gearman function to set a jenkins build
* description
*
*
* @author Khai Do
*/
public class SetDescriptionWorker extends AbstractGearmanFunction {
private static final Logger logger = LoggerFactory
.getLogger(Constants.PLUGIN_LOGGER_NAME);
/*
* The Gearman Function
* @see org.gearman.worker.AbstractGearmanFunction#executeFunction()
*/
@Override
public GearmanJobResult executeFunction() {
// check job results
boolean jobResult = false;
String jobExceptionMsg = "";
String jobWarningMsg = "";
String jobResultMsg = "";
try {
// decode json
String decodedData = new String((byte[]) this.data, "UTF-8");
// convert parameters passed in from client to hash map
Gson gson = new Gson();
Map<String, String> data = gson.fromJson(decodedData,
new TypeToken<Map<String, String>>() {
}.getType());
// get build description
String buildDescription = data.get("description");
// get build id
String buildId = data.get("build_id");
String[] idToken = buildId.split(":");
if (idToken.length != 2 || buildDescription == null || buildId == null) {
jobExceptionMsg = "Invalid Unique Id";
throw new IllegalArgumentException(jobExceptionMsg);
} else {
String jobName = idToken[0];
String jobId = idToken[1];
if (!jobName.isEmpty() && !jobId.isEmpty()) {
// find build then update its description
Run<?,?> build = findBuild(jobName, jobId);
if (build != null) {
build.setDescription(buildDescription);
jobResultMsg = "Description for Jenkins build " +buildId+" was pdated to " + buildDescription;
jobResult = true;
} else {
jobExceptionMsg = "Cannot find build with id " + buildId;
throw new IllegalArgumentException(jobExceptionMsg);
}
} else {
jobExceptionMsg = "Build id is invalid or not specified";
throw new IllegalArgumentException(jobExceptionMsg);
}
}
} catch (UnsupportedEncodingException e) {
jobExceptionMsg = "Error decoding parameters";
} catch (NullPointerException e) {
jobExceptionMsg = "Error decoding parameters";
} catch (IOException e) {
jobExceptionMsg = "Error setting build description";
} catch (IllegalArgumentException e) {
jobExceptionMsg = e.getMessage();
}
GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, jobResult,
jobResultMsg.getBytes(), jobWarningMsg.getBytes(),
jobExceptionMsg.getBytes(), 0, 0);
return gjr;
}
/**
* Function to finds the build with the unique build id.
*
* @param jobName
* The jenkins job or project name
* @param uuid
* The jenkins job id
* @return
* the build Run if found, otherwise return null
*/
private Run<?,?> findBuild(String jobName, String jobId) {
AbstractProject<?,?> project = Jenkins.getInstance().getItemByFullName(jobName, AbstractProject.class);
if (project != null){
Run<?,?> run = project.getBuild(jobId);
if (run != null) {
return run;
}
}
return null;
}
}

View File

@ -19,36 +19,34 @@
package hudson.plugins.gearman;
import hudson.model.Hudson;
import hudson.model.Action;
import hudson.model.ParameterValue;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Cause;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.Project;
import hudson.model.Queue;
import hudson.model.Queue.Executable;
import hudson.model.queue.QueueTaskFuture;
import hudson.model.StringParameterValue;
import hudson.model.queue.QueueTaskFuture;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.gearman.common.GearmanJobServerSession;
import org.gearman.client.GearmanIOEventListener;
import org.gearman.client.GearmanJobResult;
import org.gearman.client.GearmanJobResultImpl;
import org.gearman.common.GearmanJobServerSession;
import org.gearman.worker.AbstractGearmanFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -90,6 +88,7 @@ public class StartJobWorker extends AbstractGearmanFunction {
data.put("name", project.getName());
data.put("number", build.getNumber());
data.put("id", build.getId());
data.put("build_id", project.getName()+":"+build.getId());
data.put("url", build.getUrl());
data.put("master", masterName);
@ -182,7 +181,7 @@ public class StartJobWorker extends AbstractGearmanFunction {
try {
// wait for start of build
Queue.Executable exec = (Executable) future.getStartCondition().get();
Queue.Executable exec = future.getStartCondition().get();
AbstractBuild<?, ?> currBuild = (AbstractBuild<?, ?>) exec;
long now = new Date().getTime();
@ -227,7 +226,7 @@ public class StartJobWorker extends AbstractGearmanFunction {
}
}
exec = (Executable) future.get();
exec = future.get();
jobData = buildStatusData(currBuild);
if (sess != null) {

View File

@ -60,7 +60,8 @@ public class ManagementWorkerThreadTest {
"master_manager", "master");
manager.registerJobs();
Set<String> functions = manager.worker.getRegisteredFunctions();
assertEquals("stop:master", functions.toArray()[0]);
assertEquals("set_description:master", functions.toArray()[0]);
assertEquals("stop:master", functions.toArray()[1]);
}
@Test