Skip to content

Commit

Permalink
Merge pull request #35187 from iocanel/init-and-exit-ignored-34643
Browse files Browse the repository at this point in the history
QUARKUS_INIT_AND_EXIT is now properly handled
  • Loading branch information
iocanel authored Aug 7, 2023
2 parents d7b6e30 + 22b36b9 commit 2960dcf
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import io.quarkus.runtime.PreventFurtherStepsException;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import io.smallrye.config.common.utils.StringUtil;

/**
* A {@link Recorder} that is used to check if the application should exit once all initialization tasks are completed.
Expand All @@ -18,13 +20,17 @@
public class InitializationTaskRecorder {

private static final String QUARKUS_INIT_AND_EXIT = "quarkus.init-and-exit";
private final RuntimeValue<InitRuntimeConfig> config;

public InitializationTaskRecorder(RuntimeValue<InitRuntimeConfig> config) {
this.config = config;
}

public void exitIfNeeded() {
//The tcks CustomConverTest is broken: It always returns true for boolean values.
//To workaround this issue, we need to check if `init-and-exit` is explicitly defined.
boolean initAndExitConfigured = StreamSupport.stream(ConfigProvider.getConfig().getPropertyNames().spliterator(), false)
.anyMatch(n -> QUARKUS_INIT_AND_EXIT.equals(n));
if (initAndExitConfigured) {
boolean initAndExitConfigured = propertyConfigured(QUARKUS_INIT_AND_EXIT);
if (initAndExitConfigured && config.getValue().initAndExit) {
if (ConfigProvider.getConfig().getValue(QUARKUS_INIT_AND_EXIT, boolean.class)) {
preventFurtherRecorderSteps(5, "Error attempting to gracefully shutdown after initialization",
() -> new PreventFurtherStepsException("Gracefully exiting after initialization.", 0));
Expand All @@ -49,4 +55,13 @@ public void run() {
}
throw supplier.get();
}

private static String propertyToEnvVar(String property) {
return StringUtil.replaceNonAlphanumericByUnderscores(property).toUpperCase();
}

private static boolean propertyConfigured(String property) {
return StreamSupport.stream(ConfigProvider.getConfig().getPropertyNames().spliterator(), false)
.anyMatch(n -> property.equals(n) || propertyToEnvVar(property).equals(n));
}
}

0 comments on commit 2960dcf

Please sign in to comment.