-
Notifications
You must be signed in to change notification settings - Fork 98
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
[JENKINS-49707] Defining AgentErrorCondition
#231
Changes from 2 commits
e842533
c2ba47d
f46a1da
7755f5f
3d3a91c
191fe4f
5e84f17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2021 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.support.steps; | ||
|
||
import hudson.Extension; | ||
import hudson.FilePath; | ||
import hudson.Launcher; | ||
import hudson.model.Computer; | ||
import hudson.model.Executor; | ||
import hudson.model.Node; | ||
import hudson.slaves.WorkspaceList; | ||
import java.io.EOFException; | ||
import java.io.IOException; | ||
import java.nio.channels.ClosedChannelException; | ||
import java.util.stream.Stream; | ||
import org.jenkinsci.Symbol; | ||
import org.jenkinsci.plugins.workflow.flow.ErrorCondition; | ||
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException; | ||
import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.Beta; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* Determines whether a failure in {@link ExecutorStep} should be retried. | ||
*/ | ||
@Restricted(Beta.class) | ||
public final class AgentErrorCondition extends ErrorCondition { | ||
|
||
@DataBoundConstructor public AgentErrorCondition() {} | ||
|
||
@Override public boolean test(Throwable t, StepContext context) throws IOException, InterruptedException { | ||
if (t instanceof FlowInterruptedException && ((FlowInterruptedException) t).getCauses().stream().anyMatch(ExecutorStepExecution.RemovedNodeCause.class::isInstance)) { | ||
return true; | ||
} | ||
if (isClosedChannel(t)) { | ||
return true; | ||
} | ||
if (t instanceof MissingContextVariableException) { | ||
Class<?> type = ((MissingContextVariableException) t).getType(); | ||
// See ExecutorStepDynamicContext for four explicitly advertised types, & DefaultStepContext for two implicitly derived ones. | ||
// ExecutorStepExecution also offers EnvVars in context, but this is available to all builds anyway. | ||
if (type == FilePath.class || type == WorkspaceList.Lease.class || type == Computer.class || type == Executor.class || type == Node.class || type == Launcher.class) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isClosedChannel(Throwable t) { | ||
if (t instanceof ClosedChannelException) { | ||
return true; | ||
} else if (t instanceof EOFException) { | ||
return true; | ||
} else if (t == null) { | ||
return false; | ||
} else { | ||
return isClosedChannel(t.getCause()) || Stream.of(t.getSuppressed()).anyMatch(AgentErrorCondition::isClosedChannel); | ||
} | ||
} | ||
|
||
@Symbol("agent") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noting collision with (a probably very rarely installed plugin - only alpha releases afaict) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be OK. https://javadoc.jenkins.io/component/symbol-annotation/org/jenkinsci/Symbol.html
|
||
@Extension public static final class DescriptorImpl extends ErrorConditionDescriptor { | ||
|
||
@Override public String getDisplayName() { | ||
return "Agent errors"; | ||
} | ||
|
||
} | ||
|
||
} |
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.
Comment really belongs in #180, and perhaps this whole
MissingContextVariableException
clause might be active only in that mode, but seems harmless enough to include this logic here.