[FIXED JENKINS-26090] Executables.getExecutor

This commit is contained in:
Jesse Glick committed 2015-03-19 17:06:39 -04:00
1 parent b62c927a27
commit f2f988e81f
2 files changed
+35 -18

No files matched your search

+3 -16
View File
@@ -47,6 +47,7 @@ import hudson.model.Descriptor.FormException;
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;
@@ -512,25 +513,11 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
* and because of that this might not be necessarily in sync with the return value of {@link #isBuilding()} &mdash;
* 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;
}
/**
@@ -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 {
/**
@@ -71,5 +73,33 @@ public class Executables {
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() {}
}