Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Commit

Permalink
Added a Stop() method to handle stopping executor worker threads.
Browse files Browse the repository at this point in the history
fix for bug 1181569

Refactored how stop was handled.  Removing thread and stopping thread
in the same syncronized loop was causing concurrent modification error.
Needed to seperate the actions for it to work.

Change-Id: I99b676f377842826f9e6a847cfeff76be91c8299
  • Loading branch information
zaro0508 committed May 31, 2013
1 parent 0b017fe commit 78abc3d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@

import java.util.Date;
import java.util.Set;
import java.util.HashSet;

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

Expand Down Expand Up @@ -118,8 +115,7 @@ public void stop() {

if (worker.isRunning()) {
try {
logger.info("---- Stopping " + getName() +" (" + new Date().toString() + ")");
worker.shutdown();
worker.stop();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import hudson.slaves.ComputerListener;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,19 +66,8 @@ public void onOffline(Computer c) {
return;
}

// remove worker when jenkins slave is deleted or disconnected
GearmanProxy gp = GearmanProxy.getInstance();
List<AbstractWorkerThread> workers = gp.getGewtHandles();
synchronized(workers) {
for (AbstractWorkerThread worker : workers) {
if (worker.name.contains(c.getName())) {
logger.info("---- stopping executor worker = "
+ worker.getName());
gp.getGewtHandles().remove(worker);
worker.stop();
}
}
}
// stop worker when jenkins slave is deleted or disconnected
GearmanProxy.getInstance().stop(c);
}

@Override
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/hudson/plugins/gearman/GearmanProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import jenkins.model.Jenkins;
Expand Down Expand Up @@ -142,6 +143,9 @@ public void createManagementWorker() {
masterName);
gwt.start();
gmwtHandles.add(gwt);

logger.info("---- Num of executors running = " + getNumExecutors());

}

/*
Expand All @@ -168,8 +172,10 @@ public void createExecutorWorkersOnNode(Computer computer) {
//ewt.registerJobs();
ewt.start();
gewtHandles.add(ewt);

}

logger.info("---- Num of executors running = " + getNumExecutors());

}

/*
Expand All @@ -194,6 +200,31 @@ public void stopAll() {
logger.info("---- Num of executors running = " + getNumExecutors());
}

/*
* This method stops all threads on the gewtHandles list that
* is used to service the jenkins slave/computer
*
*
* @param Node
* The Computer to stop
*
*/
public void stop(Computer computer) {

// find the computer in the executor workers list and stop it
synchronized(gewtHandles) {
for (Iterator<AbstractWorkerThread> it = gewtHandles.iterator(); it.hasNext(); ) {
AbstractWorkerThread t = it.next();
if (t.name.contains(computer.getName())) {
t.stop();
it.remove();
}
}
}

logger.info("---- Num of executors running = " + getNumExecutors());
}

/*
* This method returns the total number of gearman executor threads
*/
Expand Down

0 comments on commit 78abc3d

Please sign in to comment.