Skip to content

Commit b9c5382

Browse files
authored
Merge pull request #1 from smootoo/queued-contexts-log-and-visualise
Log the QueuedContexts collection each time it changes.
2 parents d9e9788 + 5e13e6e commit b9c5382

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ public synchronized void unreserve(List<LockableResource> resources) {
556556
nextContextLogger,
557557
resourceNamesToUnreserve);
558558
this.queuedContexts.remove(nextContext);
559+
if (LOGGER.isLoggable(Level.FINE)) {
560+
LOGGER.log(Level.FINE, "Queued Contexts " + this.queuedContexts);
561+
}
559562

560563
// resourceNamesToUnreserve contains the names of the previous resources.
561564
// requiredResourceForNextContext contains the resource objects which are required for the next context.
@@ -751,13 +754,19 @@ public synchronized void queueContext(StepContext context, List<LockableResource
751754
}
752755

753756
this.queuedContexts.add(new QueuedContextStruct(context, requiredResources, resourceDescription));
757+
if (LOGGER.isLoggable(Level.FINE)) {
758+
LOGGER.log(Level.FINE, "Queued Contexts " + this.queuedContexts);
759+
}
754760
save();
755761
}
756762

757763
public synchronized boolean unqueueContext(StepContext context) {
758764
for (Iterator<QueuedContextStruct> iter = this.queuedContexts.listIterator(); iter.hasNext(); ) {
759765
if (iter.next().getContext() == context) {
760766
iter.remove();
767+
if (LOGGER.isLoggable(Level.FINE)) {
768+
LOGGER.log(Level.FINE, "Queued Contexts " + this.queuedContexts);
769+
}
761770
save();
762771
return true;
763772
}
@@ -770,6 +779,14 @@ public static LockableResourcesManager get() {
770779
.getDescriptorOrDie(LockableResourcesManager.class);
771780
}
772781

782+
public synchronized int getQueuedContextsSize() {
783+
return this.queuedContexts.size();
784+
}
785+
786+
public synchronized ArrayList<QueuedContextStruct> getQueuedContexts() {
787+
return new ArrayList<>(this.queuedContexts);
788+
}
789+
773790
public synchronized void save() {
774791
if(BulkChange.contains(this))
775792
return;

src/main/java/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.jenkins.plugins.lockableresources.LockableResource;
2929
import org.jenkins.plugins.lockableresources.LockableResourcesManager;
3030
import org.jenkins.plugins.lockableresources.Messages;
31+
import org.jenkins.plugins.lockableresources.queue.QueuedContextStruct;
3132
import org.kohsuke.stapler.StaplerRequest;
3233
import org.kohsuke.stapler.StaplerResponse;
3334

@@ -88,6 +89,14 @@ public int getNumberOfAllLabels() {
8889
return LockableResourcesManager.get().getAllLabels().size();
8990
}
9091

92+
public ArrayList<QueuedContextStruct> getQueuedContexts() {
93+
return LockableResourcesManager.get().getQueuedContexts();
94+
}
95+
96+
public int getNumberQueuedContexts() {
97+
return LockableResourcesManager.get().getQueuedContextsSize();
98+
}
99+
91100
public void doUnlock(StaplerRequest req, StaplerResponse rsp)
92101
throws IOException, ServletException {
93102
Jenkins.getInstance().checkPermission(UNLOCK);

src/main/java/org/jenkins/plugins/lockableresources/queue/QueuedContextStruct.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
import java.io.Serializable;
1212
import java.util.List;
1313

14+
import hudson.model.Run;
1415
import org.jenkinsci.plugins.workflow.steps.StepContext;
15-
import org.jenkins.plugins.lockableresources.queue.LockableResourcesStruct;
1616

1717
import edu.umd.cs.findbugs.annotations.Nullable;
18+
import org.kohsuke.stapler.export.Exported;
1819

1920
/*
2021
* This class is used to queue pipeline contexts
@@ -38,6 +39,10 @@ public class QueuedContextStruct implements Serializable {
3839
*/
3940
private String resourceDescription;
4041

42+
// build information in the requesting context. Useful for displaying on the ui and logging
43+
private transient volatile Run<?, ?> build = null;
44+
private transient volatile String buildExternalizableId = null;
45+
4146
/*
4247
* Constructor for the QueuedContextStruct class.
4348
*/
@@ -54,6 +59,34 @@ public StepContext getContext() {
5459
return this.context;
5560
}
5661

62+
@Exported
63+
public String getBuildExternalizableId() {
64+
if (this.buildExternalizableId == null) {
65+
// getting the externalizableId can fail for many reasons, set to null if it fails for some reason
66+
try {
67+
buildExternalizableId = this.context.get(Run.class).getExternalizableId();
68+
} catch (Exception e) {
69+
buildExternalizableId = null;
70+
}
71+
}
72+
return this.buildExternalizableId;
73+
}
74+
75+
public Run<?, ?> getBuild() {
76+
if (build == null) {
77+
build = Run.fromExternalizableId(getBuildExternalizableId());
78+
}
79+
return build;
80+
}
81+
82+
@Exported
83+
public String getBuildName() {
84+
if (getBuild() != null)
85+
return getBuild().getFullDisplayName();
86+
else
87+
return null;
88+
}
89+
5790
/*
5891
* Gets the required resources.
5992
*/
@@ -64,9 +97,15 @@ public List<LockableResourcesStruct> getResources() {
6497
/*
6598
* Gets the resource description for logging messages.
6699
*/
100+
@Exported
67101
public String getResourceDescription() {
68102
return this.resourceDescription;
69103
}
70104

71105
private static final long serialVersionUID = 1L;
106+
107+
@Override
108+
public String toString() {
109+
return "Build(" + getBuildExternalizableId() + ") Resource(" + resourceDescription + ")";
110+
}
72111
}

src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/index.jelly

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ function reset_resource_${i}() {
131131
</table>
132132
</j:if>
133133

134+
<h3>Resource queue</h3>
135+
<j:choose>
136+
<j:when test="${it.getNumberQueuedContexts() != 0}">
137+
<table class="pane" style="width: 50%;">
138+
<tbody>
139+
<tr>
140+
<td class="pane-header">Resource requested</td>
141+
<td class="pane-header">Requested by</td>
142+
</tr>
143+
<j:forEach var="context" items="${it.getQueuedContexts()}">
144+
<tr>
145+
<td class="pane" >${context.resourceDescription}</td>
146+
<td class="pane" >
147+
<a href="${rootURL}/${context.build.url}">
148+
${context.build.fullDisplayName}
149+
</a>
150+
</td>
151+
</tr>
152+
</j:forEach>
153+
</tbody>
154+
</table>
155+
</j:when>
156+
<j:otherwise><h4>Empty</h4></j:otherwise>
157+
</j:choose>
158+
134159
</l:main-panel>
135160
</l:layout>
136161
</j:jelly>

0 commit comments

Comments
 (0)