-
-
Notifications
You must be signed in to change notification settings - Fork 105
[FIXED JENKINS-41755] background task execution #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...java/org/jenkinsci/plugins/workflow/steps/durable_task/BackgroundDurableTaskJoinStep.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package org.jenkinsci.plugins.workflow.steps.durable_task; | ||
|
|
||
| import hudson.Extension; | ||
| import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl; | ||
| import org.jenkinsci.plugins.workflow.steps.Step; | ||
| import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
| import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
| import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Waits for the task forked in {@code sh(background:true, ...)} to complete | ||
| * | ||
| * TODO: native timeout support. in the mean time, combine with the timeout step | ||
| * | ||
| * @author Kohsuke Kawaguchi | ||
| * @see BackgroundTask | ||
| */ | ||
| public class BackgroundDurableTaskJoinStep extends Step { | ||
| private final BackgroundTask t; | ||
|
|
||
| @DataBoundConstructor | ||
| public BackgroundDurableTaskJoinStep(BackgroundTask t) { | ||
| this.t = t; | ||
| } | ||
|
|
||
| @Override | ||
| public StepExecution start(StepContext context) throws Exception { | ||
| return new Execution(context, t.getExecution()); | ||
| } | ||
|
|
||
| static final class Execution extends AbstractStepExecutionImpl { | ||
| private DurableTaskStep.Execution task; | ||
| public Execution(StepContext context, DurableTaskStep.Execution t) { | ||
| super(context); | ||
| this.task = t; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean start() throws Exception { | ||
| task.addCompletionHandler(getContext()); | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void stop(@Nonnull Throwable cause) throws Exception { | ||
| // interrupting this step shouldn't cause the process to die | ||
| // DO NOT: task.stop(cause); | ||
| } | ||
| } | ||
|
|
||
| @Extension | ||
| public static class DescriptorImpl extends StepDescriptor { | ||
| @Override | ||
| public Set<? extends Class<?>> getRequiredContext() { | ||
| return Collections.emptySet(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getFunctionName() { | ||
| return "backgroundDurableTaskJoin"; | ||
| } | ||
|
|
||
| /** | ||
| * Marking as advanced for now since this step is | ||
| * meant to be used in {@link BackgroundTask#join()} | ||
| * | ||
| * If we are to open this up to the general audience | ||
| * it should get a better name | ||
| */ | ||
| @Override | ||
| public boolean isAdvanced() { | ||
| return true; | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/BackgroundTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package org.jenkinsci.plugins.workflow.steps.durable_task; | ||
|
|
||
| import hudson.AbortException; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * Represents an object that tracks background task | ||
| * forked off by {@code sh(background:true)} | ||
| * | ||
| * <p> | ||
| * This object is serialized along with the pipeline program. | ||
| * | ||
| * @author Kohsuke Kawaguchi | ||
| */ | ||
| public class BackgroundTask implements Serializable { | ||
| private final DurableTaskStep.Execution execution; | ||
|
|
||
| /*package*/ BackgroundTask(DurableTaskStep.Execution execution) { | ||
| this.execution = execution; | ||
| } | ||
|
|
||
| /*package*/ DurableTaskStep.Execution getExecution() { | ||
| return execution; | ||
| } | ||
|
|
||
| /** | ||
| * Suspends until the process is done. | ||
| * | ||
| * @see BackgroundDurableTaskJoinStep | ||
| */ | ||
| public int join() { | ||
| // currently cannot be implemented as an instance method | ||
| // because this module doesn't depend on workflow-cps. | ||
| // use BackgroundDurableTaskJoinStep | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Immediately kills the task. | ||
| */ | ||
| public void kill() throws Exception { | ||
| execution.stop(new AbortException()); | ||
| } | ||
|
|
||
| /* | ||
| def proc = sh(background:true, script:'android something') | ||
|
|
||
| proc.join() | ||
| */ | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/FutureCallbackProxy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.jenkinsci.plugins.workflow.steps.durable_task; | ||
|
|
||
| import com.google.common.util.concurrent.FutureCallback; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * {@link FutureCallback} that buffers the result and forwards | ||
| * it to any number of {@link FutureCallback}s. | ||
| * @author Kohsuke Kawaguchi | ||
| */ | ||
| final class FutureCallbackProxy<T> implements FutureCallback<T>, Serializable { | ||
| private T result; | ||
| private Throwable t; | ||
| private boolean completed; | ||
|
|
||
| private final List<FutureCallback<? super T>> callbacks = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void onSuccess(T result) { | ||
| this.result = result; | ||
| fire(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Throwable t) { | ||
| this.t = t; | ||
| fire(); | ||
| } | ||
|
|
||
| private void fire() { | ||
| List<FutureCallback<? super T>> clone; | ||
| synchronized (this) { | ||
| completed = true; | ||
| clone = new ArrayList<>(callbacks); | ||
| } | ||
| for (FutureCallback<? super T> c : clone) { | ||
| fire(c); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private void fire(FutureCallback<? super T> c) { | ||
| if (t!=null) | ||
| c.onFailure(t); | ||
| else | ||
| c.onSuccess(result); | ||
| } | ||
|
|
||
| public void addCallback(FutureCallback<? super T> callback) { | ||
| boolean fireNow = false; | ||
| synchronized (this) { | ||
| if (!completed) | ||
| callbacks.add(callback); | ||
| else | ||
| fireNow = true; | ||
| } | ||
|
|
||
| if (fireNow) | ||
| fire(callback); | ||
| } | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eek!