
hudson.model.Executor Maven / Gradle / Ivy
package hudson.model;
import hudson.Util;
import hudson.security.ACL;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.export.Exported;
import org.acegisecurity.context.SecurityContextHolder;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;
/**
* Thread that executes builds.
*
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public class Executor extends Thread implements ModelObject {
private final Computer owner;
private final Queue queue;
private long startTime;
/**
* Used to track when a job was last executed.
*/
private long finishTime;
/**
* Executor number that identifies it among other executors for the same {@link Computer}.
*/
private int number;
/**
* {@link Queue.Executable} being executed right now, or null if the executor is idle.
*/
private volatile Queue.Executable executable;
private Throwable causeOfDeath;
public Executor(Computer owner) {
super("Executor #"+owner.getExecutors().size()+" for "+owner.getDisplayName());
this.owner = owner;
this.queue = Hudson.getInstance().getQueue();
this.number = owner.getExecutors().size();
start();
}
public void run() {
// run as the system user. see ACL.SYSTEM for more discussion about why this is somewhat broken
SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
try {
finishTime = System.currentTimeMillis();
while(true) {
if(Hudson.getInstance() == null || Hudson.getInstance().isTerminating())
return;
synchronized(owner) {
if(owner.getNumExecutors()=100) num=99;
return num;
}
/**
* Computes a human-readable text that shows the expected remaining time
* until the build completes.
*/
public String getEstimatedRemainingTime() {
Queue.Executable e = executable;
if(e==null) return Messages.Executor_NotAvailable();
long d = e.getParent().getEstimatedDuration();
if(d<0) return Messages.Executor_NotAvailable();
long eta = d-(System.currentTimeMillis()-startTime);
if(eta<=0) return Messages.Executor_NotAvailable();
return Util.getTimeSpanString(eta);
}
/**
* The same as {@link #getEstimatedRemainingTime()} but return
* it as a number of milli-seconds.
*/
public long getEstimatedRemainingTimeMillis() {
Queue.Executable e = executable;
if(e==null) return -1;
long d = e.getParent().getEstimatedDuration();
if(d<0) return -1;
long eta = d-(System.currentTimeMillis()-startTime);
if(eta<=0) return -1;
return eta;
}
/**
* Stops the current build.
*/
public void doStop( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
Queue.Executable e = executable;
if(e!=null) {
e.getParent().checkAbortPermission();
interrupt();
}
rsp.forwardToPreviousPage(req);
}
/**
* Checks if the current user has a permission to stop this build.
*/
public boolean hasStopPermission() {
Queue.Executable e = executable;
return e!=null && e.getParent().hasAbortPermission();
}
public Computer getOwner() {
return owner;
}
/**
* Returns when this executor started or should start being idle.
*/
public long getIdleStartMilliseconds() {
if (isIdle())
return finishTime;
else {
return Math.max(startTime + Math.max(0, executable.getParent().getEstimatedDuration()),
System.currentTimeMillis() + 15000);
}
}
/**
* Exposes the executor to the remote API.
*/
public Api getApi() {
return new Api(this);
}
/**
* Returns the executor of the current thread.
*/
public static Executor currentExecutor() {
return (Executor)Thread.currentThread();
}
private static final Logger LOGGER = Logger.getLogger(Executor.class.getName());
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy