-
Notifications
You must be signed in to change notification settings - Fork 96
/
DurableTaskStep.java
351 lines (315 loc) · 14.7 KB
/
DurableTaskStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* The MIT License
*
* Copyright (c) 2013-2014, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.workflow.steps.durable_task;
import com.google.common.collect.ImmutableSet;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import hudson.util.LogTaskListener;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.util.Timer;
import org.jenkinsci.plugins.durabletask.Controller;
import org.jenkinsci.plugins.durabletask.DurableTask;
import org.jenkinsci.plugins.workflow.FilePathUtils;
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.jenkinsci.plugins.workflow.support.concurrent.Timeout;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
/**
* Runs an durable task on a slave, such as a shell script.
*/
public abstract class DurableTaskStep extends Step {
private static final Logger LOGGER = Logger.getLogger(DurableTaskStep.class.getName());
private boolean returnStdout;
private String encoding = DurableTaskStepDescriptor.defaultEncoding;
private boolean returnStatus;
protected abstract DurableTask task();
public boolean isReturnStdout() {
return returnStdout;
}
@DataBoundSetter public void setReturnStdout(boolean returnStdout) {
this.returnStdout = returnStdout;
}
public String getEncoding() {
return encoding;
}
@DataBoundSetter public void setEncoding(String encoding) {
this.encoding = encoding;
}
public boolean isReturnStatus() {
return returnStatus;
}
@DataBoundSetter public void setReturnStatus(boolean returnStatus) {
this.returnStatus = returnStatus;
}
@Override public StepExecution start(StepContext context) throws Exception {
return new Execution(context, this);
}
public abstract static class DurableTaskStepDescriptor extends StepDescriptor {
public static final String defaultEncoding = "UTF-8";
public FormValidation doCheckEncoding(@QueryParameter boolean returnStdout, @QueryParameter String encoding) {
try {
Charset.forName(encoding);
} catch (Exception x) {
return FormValidation.error(x, "Unrecognized encoding");
}
if (!returnStdout && !encoding.equals(DurableTaskStepDescriptor.defaultEncoding)) {
return FormValidation.warning("encoding is ignored unless returnStdout is checked.");
}
return FormValidation.ok();
}
public FormValidation doCheckReturnStatus(@QueryParameter boolean returnStdout, @QueryParameter boolean returnStatus) {
if (returnStdout && returnStatus) {
return FormValidation.error("You may not select both returnStdout and returnStatus.");
}
return FormValidation.ok();
}
@Override public final Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(FilePath.class, EnvVars.class, Launcher.class, TaskListener.class);
}
}
/**
* Represents one task that is believed to still be running.
*/
@SuppressFBWarnings(value="SE_TRANSIENT_FIELD_NOT_RESTORED", justification="recurrencePeriod is set in onResume, not deserialization")
static final class Execution extends AbstractStepExecutionImpl implements Runnable {
private static final long MIN_RECURRENCE_PERIOD = 250; // ¼s
private static final long MAX_RECURRENCE_PERIOD = 15000; // 15s
private static final float RECURRENCE_PERIOD_BACKOFF = 1.2f;
private transient final DurableTaskStep step;
private transient FilePath ws;
private transient long recurrencePeriod;
private transient volatile ScheduledFuture<?> task, stopTask;
private transient boolean printedCannotContactMessage;
private Controller controller;
private String node;
private String remote;
private boolean returnStdout; // serialized default is false
private String encoding; // serialized default is irrelevant
private boolean returnStatus; // serialized default is false
Execution(StepContext context, DurableTaskStep step) {
super(context);
this.step = step;
}
@Override public boolean start() throws Exception {
returnStdout = step.returnStdout;
encoding = step.encoding;
returnStatus = step.returnStatus;
StepContext context = getContext();
ws = context.get(FilePath.class);
node = FilePathUtils.getNodeName(ws);
DurableTask durableTask = step.task();
if (returnStdout) {
durableTask.captureOutput();
}
controller = durableTask.launch(context.get(EnvVars.class), ws, context.get(Launcher.class), context.get(TaskListener.class));
this.remote = ws.getRemote();
setupTimer();
return false;
}
private @CheckForNull FilePath getWorkspace() throws AbortException {
if (ws == null) {
ws = FilePathUtils.find(node, remote);
if (ws == null) {
LOGGER.log(Level.FINE, "Jenkins is not running, no such node {0}, or it is offline", node);
return null;
}
}
boolean directory;
try (Timeout timeout = Timeout.limit(10, TimeUnit.SECONDS)) {
directory = ws.isDirectory();
} catch (Exception x) {
// RequestAbortedException, ChannelClosedException, EOFException, wrappers thereof; InterruptedException if it just takes too long.
LOGGER.log(Level.FINE, node + " is evidently offline now", x);
ws = null;
if (!printedCannotContactMessage) {
logger().println("Cannot contact " + node + ": " + x);
printedCannotContactMessage = true;
}
return null;
}
if (!directory) {
throw new AbortException("missing workspace " + remote + " on " + node);
}
return ws;
}
private @Nonnull PrintStream logger() {
TaskListener l;
StepContext context = getContext();
try {
l = context.get(TaskListener.class);
if (l != null) {
LOGGER.log(Level.FINEST, "JENKINS-34021: DurableTaskStep.Execution.listener present in {0}", context);
} else {
LOGGER.log(Level.WARNING, "JENKINS-34021: TaskListener not available upon request in {0}", context);
l = new LogTaskListener(LOGGER, Level.FINE);
}
} catch (Exception x) {
LOGGER.log(Level.FINE, "JENKINS-34021: could not get TaskListener in " + context, x);
l = new LogTaskListener(LOGGER, Level.FINE);
recurrencePeriod = 0;
}
return l.getLogger();
}
private @Nonnull Launcher launcher() throws IOException, InterruptedException {
StepContext context = getContext();
Launcher l = context.get(Launcher.class);
if (l == null) {
throw new IOException("JENKINS-37486: Launcher not present in " + context);
}
return l;
}
@Override public void stop(final Throwable cause) throws Exception {
FilePath workspace = getWorkspace();
if (workspace != null) {
logger().println("Sending interrupt signal to process");
LOGGER.log(Level.FINE, "stopping process", cause);
stopTask = Timer.get().schedule(new Runnable() {
@Override public void run() {
stopTask = null;
if (recurrencePeriod > 0) {
recurrencePeriod = 0;
logger().println("After 10s process did not stop");
getContext().onFailure(cause);
}
}
}, 10, TimeUnit.SECONDS);
controller.stop(workspace, launcher());
} else {
logger().println("Could not connect to " + node + " to send interrupt signal to process");
recurrencePeriod = 0;
getContext().onFailure(cause);
}
}
@Override public String getStatus() {
StringBuilder b = new StringBuilder();
try (Timeout timeout = Timeout.limit(2, TimeUnit.SECONDS)) { // CpsThreadDump applies a 3s timeout anyway
FilePath workspace = getWorkspace();
if (workspace != null) {
b.append(controller.getDiagnostics(workspace, launcher()));
} else {
b.append("waiting to reconnect to ").append(remote).append(" on ").append(node);
}
} catch (IOException | InterruptedException x) {
b.append("failed to look up workspace ").append(remote).append(" on ").append(node).append(": ").append(x);
}
b.append("; recurrence period: ").append(recurrencePeriod).append("ms");
ScheduledFuture<?> t = task;
if (t != null) {
b.append("; check task scheduled; cancelled? ").append(t.isCancelled()).append(" done? ").append(t.isDone());
}
t = stopTask;
if (t != null) {
b.append("; stop task scheduled; cancelled? ").append(t.isCancelled()).append(" done? ").append(t.isDone());
}
return b.toString();
}
/** Checks for progress or completion of the external task. */
@Override public void run() {
task = null;
try {
check();
} finally {
if (recurrencePeriod > 0) {
task = Timer.get().schedule(this, recurrencePeriod, TimeUnit.MILLISECONDS);
}
}
}
@SuppressFBWarnings(value="REC_CATCH_EXCEPTION", justification="silly rule")
private void check() {
if (recurrencePeriod == 0) { // from stop
return;
}
final FilePath workspace;
try {
workspace = getWorkspace();
} catch (AbortException x) {
recurrencePeriod = 0;
getContext().onFailure(x);
return;
}
if (workspace == null) {
return; // slave not yet ready, wait for another day
}
try (Timeout timeout = Timeout.limit(10, TimeUnit.SECONDS)) {
if (controller.writeLog(workspace, logger())) {
getContext().saveState();
recurrencePeriod = MIN_RECURRENCE_PERIOD; // got output, maybe we will get more soon
} else {
recurrencePeriod = Math.min((long) (recurrencePeriod * RECURRENCE_PERIOD_BACKOFF), MAX_RECURRENCE_PERIOD);
}
Integer exitCode = controller.exitStatus(workspace, launcher());
if (exitCode == null) {
LOGGER.log(Level.FINE, "still running in {0} on {1}", new Object[] {remote, node});
} else {
if (controller.writeLog(workspace, logger())) {
LOGGER.log(Level.FINE, "last-minute output in {0} on {1}", new Object[] {remote, node});
}
if (returnStatus || exitCode == 0) {
getContext().onSuccess(returnStatus ? exitCode : returnStdout ? new String(controller.getOutput(workspace, launcher()), encoding) : null);
} else {
if (returnStdout) {
logger().write(controller.getOutput(workspace, launcher())); // diagnostic
}
getContext().onFailure(new AbortException("script returned exit code " + exitCode));
}
recurrencePeriod = 0;
controller.cleanup(workspace);
}
} catch (Exception x) {
LOGGER.log(Level.FINE, "could not check " + workspace, x);
ws = null;
if (!printedCannotContactMessage) {
logger().println("Cannot contact " + node + ": " + x);
printedCannotContactMessage = true;
}
}
}
@Override public void onResume() {
setupTimer();
}
private void setupTimer() {
recurrencePeriod = MIN_RECURRENCE_PERIOD;
task = Timer.get().schedule(this, recurrencePeriod, TimeUnit.MILLISECONDS);
}
private static final long serialVersionUID = 1L;
}
}