|
33 | 33 | import java.io.IOException; |
34 | 34 | import java.util.Map; |
35 | 35 | import java.util.Set; |
| 36 | +import java.util.logging.Level; |
| 37 | +import java.util.logging.Logger; |
36 | 38 | import jenkins.model.Jenkins; |
37 | 39 | import org.kohsuke.accmod.Restricted; |
38 | 40 | import org.kohsuke.accmod.restrictions.NoExternalUse; |
|
89 | 91 | * @see Jenkins#administrativeMonitors |
90 | 92 | */ |
91 | 93 | public abstract class AdministrativeMonitor extends AbstractModelObject implements ExtensionPoint, StaplerProxy { |
| 94 | + private static final Logger LOGGER = Logger.getLogger(AdministrativeMonitor.class.getName()); |
| 95 | + |
92 | 96 | /** |
93 | 97 | * Human-readable ID of this monitor, which needs to be unique within the system. |
94 | 98 | * |
@@ -161,13 +165,34 @@ public boolean isSnoozed() { |
161 | 165 | if (expiry == null) { |
162 | 166 | return false; |
163 | 167 | } |
164 | | - return System.currentTimeMillis() < expiry; |
| 168 | + long now = System.currentTimeMillis(); |
| 169 | + if (now >= expiry) { |
| 170 | + // Cleanup expired entry to prevent memory leak |
| 171 | + try { |
| 172 | + AbstractCIBase jenkins = Jenkins.get(); |
| 173 | + Map<String, Long> map = jenkins.getSnoozedAdministrativeMonitors(); |
| 174 | + if (map.remove(id) != null) { |
| 175 | + jenkins.setSnoozedAdministrativeMonitors(map); |
| 176 | + jenkins.save(); |
| 177 | + } |
| 178 | + } catch (IOException e) { |
| 179 | + LOGGER.log(Level.WARNING, "Failed to cleanup expired snooze for " + id, e); |
| 180 | + } |
| 181 | + return false; |
| 182 | + } |
| 183 | + return true; |
165 | 184 | } |
166 | 185 |
|
167 | 186 | /** |
168 | 187 | * @since 2.549 |
169 | 188 | */ |
170 | 189 | public void snooze(long durationMs) throws IOException { |
| 190 | + if (durationMs <= 0) { |
| 191 | + throw new IllegalArgumentException("Duration must be positive"); |
| 192 | + } |
| 193 | + if (durationMs > 365L * 24 * 60 * 60 * 1000) { |
| 194 | + throw new IllegalArgumentException("Duration exceeds maximum (1 year)"); |
| 195 | + } |
171 | 196 | long expiryTime = System.currentTimeMillis() + durationMs; |
172 | 197 | AbstractCIBase jenkins = Jenkins.get(); |
173 | 198 | Map<String, Long> map = jenkins.getSnoozedAdministrativeMonitors(); |
|
0 commit comments