Skip to content

Commit

Permalink
Merge pull request #33 from nqminhuit/fix-not-close-vertx
Browse files Browse the repository at this point in the history
Fix not close vertx
  • Loading branch information
nqminhuit authored Apr 10, 2022
2 parents d4ed0d9 + eaa30d1 commit 2937388
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/nqm/command/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.nqm.config.GisConfig.CURRENT_DIR;
import static org.nqm.utils.StdOutUtils.errln;
import static org.nqm.utils.StdOutUtils.warnln;
import java.io.File;
import java.nio.file.Path;
import java.util.function.Consumer;
Expand All @@ -24,6 +25,10 @@ public static void deployVertx(Path path, String... args) {
GisVertx.instance().deployVerticle(new CommandVerticle(path, args));
}

private static void deployEmptyVertx() {
GisVertx.instance().deployVerticle(new CommandVerticle());
}

private static void forEachModuleWith(Predicate<Path> pred, Consumer<Path> consumeDir, boolean withRoot) {
var gitModulesFilePath = Stream.of(Path.of(".", ".gis-modules"), Path.of(".", ".gitmodules"))
.map(Path::toFile)
Expand All @@ -47,7 +52,7 @@ private static void forEachModuleWith(Predicate<Path> pred, Consumer<Path> consu
.forEach(dir -> shouldConsumeDirOrNot(pred, consumeDir, dir));
}
else {
errln("failed to read file '.gitmodules'");
errln("failed to read file '.gis-modules' or '.gitmodules'");
}
});
}
Expand All @@ -61,7 +66,8 @@ private static void shouldConsumeDirOrNot(Predicate<Path> pred, Consumer<Path> c
consumeDir.accept(path);
}
else {
GisLog.debug("directory '%s' does not satisfy the predicate".formatted("" + path));
warnln("module '%s' does not satisfy the predicate".formatted("" + path.getFileName()));
deployEmptyVertx();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/nqm/config/GisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private static VertxOptions vertxOptions() {
options.setEventLoopPoolSize(1);
options.setWorkerPoolSize(1);
options.setInternalBlockingPoolSize(1);
options.setBlockedThreadCheckInterval(10000);
options.setBlockedThreadCheckInterval(1000000);
return options;
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/nqm/utils/StdOutUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ private StdOutUtils() {}
public static final String CL_CYAN = "\u001B[36m";
public static final String CL_WHITE = "\u001B[37m";

public static final String FONT_BOLD = "\u001B[1m";

public static void errln(String msg) {
err.println(" " + CL_RED + "ERROR: " + msg + CL_RESET);
}
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/nqm/vertx/CommandVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,22 @@ public CommandVerticle(Path path, String... args) {
for (int i = 0; i < args.length; i++) {
this.commandWithArgs[i + 1] = args[i];
}
GisLog.debug("executing command '%s' under directory '%s'", commandWithArgs, path);
GisLog.debug("executing command '%s' under module '%s'", commandWithArgs, path.getFileName());
GisVertx.eventAddDir(path);
}

public CommandVerticle() {
this.path = null;
this.commandWithArgs = null;
GisVertx.eventAddDir(Path.of("."));
}

@Override
public void start() {
if (path == null) {
GisVertx.eventRemoveDir(Path.of("."));
return;
}
vertx.executeBlocking(
(Promise<Process> promise) -> {
try {
Expand All @@ -64,12 +74,7 @@ private void safelyPrint(Process pr) {
var sb = new StringBuilder(infof("%s", "" + path.getFileName()));
try {
while (isNotBlank(line = input.readLine())) {
if (commandWithArgs[1].equals("status")) {
sb.append(gitStatus(line));
}
else {
sb.append("\n ").append(line);
}
sb.append(commandWithArgs[1].equals("status") ? gitStatus(line) : "%n %s".formatted(line));
}
out.println(sb.toString());
Optional.of(pr.waitFor())
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/org/nqm/vertx/GisVertx.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static org.nqm.config.GisConfig.VERTX_OPTIONS;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.nqm.config.GisLog;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
Expand All @@ -13,7 +13,7 @@ public class GisVertx {

private static Vertx vertxInstance;
private static EventBus eventBus;
private static List<String> processedModules;
private static Map<String, Boolean> processedModules;

public static final String ADDR_ADD_DIR = "add.dir";
public static final String ADDR_REM_DIR = "remove.dir";
Expand All @@ -27,9 +27,9 @@ public static Vertx instance() {
return vertxInstance;
}

private static List<String> processedModules() {
private static Map<String, Boolean> processedModules() {
if (processedModules == null) {
processedModules = new ArrayList<>();
processedModules = new HashMap<>();
}
return processedModules;
}
Expand All @@ -39,31 +39,31 @@ public static EventBus eventBus() {
eventBus = instance().eventBus();
}

eventBus.consumer(ADDR_ADD_DIR).handler(GisVertx::handleAddProcessedDir);
eventBus.consumer(ADDR_REM_DIR).handler(GisVertx::handleRemoveProcessedDir);
eventBus.consumer(ADDR_ADD_DIR).handler(GisVertx::addNewModule);
eventBus.consumer(ADDR_REM_DIR).handler(GisVertx::processedModule);

return eventBus;
}

private static void handleAddProcessedDir(Message<Object> msg) {
processedModules().add("" + msg.body());
private static void addNewModule(Message<Object> msg) {
processedModules().put("" + msg.body(), false);
}

private static void handleRemoveProcessedDir(Message<Object> msg) {
processedModules().removeIf(d -> d.equals("" + msg.body()));
GisLog.debug("### queue size = '%s'".formatted(processedModules().size()));
if (processedModules().isEmpty()) {
private static void processedModule(Message<Object> msg) {
var modules = processedModules();
modules.computeIfPresent("" + msg.body(), (k, v) -> true);
if (!modules.isEmpty() && modules.values().stream().allMatch(Boolean.TRUE::equals)) {
System.exit(0);
}
}

public static void eventAddDir(Path dir) {
GisLog.debug("+++ adding dir '%s' to queue".formatted("" + dir));
GisLog.debug("+++ adding module '%s' to queue".formatted("" + dir.getFileName()));
eventBus().send(GisVertx.ADDR_ADD_DIR, "" + dir);
}

public static void eventRemoveDir(Path dir) {
GisLog.debug("--- removing dir '%s' from queue".formatted("" + dir));
GisLog.debug("--- removing module '%s' from queue".formatted("" + dir.getFileName()));
eventBus().send(GisVertx.ADDR_REM_DIR, "" + dir);
}

Expand Down

0 comments on commit 2937388

Please sign in to comment.