-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40754 from aloubyansky/incubating-model-resolutio…
…n-task-runner Incubating model resolver: wrap use of Phaser API in a simple task runner API
- Loading branch information
Showing
3 changed files
with
138 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/ModelResolutionTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.quarkus.bootstrap.resolver.maven; | ||
|
||
/** | ||
* Task related to resolution of an {@link io.quarkus.bootstrap.model.ApplicationModel} | ||
*/ | ||
public interface ModelResolutionTask { | ||
void run() throws Exception; | ||
} |
70 changes: 70 additions & 0 deletions
70
...resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/ModelResolutionTaskRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.quarkus.bootstrap.resolver.maven; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
import java.util.concurrent.Phaser; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
class ModelResolutionTaskRunner { | ||
|
||
private static final Logger log = Logger.getLogger(ModelResolutionTaskRunner.class); | ||
|
||
private final Phaser phaser = new Phaser(1); | ||
|
||
/** | ||
* Errors caught while running tasks | ||
*/ | ||
private final Collection<Exception> errors = new ConcurrentLinkedDeque<>(); | ||
|
||
/** | ||
* Runs a model resolution task asynchronously. This method may return before the task has completed. | ||
* | ||
* @param task task to run | ||
*/ | ||
void run(ModelResolutionTask task) { | ||
phaser.register(); | ||
CompletableFuture.runAsync(() -> { | ||
try { | ||
task.run(); | ||
} catch (Exception e) { | ||
errors.add(e); | ||
} finally { | ||
phaser.arriveAndDeregister(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Blocks until all the tasks have completed. | ||
* <p> | ||
* In case some tasks failed with errors, this method will log each error and throw a {@link RuntimeException} | ||
* with a corresponding message. | ||
*/ | ||
void waitForCompletion() { | ||
phaser.arriveAndAwaitAdvance(); | ||
assertNoErrors(); | ||
} | ||
|
||
private void assertNoErrors() { | ||
if (!errors.isEmpty()) { | ||
var sb = new StringBuilder( | ||
"The following errors were encountered while processing Quarkus application dependencies:"); | ||
log.error(sb); | ||
var i = 1; | ||
for (var error : errors) { | ||
var prefix = i++ + ")"; | ||
log.error(prefix, error); | ||
sb.append(System.lineSeparator()).append(prefix).append(" ").append(error.getLocalizedMessage()); | ||
for (var e : error.getStackTrace()) { | ||
sb.append(System.lineSeparator()).append(e); | ||
if (e.getClassName().contains("io.quarkus")) { | ||
break; | ||
} | ||
} | ||
} | ||
throw new RuntimeException(sb.toString()); | ||
} | ||
} | ||
} |