Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-30269] Step execution implementation
- Loading branch information
Showing
5 changed files
with
187 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
38 changes: 37 additions & 1 deletion
38
src/main/java/org/jenkins/plugins/lockableresources/LockStep.java
This file contains 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 |
---|---|---|
@@ -1,5 +1,41 @@ | ||
package org.jenkins.plugins.lockableresources; | ||
|
||
public class LockStep { | ||
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl; | ||
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import hudson.Extension; | ||
|
||
public class LockStep extends AbstractStepImpl { | ||
|
||
public final String resource; | ||
|
||
@DataBoundConstructor | ||
public LockStep(String resource) { | ||
if (resource == null || resource.isEmpty()) { | ||
throw new IllegalArgumentException("must specify resource"); | ||
} | ||
this.resource = resource; | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends AbstractStepDescriptorImpl { | ||
|
||
public DescriptorImpl() { | ||
super(LockStepExecution.class); | ||
} | ||
|
||
@Override public String getFunctionName() { | ||
return "lock"; | ||
} | ||
|
||
@Override public String getDisplayName() { | ||
return "Lock"; | ||
} | ||
|
||
@Override public boolean takesImplicitBlockArgument() { | ||
return true; | ||
} | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/org/jenkins/plugins/lockableresources/LockStepExecution.java
This file contains 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,99 @@ | ||
package org.jenkins.plugins.lockableresources; | ||
|
||
import java.util.Arrays; | ||
import java.util.UUID; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl; | ||
import org.jenkinsci.plugins.workflow.steps.BodyExecution; | ||
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepContextParameter; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.inject.Inject; | ||
|
||
import hudson.Util; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import jenkins.util.Timer; | ||
|
||
public class LockStepExecution extends AbstractStepExecutionImpl { | ||
|
||
@Inject(optional = true) | ||
private transient LockStep step; | ||
|
||
@StepContextParameter | ||
private transient Run<?, ?> run; | ||
|
||
@StepContextParameter | ||
private transient TaskListener listener; | ||
|
||
private transient volatile ScheduledFuture<?> task; | ||
|
||
private BodyExecution body; | ||
private final String id = UUID.randomUUID().toString(); | ||
|
||
|
||
@Override | ||
public boolean start() throws Exception { | ||
tryLock(); | ||
return false; | ||
} | ||
|
||
private void tryLock() { | ||
getContext().saveState(); | ||
Timer.get().schedule(new Runnable() { | ||
@Override | ||
public void run() { | ||
LockableResource resource = new LockableResource(step.resource); | ||
if (!proceed()) { | ||
tryLock(); | ||
} | ||
} | ||
}, 1000, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
private boolean proceed() { | ||
LockableResource resource = new LockableResource(step.resource); | ||
if (LockableResourcesManager.get().lock(Arrays.asList(new LockableResource[] { resource }), run)) { | ||
listener.getLogger().println("Lock aquired on [" + step.resource + "]"); | ||
body = getContext().newBodyInvoker(). | ||
withCallback(new Callback(resource, run)). | ||
withDisplayName(null). | ||
start(); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
private static final class Callback extends BodyExecutionCallback.TailCall { | ||
|
||
private final LockableResource resource; | ||
private final Run<?, ?> run; | ||
|
||
Callback(LockableResource resource, Run<?, ?> run) { | ||
this.resource = resource; | ||
this.run = run; | ||
} | ||
|
||
@Override | ||
protected void finished(StepContext context) throws Exception { | ||
LockableResourcesManager.get().unlock(Arrays.asList(new LockableResource[] { resource }), run); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void stop(Throwable cause) throws Exception { | ||
// TODO does it need implementation? | ||
} | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
} |
This file contains 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
This file contains 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