Skip to content

Commit 4e6026a

Browse files
authored
Merge pull request #158 from jglick/GeneralNonBlockingStepExecution-JENKINS-49337
Using GeneralNonBlockingStepExecution
2 parents 648f335 + 6066fbf commit 4e6026a

File tree

6 files changed

+168
-44
lines changed

6 files changed

+168
-44
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<changelist>-SNAPSHOT</changelist>
3434
<jenkins.version>2.60.3</jenkins.version>
3535
<java.level>8</java.level>
36-
<workflow-step-api-plugin.version>2.9</workflow-step-api-plugin.version>
36+
<workflow-step-api-plugin.version>2.18</workflow-step-api-plugin.version>
3737
<workflow-support-plugin.version>2.12</workflow-support-plugin.version>
3838
<workflow-cps-plugin.version>2.25</workflow-cps-plugin.version>
3939
</properties>

src/main/java/org/jenkinsci/plugins/docker/workflow/AbstractEndpointStepExecution.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,18 @@
3434
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
3535
import org.jenkinsci.plugins.workflow.steps.StepContext;
3636

37+
/** @deprecated only here for binary compatibility */
38+
@Deprecated
3739
abstract class AbstractEndpointStepExecution extends AbstractStepExecutionImpl {
3840

3941
private static final long serialVersionUID = 1;
4042

41-
protected abstract KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException;
43+
protected KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException {
44+
throw new AssertionError();
45+
}
4246

4347
@Override public final boolean start() throws Exception {
44-
KeyMaterialFactory keyMaterialFactory = newKeyMaterialFactory();
45-
KeyMaterial material = keyMaterialFactory.materialize();
46-
getContext().newBodyInvoker().
47-
withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new Expander(material))).
48-
withCallback(new Callback(material)).
49-
start();
50-
return false;
48+
throw new AssertionError();
5149
}
5250

5351
@Override public final void stop(Throwable cause) throws Exception {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2015, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package org.jenkinsci.plugins.docker.workflow;
25+
26+
import hudson.EnvVars;
27+
import java.io.IOException;
28+
import java.util.logging.Level;
29+
import java.util.logging.Logger;
30+
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterial;
31+
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
32+
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
33+
import org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution;
34+
import org.jenkinsci.plugins.workflow.steps.StepContext;
35+
36+
abstract class AbstractEndpointStepExecution2 extends GeneralNonBlockingStepExecution {
37+
38+
private static final long serialVersionUID = 1;
39+
40+
protected AbstractEndpointStepExecution2(StepContext context) {
41+
super(context);
42+
}
43+
44+
protected abstract KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException;
45+
46+
@Override public final boolean start() throws Exception {
47+
run(this::doStart);
48+
return false;
49+
}
50+
51+
private void doStart() throws Exception {
52+
KeyMaterialFactory keyMaterialFactory = newKeyMaterialFactory();
53+
KeyMaterial material = keyMaterialFactory.materialize();
54+
getContext().newBodyInvoker().
55+
withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new Expander(material))).
56+
withCallback(new Callback(material)).
57+
start();
58+
}
59+
60+
private static class Expander extends EnvironmentExpander {
61+
62+
private static final long serialVersionUID = 1;
63+
private final KeyMaterial material;
64+
65+
Expander(KeyMaterial material) {
66+
this.material = material;
67+
}
68+
69+
@Override public void expand(EnvVars env) throws IOException, InterruptedException {
70+
env.putAll(material.env());
71+
}
72+
73+
}
74+
75+
private class Callback extends TailCall {
76+
77+
private static final long serialVersionUID = 1;
78+
private final KeyMaterial material;
79+
80+
Callback(KeyMaterial material) {
81+
this.material = material;
82+
}
83+
84+
@Override protected void finished(StepContext context) throws Exception {
85+
try {
86+
material.close();
87+
} catch (IOException x) {
88+
Logger.getLogger(AbstractEndpointStepExecution2.class.getName()).log(Level.WARNING, null, x);
89+
}
90+
}
91+
92+
}
93+
94+
}

