summaryrefslogtreecommitdiff
path: root/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java
blob: 08e07a9392e9af6eff3bdabc388dbffed78290cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *
 * Copyright 2014 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.Extension;
import hudson.model.TaskListener;
import hudson.model.Computer;
import hudson.slaves.ComputerListener;
import hudson.slaves.OfflineCause;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Update gearman workers on node state and configuration changes
 */
@Extension
public class ComputerListenerImpl extends ComputerListener {

    private static final Logger logger = LoggerFactory
            .getLogger(Constants.PLUGIN_LOGGER_NAME);

    @Override
    public void onConfigurationChange() {
        // only fired on nodes configuration changes like a label or
        // name change. Not fired on state changes, like offline or online.
        logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
                + " onConfigurationChange");

        // update functions only when gearman-plugin is enabled
        if (!GearmanPluginConfig.get().enablePlugin()) {
            return;
        }

        // re-register gearman functions on node configuration changes,
        // specifically node label changes
        GearmanProxy.getInstance().registerJobs();

        // TODO: adjust for an update to executors. Method does not provide the
        // computer to know which thread to remove or add
    }

    @Override
    public void onOffline(Computer c) {
        // gets called when existing slave dis-connects
        // gets called when slave is deleted.
        logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
                + " onOffline computer" + c);

        // update functions only when gearman-plugin is enabled
        if (!GearmanPluginConfig.get().enablePlugin()) {
            return;
        }

        // stop worker when jenkins slave is deleted or disconnected
        GearmanProxy.getInstance().stop(c);
    }

    @Override
    public void onOnline(Computer c, TaskListener listener) throws IOException,
            InterruptedException {
        // gets called when master goes into online state
        // gets called when existing slave re-connects
        // gets called when new slave goes into online state
        logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
                + " onOnline computer " + c);

        // update functions only when gearman-plugin is enabled
        if (!GearmanPluginConfig.get().enablePlugin()) {
            return;
        }

        GearmanProxy gp = GearmanProxy.getInstance();
        /*
         * Spawn management executor worker if one doesn't exist yet.
         * This worker does not need any executors. It only needs
         * to work with gearman.
         */
        gp.createManagementWorker();

        // on creation of new slave
        gp.createExecutorWorkersOnNode(c);
    }

    @Override
    public void onTemporarilyOffline(Computer c, OfflineCause cause) {
        // fired when master or slave goes into temporary offline state
        logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
                + " onTemporarilyOffline computer " + c);
        // update functions only when gearman-plugin is enabled
        if (!GearmanPluginConfig.get().enablePlugin()) {
            return;
        }

        // stop worker when jenkins slave is set to offline
        GearmanProxy.getInstance().stop(c);
    }

    @Override
    public void onTemporarilyOnline(Computer c) {
        // fired when master or slave goes into temporary online state
        logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
                + " onTemporarilyOnline computer " + c);
        // update functions only when gearman-plugin is enabled
        if (!GearmanPluginConfig.get().enablePlugin()) {
            return;
        }

        GearmanProxy gp = GearmanProxy.getInstance();
        /*
         * Spawn management executor worker if one doesn't exist yet.
         * This worker does not need any executors. It only needs
         * to work with gearman.
         */
        gp.createManagementWorker();

        // on brining a slave back online
        gp.createExecutorWorkersOnNode(c);
    }

}