Skip to content

Commit

Permalink
Fix issue with application state notification
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Mar 31, 2020
1 parent cd8c2c8 commit f941994
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.quarkus.builder.BuildContext;
import io.quarkus.builder.BuildStep;
import io.quarkus.deployment.builditem.ApplicationClassPredicateBuildItem;
import io.quarkus.dev.appstate.ApplicationStateNotification;
import io.quarkus.dev.spi.HotReplacementSetup;
import io.quarkus.runner.bootstrap.AugmentActionImpl;
import io.quarkus.runtime.ApplicationLifecycleManager;
Expand Down Expand Up @@ -120,9 +119,6 @@ public void accept(Integer integer) {
public synchronized void restartApp(Set<String> changedResources) {
restarting = true;
stop();

//this clears any old state
ApplicationStateNotification.notifyApplicationStopped();
restarting = false;
Timing.restart(curatedApplication.getAugmentClassLoader());
deploymentProblem = null;
Expand All @@ -133,7 +129,6 @@ public synchronized void restartApp(Set<String> changedResources) {
try {
StartupAction start = augmentAction.reloadExistingApplication(changedResources);
runner = start.runMainClass();
ApplicationStateNotification.waitForApplicationStart();
} catch (Throwable t) {
deploymentProblem = t;
log.error("Failed to start quarkus", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ public StartupActionImpl(CuratedApplication curatedApplication, BuildResult buil
* of the JVM will exit when the app stops.
*/
public RunningQuarkusApplication runMainClass(String... args) throws Exception {
//first

//first we hack around class loading in the fork join pool
ForkJoinClassLoading.setForkJoinClassLoader(runtimeClassLoader);

//this clears any old state, and gets ready to start again
ApplicationStateNotification.reset();
//we have our class loaders
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(runtimeClassLoader);
Expand All @@ -97,7 +100,6 @@ public void run() {
start.invoke(null, (Object) args);
} catch (Throwable e) {
log.error("Error running Quarkus", e);
ApplicationStateNotification.notifyStartupComplete(e.getCause());
}
}
}, "Quarkus Main Thread");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,35 @@
*/
public class ApplicationStateNotification {

private static boolean started = false;
private static State state = State.INITIAL;
private static Throwable startupProblem;

public static synchronized void notifyStartupComplete(Throwable sp) {
started = startupProblem == null;
startupProblem = sp;
public static synchronized void reset() {
if (state == State.STARTED) {
throw new IllegalStateException("Cannot reset a started application");
}
state = State.INITIAL;
startupProblem = null;
}

public static synchronized void notifyStartupComplete() {
state = State.STARTED;
ApplicationStateNotification.class.notifyAll();
}

public static synchronized void notifyApplicationStopped() {
started = false;
startupProblem = null;
state = State.STOPPED;
ApplicationStateNotification.class.notifyAll();
}

public static synchronized void notifyStartupFailed(Throwable t) {
startupProblem = t;
state = State.STOPPED;
ApplicationStateNotification.class.notifyAll();
}

public static synchronized void waitForApplicationStart() {
while (!started && startupProblem == null) {
while (state == State.INITIAL) {
try {
ApplicationStateNotification.class.wait();
} catch (InterruptedException e) {
Expand All @@ -41,12 +53,18 @@ public static synchronized void waitForApplicationStart() {
}

public static synchronized void waitForApplicationStop() {
while (started) {
while (state != State.STOPPED) {
try {
ApplicationStateNotification.class.wait();
} catch (InterruptedException e) {
//ignore
}
}
}

enum State {
INITIAL,
STARTED,
STOPPED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public final void start(String[] args) {
} finally {
stateLock.unlock();
}
ApplicationStateNotification.notifyStartupComplete(t);
ApplicationStateNotification.notifyStartupFailed(t);
throw t;
}
stateLock.lock();
try {
state = ST_STARTED;
stateCond.signalAll();
ApplicationStateNotification.notifyStartupComplete(null);
ApplicationStateNotification.notifyStartupComplete();
} finally {
stateLock.unlock();
}
Expand Down

0 comments on commit f941994

Please sign in to comment.