~zaro0508/gearman-plugin/master

« back to all changes in this revision

Viewing changes to src/main/java/hudson/plugins/gearman/StopJobWorker.java

  • Committer: Monty Taylor
  • Date: 2019-07-31 17:30:46 UTC
  • Revision ID: git-v1:93aaac09b2cc92c64908b308271a04231ebd755e
Retire github mirror, repo moved to opendev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 * Copyright 2013 Hewlett-Packard Development Company, L.P.
4
 
 *
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 *
17
 
 */
18
 
 
19
 
 
20
 
package hudson.plugins.gearman;
21
 
 
22
 
import hudson.model.Run;
23
 
import hudson.model.Executor;
24
 
 
25
 
import java.io.UnsupportedEncodingException;
26
 
import java.util.Map;
27
 
 
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;
33
 
 
34
 
import com.google.gson.Gson;
35
 
import com.google.gson.reflect.TypeToken;
36
 
 
37
 
/**
38
 
 * This is a gearman function that will cancel/abort jenkins builds
39
 
 *
40
 
 * @author Khai Do
41
 
 */
42
 
public class StopJobWorker extends AbstractGearmanFunction {
43
 
 
44
 
    private static final Logger logger = LoggerFactory
45
 
            .getLogger(Constants.PLUGIN_LOGGER_NAME);
46
 
 
47
 
 
48
 
    /*
49
 
     * The Gearman Function
50
 
     * @see org.gearman.worker.AbstractGearmanFunction#executeFunction()
51
 
     */
52
 
    @Override
53
 
    public GearmanJobResult executeFunction() {
54
 
 
55
 
        // check job results
56
 
        boolean jobResult = false;
57
 
        String jobResultMsg = "";
58
 
 
59
 
        String decodedData;
60
 
        // decode json
61
 
        try {
62
 
            decodedData = new String((byte[]) this.data, "UTF-8");
63
 
        } catch (UnsupportedEncodingException e) {
64
 
            throw new IllegalArgumentException("Unsupported encoding exception in argument");
65
 
        }
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>>() {
70
 
                }.getType());
71
 
 
72
 
        // get build id
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");
77
 
        }
78
 
 
79
 
        // Abort running jenkins build that contain matching uuid
80
 
        Run<?,?> build = GearmanPluginUtil.findBuild(jobName, Integer.parseInt(buildNumber));
81
 
        if (build != null) {
82
 
            if (build.isBuilding()) {
83
 
                Executor executor = build.getExecutor();
84
 
                // abort the running jenkins build
85
 
                if (!executor.isInterrupted()) {
86
 
                    executor.interrupt();
87
 
                    logger.info("---- Aborting build : " +
88
 
                                jobName + ": " + buildNumber);
89
 
                    jobResult = true;
90
 
                }
91
 
            } else {
92
 
                logger.info("---- Request to abourt non-building build : " +
93
 
                            jobName + ": " + buildNumber);
94
 
            }
95
 
        } else {
96
 
            throw new IllegalArgumentException("Cannot find build " +
97
 
                                               jobName + ": " + buildNumber);
98
 
        }
99
 
 
100
 
        GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, jobResult,
101
 
                jobResultMsg.getBytes(), null, null, 0, 0);
102
 
        return gjr;
103
 
    }
104
 
}