Skip to content

Commit 75b0aa3

Browse files
committed
[YAML thing provider] Clean retry queue when a thing is updated/removed
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
1 parent 48e1442 commit 75b0aa3

File tree

1 file changed

+28
-5
lines changed
  • bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things

1 file changed

+28
-5
lines changed

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlThingProvider.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,19 @@ public class YamlThingProvider extends AbstractProvider<Thing>
103103
private final Map<String, Collection<Thing>> thingsMap = new ConcurrentHashMap<>();
104104

105105
private final List<QueueContent> queue = new CopyOnWriteArrayList<>();
106+
private final Object queueLock = new Object();
106107

107108
private final Runnable lazyRetryRunnable = new Runnable() {
108109
@Override
109110
public void run() {
110111
logger.debug("Starting lazy retry thread");
111112
while (!queue.isEmpty()) {
112-
for (QueueContent qc : queue) {
113-
if (retryCreateThing(qc.thingHandlerFactory, qc.thingTypeUID, qc.configuration, qc.thingUID,
114-
qc.bridgeUID)) {
115-
queue.remove(qc);
113+
synchronized (queueLock) {
114+
for (QueueContent qc : queue) {
115+
if (retryCreateThing(qc.thingHandlerFactory, qc.thingTypeUID, qc.configuration, qc.thingUID,
116+
qc.bridgeUID)) {
117+
queue.remove(qc);
118+
}
116119
}
117120
}
118121
if (!queue.isEmpty()) {
@@ -198,6 +201,11 @@ public void addedModel(String modelName, Collection<YamlThingDTO> elements) {
198201
@Override
199202
public void updatedModel(String modelName, Collection<YamlThingDTO> elements) {
200203
boolean isolated = isIsolatedModel(modelName);
204+
if (!isolated) {
205+
elements.stream().map(this::buildThingUID).filter(Objects::nonNull).forEach(uid -> {
206+
removeFromRetryQueue(uid);
207+
});
208+
}
201209
List<Thing> updated = elements.stream().map(t -> mapThing(t, isolated)).filter(Objects::nonNull).toList();
202210
Collection<Thing> modelThings = Objects
203211
.requireNonNull(thingsMap.computeIfAbsent(modelName, k -> new ArrayList<>()));
@@ -224,6 +232,9 @@ public void removedModel(String modelName, Collection<YamlThingDTO> elements) {
224232
boolean isolated = isIsolatedModel(modelName);
225233
Collection<Thing> modelThings = thingsMap.getOrDefault(modelName, List.of());
226234
elements.stream().map(this::buildThingUID).filter(Objects::nonNull).forEach(uid -> {
235+
if (!isolated) {
236+
removeFromRetryQueue(uid);
237+
}
227238
modelThings.stream().filter(th -> th.getUID().equals(uid)).findFirst().ifPresentOrElse(oldThing -> {
228239
modelThings.remove(oldThing);
229240
logger.debug("model {} removed thing {}", modelName, uid);
@@ -544,7 +555,9 @@ private void mergeThing(Thing target, Thing source, boolean keepSourceConfig) {
544555

545556
private void queueRetryThingCreation(ThingHandlerFactory handlerFactory, ThingTypeUID thingTypeUID,
546557
Configuration configuration, ThingUID thingUID, @Nullable ThingUID bridgeUID) {
547-
queue.add(new QueueContent(handlerFactory, thingTypeUID, configuration, thingUID, bridgeUID));
558+
synchronized (queueLock) {
559+
queue.add(new QueueContent(handlerFactory, thingTypeUID, configuration, thingUID, bridgeUID));
560+
}
548561
Thread thread = lazyRetryThread;
549562
if (thread == null || !thread.isAlive()) {
550563
thread = new Thread(lazyRetryRunnable);
@@ -553,6 +566,16 @@ private void queueRetryThingCreation(ThingHandlerFactory handlerFactory, ThingTy
553566
}
554567
}
555568

569+
private void removeFromRetryQueue(ThingUID thingUID) {
570+
synchronized (queueLock) {
571+
for (QueueContent qc : queue) {
572+
if (thingUID.equals(qc.thingUID)) {
573+
queue.remove(qc);
574+
}
575+
}
576+
}
577+
}
578+
556579
private Configuration processThingConfiguration(ThingTypeUID thingTypeUID, ThingUID thingUID,
557580
Configuration configuration) {
558581
Set<String> thingStringParams = !configuration.keySet().isEmpty()

0 commit comments

Comments
 (0)