Skip to content

Commit 0ae2010

Browse files
committed
jooby-run: improve restart process (less memory/fast)
- queue the compilation process - fix #3482
1 parent b9cffbc commit 0ae2010

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

modules/jooby-gradle-plugin/src/main/java/io/jooby/gradle/RunTask.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.Optional;
1414
import java.util.Scanner;
1515
import java.util.Set;
16+
import java.util.concurrent.CountDownLatch;
17+
import java.util.concurrent.atomic.AtomicBoolean;
1618
import java.util.function.BiConsumer;
1719
import java.util.stream.Collectors;
1820

@@ -112,20 +114,32 @@ public void run() throws Throwable {
112114

113115
BiConsumer<String, Path> onFileChanged = (event, path) -> {
114116
if (config.isCompileExtension(path)) {
115-
BuildLauncher compiler = connection.newBuild()
116-
.setStandardError(System.err)
117-
.setStandardOutput(System.out)
118-
.forTasks(taskList.toArray(new String[0]));
119-
120-
compiler.run(new ResultHandler<Void>() {
121-
@Override public void onComplete(Void result) {
122-
getLogger().debug("Restarting application on file change: " + path);
123-
joobyRun.restart(path);
124-
}
125-
126-
@Override public void onFailure(GradleConnectionException failure) {
127-
getLogger().debug("Compilation error found: " + path);
128-
}
117+
joobyRun.restart(path, () -> {
118+
CountDownLatch latch = new CountDownLatch(1);
119+
AtomicBoolean success = new AtomicBoolean(false);
120+
BuildLauncher compiler = connection.newBuild()
121+
.setStandardError(System.err)
122+
.setStandardOutput(System.out)
123+
.forTasks(taskList.toArray(new String[0]));
124+
125+
compiler.run(new ResultHandler<Void>() {
126+
@Override public void onComplete(Void result) {
127+
getLogger().debug("Restarting application on file change: " + path);
128+
success.set(true);
129+
latch.countDown();
130+
}
131+
132+
@Override public void onFailure(GradleConnectionException failure) {
133+
getLogger().debug("Compilation error found: " + path);
134+
latch.countDown();
135+
}
136+
});
137+
try {
138+
latch.await();
139+
} catch (InterruptedException e) {
140+
Thread.currentThread().interrupt();
141+
}
142+
return success.get();
129143
});
130144
} else if (config.isRestartExtension(path)) {
131145
getLogger().debug("Restarting application on file change: " + path);

modules/jooby-maven-plugin/src/main/java/io/jooby/maven/RunMojo.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,21 @@ protected void doExecute(List<MavenProject> projects, String mainClass) throws T
8989
BiConsumer<String, Path> onFileChanged =
9090
(event, path) -> {
9191
if (options.isCompileExtension(path)) {
92-
MavenExecutionResult result = maven.execute(mavenRequest("process-classes"));
93-
// Success?
94-
if (result.hasExceptions()) {
95-
getLog().debug("Compilation error found: " + path);
96-
} else {
97-
getLog().debug("Restarting application on file change: " + path);
98-
joobyRun.restart(path);
99-
}
92+
getLog().debug("Restarting application on file change: " + path);
93+
joobyRun.restart(
94+
path,
95+
() -> {
96+
MavenExecutionResult result = maven.execute(mavenRequest("process-classes"));
97+
var error = result.hasExceptions();
98+
// Success?
99+
if (error) {
100+
getLog().debug("Compilation error found: " + path);
101+
}
102+
return !error;
103+
});
100104
} else if (options.isRestartExtension(path)) {
101105
getLog().debug("Restarting application on file change: " + path);
106+
// No compilation required, returns true to proceed with restart
102107
joobyRun.restart(path);
103108
} else {
104109
getLog().debug("Ignoring file change: " + path);

modules/jooby-run/src/main/java/io/jooby/run/JoobyRun.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@
1313
import java.nio.file.Files;
1414
import java.nio.file.Path;
1515
import java.time.Clock;
16-
import java.util.ArrayList;
17-
import java.util.HashMap;
18-
import java.util.LinkedHashSet;
19-
import java.util.List;
20-
import java.util.Map;
21-
import java.util.Set;
16+
import java.util.*;
2217
import java.util.concurrent.ConcurrentLinkedQueue;
2318
import java.util.concurrent.Executors;
2419
import java.util.concurrent.ScheduledExecutorService;
2520
import java.util.concurrent.TimeUnit;
2621
import java.util.concurrent.atomic.AtomicInteger;
2722
import java.util.function.BiConsumer;
23+
import java.util.function.Supplier;
2824
import java.util.stream.Collectors;
2925
import java.util.stream.Stream;
3026

@@ -52,7 +48,7 @@
5248
*/
5349
public class JoobyRun {
5450

55-
private record Event(Path path, long time) {}
51+
private record Event(Path path, long time, Supplier<Boolean> compileTask) {}
5652

5753
private static class AppModule {
5854
private final Logger logger;
@@ -420,7 +416,11 @@ public void start() throws Throwable {
420416

421417
/** Restart the application. */
422418
public void restart(Path path) {
423-
queue.offer(new Event(path, clock.millis()));
419+
restart(path, null);
420+
}
421+
422+
public void restart(Path path, Supplier<Boolean> compileTask) {
423+
queue.offer(new Event(path, clock.millis(), compileTask));
424424
}
425425

426426
private synchronized void actualRestart() {
@@ -433,13 +433,21 @@ private synchronized void actualRestart() {
433433
return; // queue was empty
434434
}
435435
var unload = false;
436+
Supplier<Boolean> compileTask = null;
436437
for (; e != null && (t - e.time) > waitTimeBeforeRestartMillis; e = queue.peek()) {
437438
unload = unload || options.isCompileExtension(e.path);
439+
compileTask = Optional.ofNullable(compileTask).orElse(e.compileTask);
438440
queue.poll();
439441
}
440442
// e will be null if the queue is empty which means all events were old enough
441443
if (e == null) {
442-
module.restart(unload);
444+
var restart = true;
445+
if (compileTask != null) {
446+
restart = compileTask.get();
447+
}
448+
if (restart) {
449+
module.restart(unload);
450+
}
443451
}
444452
}
445453

0 commit comments

Comments
 (0)