src/main/java/org/jenkinsci/plugins/docker/workflow/RegistryEndpointStep.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package org.jenkinsci.plugins.docker.workflow;
2525

26-
import com.google.inject.Inject;
26+
import com.google.common.collect.ImmutableSet;
2727
import hudson.EnvVars;
2828
import hudson.Extension;
2929
import hudson.FilePath;
@@ -36,21 +36,22 @@
3636
import java.util.Collections;
3737
import java.util.HashMap;
3838
import java.util.Map;
39+
import java.util.Set;
3940
import java.util.TreeMap;
4041
import javax.annotation.CheckForNull;
4142
import javax.annotation.Nonnull;
4243
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
4344
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
4445
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
4546
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
46-
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
47-
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
4847
import org.jenkinsci.plugins.workflow.steps.Step;
49-
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
48+
import org.jenkinsci.plugins.workflow.steps.StepContext;
49+
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
50+
import org.jenkinsci.plugins.workflow.steps.StepExecution;
5051
import org.kohsuke.stapler.DataBoundConstructor;
5152
import org.kohsuke.stapler.DataBoundSetter;
5253

53-
public class RegistryEndpointStep extends AbstractStepImpl {
54+
public class RegistryEndpointStep extends Step {
5455

5556
private final @Nonnull DockerRegistryEndpoint registry;
5657
private @CheckForNull String toolName;
@@ -72,29 +73,39 @@ public String getToolName() {
7273
this.toolName = Util.fixEmpty(toolName);
7374
}
7475

75-
public static class Execution extends AbstractEndpointStepExecution {
76+
@Override public StepExecution start(StepContext context) throws Exception {
77+
return new Execution2(this, context);
78+
}
79+
80+
private static final class Execution2 extends AbstractEndpointStepExecution2 {
7681

7782
private static final long serialVersionUID = 1;
7883

79-
@Inject(optional=true) private transient RegistryEndpointStep step;
80-
@StepContextParameter private transient FilePath workspace;
81-
@StepContextParameter private transient Launcher launcher;
82-
@StepContextParameter private transient TaskListener listener;
83-
@StepContextParameter private transient Node node;
84-
@StepContextParameter private transient EnvVars envVars;
85-
@StepContextParameter private transient Run run;
84+
private transient RegistryEndpointStep step;
85+
86+
Execution2(RegistryEndpointStep step, StepContext context) {
87+
super(context);
88+
this.step = step;
89+
}
8690

8791
@Override protected KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException {
88-
return step.registry.newKeyMaterialFactory(run, workspace, launcher, envVars, listener, DockerTool.getExecutable(step.toolName, node, listener, envVars));
92+
TaskListener listener = getContext().get(TaskListener.class);
93+
EnvVars envVars = getContext().get(EnvVars.class);
94+
String executable = DockerTool.getExecutable(step.toolName, getContext().get(Node.class), listener, envVars);
95+
return step.registry.newKeyMaterialFactory(getContext().get(Run.class), getContext().get(FilePath.class), getContext().get(Launcher.class), envVars, listener, executable);
8996
}
9097

9198
}
9299

93-
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {
100+
/** @deprecated only here for binary compatibility */
101+
@Deprecated
102+
public static class Execution extends AbstractEndpointStepExecution {
94103

95-
public DescriptorImpl() {
96-
super(Execution.class);
97-
}
104+
private static final long serialVersionUID = 1;
105+
106+
}
107+
108+
@Extension public static class DescriptorImpl extends StepDescriptor {
98109

99110
@Override public String getFunctionName() {
100111
return "withDockerRegistry";
@@ -135,6 +146,11 @@ public DescriptorImpl() {
135146
return super.newInstance(arguments);
136147
}
137148

149+
@SuppressWarnings("unchecked")
150+
@Override public Set<? extends Class<?>> getRequiredContext() {
151+
return ImmutableSet.of(TaskListener.class, EnvVars.class, Node.class, Run.class, FilePath.class, Launcher.class);
152+
}
153+
138154
}
139155

140156
}

src/main/java/org/jenkinsci/plugins/docker/workflow/ServerEndpointStep.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@
2323
*/
2424
package org.jenkinsci.plugins.docker.workflow;
2525

26-
import com.google.inject.Inject;
26+
import com.google.common.collect.ImmutableSet;
2727
import hudson.Extension;
2828
import hudson.FilePath;
29+
import hudson.model.Run;
2930
import java.io.IOException;
31+
import java.util.Set;
3032
import javax.annotation.Nonnull;
31-
32-
import hudson.model.Run;
3333
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
3434
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
35-
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
36-
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
37-
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
35+
import org.jenkinsci.plugins.workflow.steps.Step;
36+
import org.jenkinsci.plugins.workflow.steps.StepContext;
37+
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
38+
import org.jenkinsci.plugins.workflow.steps.StepExecution;
3839
import org.kohsuke.stapler.DataBoundConstructor;
3940

40-
public class ServerEndpointStep extends AbstractStepImpl {
41+
public class ServerEndpointStep extends Step {
4142

4243
private final @Nonnull DockerServerEndpoint server;
4344

@@ -50,25 +51,36 @@ public DockerServerEndpoint getServer() {
5051
return server;
5152
}
5253

53-
public static class Execution extends AbstractEndpointStepExecution {
54+
@Override public StepExecution start(StepContext context) throws Exception {
55+
return new Execution2(this, context);
56+
}
57+
58+
private static final class Execution2 extends AbstractEndpointStepExecution2 {
5459

5560
private static final long serialVersionUID = 1;
5661

57-
@Inject(optional=true) private transient ServerEndpointStep step;
58-
@StepContextParameter private transient Run run;
59-
@StepContextParameter private transient FilePath workspace;
62+
private transient final ServerEndpointStep step;
63+
64+
Execution2(ServerEndpointStep step, StepContext context) {
65+
super(context);
66+
this.step = step;
67+
}
6068

6169
@Override protected KeyMaterialFactory newKeyMaterialFactory() throws IOException, InterruptedException {
62-
return step.server.newKeyMaterialFactory(run, workspace.getChannel());
70+
return step.server.newKeyMaterialFactory(getContext().get(Run.class), getContext().get(FilePath.class).getChannel());
6371
}
6472

6573
}
6674

67-
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {
75+
/** @deprecated only here for binary compatibility */
76+
@Deprecated
77+
public static class Execution extends AbstractEndpointStepExecution {
6878

69-
public DescriptorImpl() {
70-
super(Execution.class);
71-
}
79+
private static final long serialVersionUID = 1;
80+
81+
}
82+
83+
@Extension public static class DescriptorImpl extends StepDescriptor {
7284

7385
@Override public String getFunctionName() {
7486
return "withDockerServer";
@@ -86,8 +98,11 @@ public DescriptorImpl() {
8698
return true;
8799
}
88100

89-
// TODO allow DockerServerEndpoint fields to be inlined, as in RegistryEndpointStep, so Docker.groovy can say simply: script.withDockerServer(uri: uri, credentialsId: credentialsId) {…}
101+
@Override public Set<? extends Class<?>> getRequiredContext() {
102+
return ImmutableSet.of(Run.class, FilePath.class);
103+
}
90104

105+
// TODO allow DockerServerEndpoint fields to be inlined, as in RegistryEndpointStep, so Docker.groovy can say simply: script.withDockerServer(uri: uri, credentialsId: credentialsId) {…}
91106
}
92107

93108
}

src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private static void destroy(String container, Launcher launcher, Node node, EnvV
109109
new DockerClient(launcher, node, toolName).stop(launcherEnv, container);
110110
}
111111

112+
// TODO switch to GeneralNonBlockingStepExecution
112113
public static class Execution extends AbstractStepExecutionImpl {
113114

114115
private static final long serialVersionUID = 1;

0 commit comments

Comments
 (0)