/*
 *
 * 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);
    }

}

@@ -3897,56 +3897,60 @@
  // 
-stop worker when jenkins slave is set to offline
+re-register gearman functions on node status change,
 %0A   
@@ -3973,38 +3973,45 @@
 y.getInstance().
-stop(c
+registerJobs(
 );%0A    %7D%0A%0A    @O
@@ -4433,366 +4433,112 @@
     
-GearmanProxy gp = GearmanProxy.getInstance();%0A        /*%0A         * Spawn management executor worker if one doesn't exist yet.%0A         * This worker does not need any executors. It only needs%0A         * to work with gearman.%0A         */%0A        gp.createManagementWorker();%0A%0A        // on brining a slave back online%0A        gp.createExecutorWorkersOnNode(c
+// re-register gearman functions on node status change,%0A        GearmanProxy.getInstance().registerJobs(
 );%0A 

Fix job result not being sent back to gearman client