Skip to content

Commit

Permalink
[FIXED JENKINS-26090] Executables.getExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Mar 19, 2015
1 parent b62c927 commit f2f988e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
19 changes: 3 additions & 16 deletions core/src/main/java/hudson/model/Run.java
Expand Up @@ -47,6 +47,7 @@
import hudson.model.Run.RunExecution;
import hudson.model.listeners.RunListener;
import hudson.model.listeners.SaveableListener;
import hudson.model.queue.Executables;
import hudson.model.queue.SubTask;
import hudson.search.SearchIndexBuilder;
import hudson.security.ACL;
Expand Down Expand Up @@ -512,25 +513,11 @@ public boolean isLogUpdated() {
* and because of that this might not be necessarily in sync with the return value of {@link #isBuilding()} —
* an executor holds on to {@link Run} some more time even after the build is finished (for example to
* perform {@linkplain Run.State#POST_PRODUCTION post-production processing}.)
* @see Executables#getExecutor
*/
@Exported
public @CheckForNull Executor getExecutor() {
Jenkins j = Jenkins.getInstance();
if (j == null) {
return null;
}
for (Computer c : j.getComputers()) {
for (Executor e : c.getExecutors()) {
if(e.getCurrentExecutable()==this)
return e;
}
for (Executor e : c.getOneOffExecutors()) {
if(e.getCurrentExecutable()==this) {
return e;
}
}
}
return null;
return this instanceof Queue.Executable ? Executables.getExecutor((Queue.Executable) this) : null;
}

/**
Expand Down
34 changes: 32 additions & 2 deletions core/src/main/java/hudson/model/queue/Executables.java
Expand Up @@ -23,16 +23,18 @@
*/
package hudson.model.queue;

import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.Queue.Executable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;

/**
* Convenience methods around {@link Executable}.
*
* @author Kohsuke Kawaguchi
*/
public class Executables {
/**
Expand Down Expand Up @@ -71,5 +73,33 @@ public static long getEstimatedDurationFor(Executable e) {
return e.getParent().getEstimatedDuration();
}
}

/**
* Finds the executor running a given process.
* @param executable a possibly running executable
* @return the executor (possibly a {@link OneOffExecutor}) whose {@link Executor#getCurrentExecutable} matches that, or null
* @since TODO
*/
public static @CheckForNull Executor getExecutor(Executable executable) {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
return null;
}
for (Computer computer : jenkins.getComputers()) {
for (Executor executor : computer.getExecutors()) {
if (executor.getCurrentExecutable() == executable) {
return executor;
}
}
for (Executor executor : computer.getOneOffExecutors()) {
if (executor.getCurrentExecutable() == executable) {
return executor;
}
}
}
return null;
}

private Executables() {}

}

0 comments on commit f2f988e

Please sign in to comment.