diff --git a/core/src/main/java/hudson/slaves/SlaveComputer.java b/core/src/main/java/hudson/slaves/SlaveComputer.java index afd5a81b7d..cc1f5cdaa9 100644 --- a/core/src/main/java/hudson/slaves/SlaveComputer.java +++ b/core/src/main/java/hudson/slaves/SlaveComputer.java @@ -54,6 +54,7 @@ import hudson.util.StreamTaskListener; import hudson.util.VersionNumber; import hudson.util.io.RewindableFileOutputStream; import hudson.util.io.RewindableRotatingFileOutputStream; +import jenkins.agents.AgentComputerUtil; import jenkins.model.Jenkins; import jenkins.security.ChannelConfigurator; import jenkins.security.MasterToSlaveCallable; @@ -1067,17 +1068,11 @@ public class SlaveComputer extends Computer { * * @return null if the calling thread doesn't have any trace of where its master is. * @since 1.362 + * @deprecated Use {@link AgentComputerUtil#getChannelToMaster()} instead. */ + @Deprecated public static VirtualChannel getChannelToMaster() { - if (Jenkins.getInstanceOrNull()!=null) // check if calling thread is on master or on slave - return FilePath.localChannel; - - // if this method is called from within the agent computation thread, this should work - Channel c = Channel.current(); - if (c!=null && Boolean.TRUE.equals(c.getProperty("slave"))) - return c; - - return null; + return AgentComputerUtil.getChannelToMaster(); } /** diff --git a/core/src/main/java/hudson/util/ProcessTree.java b/core/src/main/java/hudson/util/ProcessTree.java index b12a967a80..8e7bcb7e6c 100644 --- a/core/src/main/java/hudson/util/ProcessTree.java +++ b/core/src/main/java/hudson/util/ProcessTree.java @@ -33,7 +33,7 @@ import hudson.FilePath; import hudson.Util; import hudson.remoting.Channel; import hudson.remoting.VirtualChannel; -import hudson.slaves.SlaveComputer; +import jenkins.agents.AgentComputerUtil; import hudson.util.ProcessKillingVeto.VetoCause; import hudson.util.ProcessTree.OSProcess; import hudson.util.ProcessTreeRemoting.IOSProcess; @@ -193,7 +193,7 @@ public abstract class ProcessTree implements Iterable, IProcessTree, /*package*/ final List getKillers() throws InterruptedException { if (killers==null) try { - VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster(); + VirtualChannel channelToMaster = AgentComputerUtil.getChannelToMaster(); if (channelToMaster!=null) { killers = channelToMaster.call(new ListAll()); } else { @@ -288,7 +288,7 @@ public abstract class ProcessTree implements Iterable, IProcessTree, // Quick check, does anything exist to check against if (!skipVetoes) { try { - VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster(); + VirtualChannel channelToMaster = AgentComputerUtil.getChannelToMaster(); if (channelToMaster!=null) { CheckVetoes vetoCheck = new CheckVetoes(this); causeMessage = channelToMaster.call(vetoCheck); @@ -429,7 +429,7 @@ public abstract class ProcessTree implements Iterable, IProcessTree, // Check for the existance of vetoers if I don't know already if (vetoersExist == null) { try { - VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster(); + VirtualChannel channelToMaster = AgentComputerUtil.getChannelToMaster(); if (channelToMaster != null) { vetoersExist = channelToMaster.call(new DoVetoersExist()); } diff --git a/core/src/main/java/jenkins/agents/AgentComputerUtil.java b/core/src/main/java/jenkins/agents/AgentComputerUtil.java new file mode 100644 index 0000000000..3aa026124e --- /dev/null +++ b/core/src/main/java/jenkins/agents/AgentComputerUtil.java @@ -0,0 +1,60 @@ +/* + * The MIT License + * + * Copyright 2020 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 jenkins.agents; + +import edu.umd.cs.findbugs.annotations.CheckForNull; +import hudson.FilePath; +import hudson.remoting.Channel; +import hudson.remoting.VirtualChannel; +import jenkins.model.Jenkins; + +public final class AgentComputerUtil { + private AgentComputerUtil() { + } + + /** + * Obtains a {@link VirtualChannel} that allows some computation to be performed on the master. + * This method can be called from any thread on the master, or from agent (more precisely, + * it only works from the remoting request-handling thread in agents, which means if you've started + * separate thread on agents, that'll fail.) + * + * @return null if the calling thread doesn't have any trace of where its master is. + * @since TODO + */ + @CheckForNull + public static VirtualChannel getChannelToMaster() { + if (Jenkins.getInstanceOrNull() != null) {// check if calling thread is on master or on agent + return FilePath.localChannel; + } + + // if this method is called from within the agent computation thread, this should work + Channel c = Channel.current(); + if (c != null && Boolean.TRUE.equals(c.getProperty("slave"))) { + return c; + } + + return null; + } +}