Skip to content

Commit

Permalink
Fix issue with lifecycle hooks concurrently modifying the underlying …
Browse files Browse the repository at this point in the history
…resources list.
  • Loading branch information
JonathanGiles committed Jul 12, 2024
1 parent b5b5741 commit 4b87e75
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.*;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -61,7 +62,7 @@ private ObjectMapper prepareObjectMapper(DistributedApplication app) {
}

// run the precommit lifecycle hook on all resources
app.manifest.getResources().values().iterator().forEachRemaining(ResourceWithLifecycle::onResourcePrecommit);
callLifecyclePrecommitHook(app);

LOGGER.info("Validating models...");
// Firstly, disable the info logging messages that are printed by Hibernate Validator
Expand Down Expand Up @@ -134,6 +135,26 @@ private void writeTemplateFile(TemplateFileOutput templateFile) {
}
}

private void callLifecyclePrecommitHook(DistributedApplication app) {
Set<ResourceWithLifecycle> processedResources = new HashSet<>();
Set<ResourceWithLifecycle> currentResources = new HashSet<>(app.manifest.getResources().values());

while (!currentResources.isEmpty()) {
// Create a snapshot of current resources to iterate over
Set<ResourceWithLifecycle> snapshot = new HashSet<>(currentResources);
for (ResourceWithLifecycle resource : snapshot) {
if (!processedResources.contains(resource)) {
resource.onResourcePrecommit();
processedResources.add(resource);
}
currentResources.remove(resource);
}
// Update currentResources to include only new resources added during processing
currentResources.addAll(app.manifest.getResources().values());
currentResources.removeAll(processedResources);
}
}

private void printAnnotations(PrintStream out, DistributedApplication app) {
app.manifest.getResources().values().forEach(resource -> {
out.println("Resource: " + resource.getName());
Expand Down

0 comments on commit 4b87e75

Please sign in to comment.