/*
 *
 * 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 java.util.Date;

import org.gearman.common.GearmanNIOJobServerConnection;
import org.gearman.worker.GearmanWorker;
import org.gearman.worker.GearmanWorkerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Base object for gearman worker threads
 *
 *
 * @author Khai Do
 */

public abstract class AbstractWorkerThread implements Runnable {

    public static final String DEFAULT_EXECUTOR_NAME = "anonymous";
    private static final Logger logger = LoggerFactory
            .getLogger(Constants.PLUGIN_EXECTUOR_LOGGER_NAME);

    protected String host;
    protected int port;
    protected String name;
    protected GearmanWorker worker;
    private GearmanNIOJobServerConnection conn;
    private Thread thread;


    public AbstractWorkerThread(String host, int port, String name) {
        this.name = name;
        this.host = host;
        this.port = port;
        worker = new GearmanWorkerImpl();
        conn = new GearmanNIOJobServerConnection(host, port);

    }

    /*
     * Register jobs with the gearman worker.
     * This method should be overriden.
     */
    public void registerJobs() {

        logger.info("----- AbstractorWorker registerJobs function ----");

    }

    /*
     * Start the thread
     */
    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    /*
     * Stop the thread
     */
    public void stop() {
        // Interrupt the thread so it unblocks any blocking call


        if (worker.isRunning()) {
            logger.info("Stopping " + name + " (" + new Date().toString() + ")");
            worker.stop();
            logger.info("Stopped " + name + " (" + new Date().toString() + ")");
        }

        thread.interrupt();

        // Wait until the thread exits
        try {

            thread.join();
        } catch (InterruptedException ex) {
            // Unexpected interruption
            ex.printStackTrace();
            System.exit(1);
        }


    }

    /*
     * Execute the thread (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {

        if (!worker.isRunning()) {
            logger.info("Starting Worker "+ name +" ("+new Date().toString()+")");
            worker.setWorkerID(name);
            worker.addServer(conn);
            worker.work();
        }

        while (!Thread.interrupted()) {

            // Running the Gearman Worker
            logger.info("Running Worker "+ name +" ("+new Date().toString()+")");

            try {
                Thread.sleep(5000);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

        }

        logger.info("Thread Stopped" + " (" + new Date().toString() + ")");

        // Thread exits
    }

}

@@ -1339,16 +1339,22 @@
 private 
+final 
 GearmanN
@@ -1381,16 +1381,16 @@
 n conn;%0A
-
     priv
@@ -2295,89 +2295,8 @@
 ();%0A
-            logger.info(%22Stopped %22 + name + %22 (%22 + new Date().toString() + %22)%22);%0A
     
@@ -2533,36 +2533,8 @@
 ();%0A
-            System.exit(1);%0A
     
@@ -2921,381 +2921,43 @@
     
-while (!Thread.interrupted()) %7B%0A%0A            // Running the Gearman Worker%0A            logger.info(%22Running Worker %22+ name +%22 (%22+new Date().toString()+%22)%22);%0A%0A            try %7B%0A                Thread.sleep(5000);%0A            %7D catch(InterruptedException ex) %7B%0A                Thread.currentThread().interrupt();%0A            %7D%0A%0A        %7D%0A%0A        logger.info(%22Thread Stopped%22
+logger.info(%22Stopped worker%22 + name
  + %22

fix a few threading issues.