3
* Copyright 2013 Hewlett-Packard Development Company, L.P.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
20
package hudson.plugins.gearman;
22
import hudson.model.Run;
23
import hudson.model.Executor;
25
import java.io.UnsupportedEncodingException;
28
import org.gearman.client.GearmanJobResult;
29
import org.gearman.client.GearmanJobResultImpl;
30
import org.gearman.worker.AbstractGearmanFunction;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
34
import com.google.gson.Gson;
35
import com.google.gson.reflect.TypeToken;
38
* This is a gearman function that will cancel/abort jenkins builds
42
public class StopJobWorker extends AbstractGearmanFunction {
44
private static final Logger logger = LoggerFactory
45
.getLogger(Constants.PLUGIN_LOGGER_NAME);
49
* The Gearman Function
50
* @see org.gearman.worker.AbstractGearmanFunction#executeFunction()
53
public GearmanJobResult executeFunction() {
56
boolean jobResult = false;
57
String jobResultMsg = "";
62
decodedData = new String((byte[]) this.data, "UTF-8");
63
} catch (UnsupportedEncodingException e) {
64
throw new IllegalArgumentException("Unsupported encoding exception in argument");
66
// convert parameters passed in from client to hash map
67
Gson gson = new Gson();
68
Map<String, String> data = gson.fromJson(decodedData,
69
new TypeToken<Map<String, String>>() {
73
String jobName = data.get("name");
74
String buildNumber = data.get("number");
75
if (jobName.isEmpty() || buildNumber.isEmpty()) {
76
throw new IllegalArgumentException("Build id is invalid or not specified");
79
// Abort running jenkins build that contain matching uuid
80
Run<?,?> build = GearmanPluginUtil.findBuild(jobName, Integer.parseInt(buildNumber));
82
if (build.isBuilding()) {
83
Executor executor = build.getExecutor();
84
// abort the running jenkins build
85
if (!executor.isInterrupted()) {
87
logger.info("---- Aborting build : " +
88
jobName + ": " + buildNumber);
92
logger.info("---- Request to abourt non-building build : " +
93
jobName + ": " + buildNumber);
96
throw new IllegalArgumentException("Cannot find build " +
97
jobName + ": " + buildNumber);
100
GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, jobResult,
101
jobResultMsg.getBytes(), null, null, 0, 0);