From 8917e4856434b4e594517aac705dfadb62104045 Mon Sep 17 00:00:00 2001 From: Chris Foote Date: Wed, 23 Oct 2024 03:02:16 +1300 Subject: [PATCH] Reinstate the released lock messages for freestyle builds (removed by #673) --- .../LockableResourcesManager.java | 20 ++++++++++-------- .../queue/LockRunListener.java | 21 +++++++++++++++---- .../FreeStyleProjectTest.java | 1 + .../lockableresources/NodesMirrorTest.java | 2 +- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java b/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java index fce804f98..f56f9e9a0 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java @@ -649,32 +649,34 @@ private void freeResources(List unlockResources, Run bui removeResources(toBeRemoved); } - public void unlockBuild(@Nullable Run build) { + @NonNull + public List unlockBuild(@Nullable Run build) { if (build == null) { - return; + return Collections.emptyList(); } List resourcesInUse = LockedResourcesBuildAction.findAndInitAction(build).getCurrentUsedResourceNames(); - - if (resourcesInUse.size() == 0) { - return; - } - unlockNames(resourcesInUse, build); + return unlockNames(resourcesInUse, build); } // --------------------------------------------------------------------------- + @NonNull @SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "not sure which exceptions might be catch.") - public void unlockNames(@Nullable List resourceNamesToUnLock, Run build) { + public List unlockNames(@Nullable List resourceNamesToUnLock, Run build) { // make sure there is a list of resource names to unlock if (resourceNamesToUnLock == null || resourceNamesToUnLock.isEmpty()) { - return; + return Collections.emptyList(); } + + List unlockedResourceNames; synchronized (this.syncResources) { + unlockedResourceNames = new ArrayList<>(resourceNamesToUnLock); unlockResources(this.fromNames(resourceNamesToUnLock), build); } + return unlockedResourceNames; } // --------------------------------------------------------------------------- diff --git a/src/main/java/org/jenkins/plugins/lockableresources/queue/LockRunListener.java b/src/main/java/org/jenkins/plugins/lockableresources/queue/LockRunListener.java index 82d43318a..0b02b3f51 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/queue/LockRunListener.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/queue/LockRunListener.java @@ -97,8 +97,15 @@ public void onCompleted(Run build, @NonNull TaskListener listener) { // Skip unlocking for multiple configuration projects, // only the child jobs will actually unlock resources. if (build instanceof MatrixBuild) return; - LOGGER.info(build.getFullDisplayName()); - LockableResourcesManager.get().unlockBuild(build); + + List unlocked = LockableResourcesManager.get().unlockBuild(build); + if (!unlocked.isEmpty()) { + listener.getLogger().printf("%s released lock on %s%n", LOG_PREFIX, unlocked); + LOGGER.info(build.getFullDisplayName() + + " released lock on " + + unlocked + + ", because the build has finished."); + } } @Override @@ -106,7 +113,13 @@ public void onDeleted(Run build) { // Skip unlocking for multiple configuration projects, // only the child jobs will actually unlock resources. if (build instanceof MatrixBuild) return; - LOGGER.info(build.getFullDisplayName()); - LockableResourcesManager.get().unlockBuild(build); + + List unlocked = LockableResourcesManager.get().unlockBuild(build); + if (!unlocked.isEmpty()) { + LOGGER.warning(build.getFullDisplayName() + + " released lock on " + + unlocked + + ", because the build has been deleted."); + } } } diff --git a/src/test/java/org/jenkins/plugins/lockableresources/FreeStyleProjectTest.java b/src/test/java/org/jenkins/plugins/lockableresources/FreeStyleProjectTest.java index 75cf28c0d..6771bd426 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/FreeStyleProjectTest.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/FreeStyleProjectTest.java @@ -259,6 +259,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen QueueTaskFuture fb2q = f2.scheduleBuild2(0); semaphore.release(); + j.waitForMessage("released lock on [shared]", fb0); j.waitForCompletion(fb0); // fb1 or fb2 might run first, it shouldn't matter as long as they both get the resource FreeStyleBuild fb1 = fb1q.waitForStart(); diff --git a/src/test/java/org/jenkins/plugins/lockableresources/NodesMirrorTest.java b/src/test/java/org/jenkins/plugins/lockableresources/NodesMirrorTest.java index 3a70d4eb3..f5fada397 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/NodesMirrorTest.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/NodesMirrorTest.java @@ -16,7 +16,7 @@ public class NodesMirrorTest { - private static final Logger LOGGER = Logger.getLogger(NodesMirror.class.getName()); + private static final Logger LOGGER = Logger.getLogger(NodesMirrorTest.class.getName()); @Rule public final JenkinsRule j = new JenkinsRule();