Skip to content

Commit

Permalink
ensure agents are taken offline/online
Browse files Browse the repository at this point in the history
  • Loading branch information
mawinter69 committed Oct 18, 2023
1 parent 6055faa commit aabe063
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;

/**
Expand Down Expand Up @@ -92,38 +90,8 @@ protected long getWarningThresholdBytes(Computer c) {

@Override
public Object data(Computer c) {
DiskSpace size = markNodeOfflineIfDiskspaceIsTooLow(c);

// mark online (again), if free space is over threshold
if (size != null && size.size > getThresholdBytes(c) && c.isOffline() && c.getOfflineCause() instanceof DiskSpace)
if (this.getClass().equals(((DiskSpace) c.getOfflineCause()).getTrigger()))
if (getDescriptor().markOnline(c)) {
LOGGER.info(Messages.DiskSpaceMonitor_MarkedOnline(c.getDisplayName()));
}
return size;
}

/**
* Marks the given node as offline if free disk space is below the configured threshold.
* @param c the node
* @return the free space
* @since 1.521
*/
@Restricted(NoExternalUse.class)
public DiskSpace markNodeOfflineIfDiskspaceIsTooLow(Computer c) {
DiskSpace size = (DiskSpace) super.data(c);
long threshold = getThresholdBytes(c);
if (size != null) {
size.setThreshold(threshold);
long warningThreshold = getWarningThresholdBytes(c);
size.setWarningThreshold(warningThreshold);
if (size.size < threshold) {
size.setTriggered(this.getClass(), true);
if (getDescriptor().markOffline(c, size)) {
LOGGER.warning(Messages.DiskSpaceMonitor_MarkedOffline(c.getDisplayName()));
}
}
}
((DiskSpaceMonitorDescriptor) getDescriptor()).markNodeOfflineOrOnline(c, size, this);
return size;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
package hudson.node_monitors;

import hudson.Functions;
import hudson.model.Computer;
import hudson.model.ComputerSet;
import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
import hudson.remoting.VirtualChannel;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;
import jenkins.MasterToSlaveFileCallable;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
Expand All @@ -46,6 +50,50 @@
* @since 1.520
*/
public abstract class DiskSpaceMonitorDescriptor extends AbstractAsyncNodeMonitorDescriptor<DiskSpace> {

private static final Logger LOGGER = Logger.getLogger(DiskSpaceMonitorDescriptor.class.getName());

@Override
protected Map<Computer, DiskSpace> monitor() throws InterruptedException {
Result<DiskSpace> base = monitorDetailed();
Map<Computer, DiskSpace> data = base.getMonitoringData();
AbstractDiskSpaceMonitor monitor = (AbstractDiskSpaceMonitor) ComputerSet.getMonitors().get(this);
for (Map.Entry<Computer, DiskSpace> e : data.entrySet()) {
Computer c = e.getKey();
DiskSpace d = e.getValue();
if (base.getSkipped().contains(c)) {
assert d == null;
continue;
}
if (d == null) {
e.setValue(d = get(c));
}
markNodeOfflineOrOnline(c, d, monitor);
}
return data;
}

@Restricted(NoExternalUse.class)
public void markNodeOfflineOrOnline(Computer c, DiskSpace size, AbstractDiskSpaceMonitor monitor) {
if (size != null) {
long threshold = monitor.getThresholdBytes(c);
size.setThreshold(threshold);
long warningThreshold = monitor.getWarningThresholdBytes(c);
size.setWarningThreshold(warningThreshold);
if (size.size <= threshold) {
size.setTriggered(monitor.getClass(), true);
if (markOffline(c, size)) {
LOGGER.warning(Messages.DiskSpaceMonitor_MarkedOffline(c.getDisplayName()));
}
}
if (size.size > threshold && c.isOffline() && c.getOfflineCause() instanceof DiskSpace)
if (monitor.getClass().equals(((DiskSpace) c.getOfflineCause()).getTrigger()))
if (markOnline(c)) {
LOGGER.info(Messages.DiskSpaceMonitor_MarkedOnline(c.getDisplayName()));
}
}
}

/**
* Value object that represents the disk space.
*/
Expand Down Expand Up @@ -183,7 +231,11 @@ public static DiskSpace parse(String size) throws ParseException {
}
}

return new DiskSpace("", (long) (Double.parseDouble(size.trim()) * multiplier));
try {
return new DiskSpace("", (long) (Double.parseDouble(size.trim()) * multiplier));
} catch (NumberFormatException nfe) {
throw new ParseException(nfe.getLocalizedMessage(), 0);
}
}

private static final long serialVersionUID = 2L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
is found to have less free disk space than this amount, it will be marked
temporarily offline.
<p>Set to 0 to disable this check on this agent.</p>
<p>Set to empty to use the globally defined value.</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
is found to have less free disk space than this amount, a warning will be
shown for it on the computer overview page, but it will not be taken offline.
<p>Set to 0 to disable this warning on this agent.</p>
<p>Set to empty to use the globally defined value.</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
is found to have less free temp space than this amount, it will be marked
temporarily offline.
<p>Set to 0 to disable this check on this agent.</p>
<p>Set to empty to use the globally defined value.</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
is found to have less free temp space than this amount, a warning will be
shown for it on the computer overview page, but it will not be taken offline.
<p>Set to 0 to disable this warning on this agent.</p>
<p>Set to empty to use the globally defined value.</p>
</div>

0 comments on commit aabe063

Please sign in to comment.