提交 546e0ec9 编写于 作者: J Jesse Glick

Merge pull request #1 from jenkinsci/pull-1610

Additional change suggestions on top of PR 1610
......@@ -90,6 +90,11 @@ public class Executor extends Thread implements ModelObject {
* {@link hudson.model.Queue.Executable} being executed right now, or null if the executor is idle.
*/
private volatile Queue.Executable executable;
/**
* Used to mark that the execution is continuing asynchronously even though {@link Executor} as {@link Thread}
* has finished.
*/
private AsynchronousExecution asynchronousExecution;
/**
......
......@@ -28,6 +28,8 @@ import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.ExecutorListener;
import hudson.model.OneOffExecutor;
import hudson.model.Queue.Executable;
import hudson.model.Queue.FlyweightTask;
import hudson.model.Resource;
import hudson.model.ResourceActivity;
import hudson.model.ResourceController;
......@@ -39,12 +41,12 @@ import org.kohsuke.accmod.restrictions.NoExternalUse;
/**
* Special means of indicating that an executable will proceed in the background without consuming a native thread ({@link Executor}).
* May be thrown from {@link hudson.model.Queue.Executable#run} after doing any preparatory work synchronously.
* May be thrown from {@link Executable#run} after doing any preparatory work synchronously.
* <p>{@link Executor#isActive} will remain true (even though {@link Executor#isAlive} is not) until {@link #completed} is called.
* The thrower will need to hold on to a reference to this instance as a handle to call {@link #completed}.
* <p>The execution may not extend into another Jenkins session; if you wish to model a long-running execution, you must schedule a new task after restart.
* This class is not serializable anyway.
* <p>Mainly intended for use with {@link OneOffExecutor} (from a {@link hudson.model.Queue.FlyweightTask}), of which there could be many,
* <p>Mainly intended for use with {@link OneOffExecutor} (from a {@link FlyweightTask}), of which there could be many,
* but could also be used with a heavyweight executor even though the number of executors is bounded by node configuration.
* <p>{@link ResourceController}/{@link ResourceActivity}/{@link ResourceList}/{@link Resource} are not currently supported.
* Nor are {@link hudson.model.Queue.Task#getSubTasks} other than the primary task.
......@@ -53,6 +55,7 @@ import org.kohsuke.accmod.restrictions.NoExternalUse;
public abstract class AsynchronousExecution extends RuntimeException {
private Executor executor;
private Throwable result;
/** Constructor for subclasses. */
protected AsynchronousExecution() {}
......@@ -76,6 +79,10 @@ public abstract class AsynchronousExecution extends RuntimeException {
/**
* Allows an executable to control whether or not to display {@code executorCell.jelly}.
*
* <p>
* If this method returns false, the asynchronous execution becomes invisible from UI.
*
* @return traditionally always true
*/
public abstract boolean displayCell();
......@@ -83,21 +90,33 @@ public abstract class AsynchronousExecution extends RuntimeException {
/**
* Obtains the associated executor.
*/
public final Executor getExecutor() {
public synchronized final Executor getExecutor() {
return executor;
}
@Restricted(NoExternalUse.class)
public final void setExecutor(Executor executor) {
public synchronized final void setExecutor(Executor executor) {
assert this.executor==null;
this.executor = executor;
if (result!=null) {
executor.completedAsynchronous( result!=NULL ? result : null );
result = null;
}
}
/**
* To be called when the task is actually complete.
* @param error normally null (preferable to handle errors yourself), but may be specified to simulate an exception from {@link hudson.model.Queue.Executable#run}, as per {@link ExecutorListener#taskCompletedWithProblems}
* @param error normally null (preferable to handle errors yourself), but may be specified to simulate an exception from {@link Executable#run}, as per {@link ExecutorListener#taskCompletedWithProblems}
*/
public final void completed(@CheckForNull Throwable error) {
executor.completedAsynchronous(error);
public synchronized final void completed(@CheckForNull Throwable error) {
if (executor!=null) {
executor.completedAsynchronous(error);
} else {
result = error == null ? NULL : error;
}
}
private static final Throwable NULL = new Throwable("NULL");
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册