Skip to content

Commit 6148945

Browse files
committed
Make various small improvements
1 parent e244892 commit 6148945

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

src/main/java/org/apposed/appose/Service.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public class Service implements AutoCloseable {
8989
private Thread stderrThread;
9090
private Thread monitorThread;
9191

92-
private Consumer<String> debugListener;
93-
private String initScript;
92+
private @Nullable Consumer<String> debugListener;
93+
private @Nullable String initScript;
9494
private ScriptSyntax syntax;
9595

9696

@@ -514,7 +514,7 @@ private void stdoutLoop() {
514514
line = stdout.readLine();
515515
}
516516
catch (IOException exc) {
517-
// Something went wrong reading the line. Panic!
517+
// Something went wrong reading the stdout line. Panic!
518518
debugService(Messages.stackTrace(exc));
519519
break;
520520
}
@@ -556,7 +556,7 @@ private void stderrLoop() {
556556
line = stderr.readLine();
557557
}
558558
catch (IOException exc) {
559-
// Something went wrong reading the line. Panic!
559+
// Something went wrong reading the stderr line. Panic!
560560
debugService(Messages.stackTrace(exc));
561561
break;
562562
}
@@ -569,12 +569,11 @@ private void stderrLoop() {
569569
}
570570
}
571571

572-
@SuppressWarnings("BusyWait")
573572
private void monitorLoop() {
574573
// Wait until the worker process terminates.
575574
while (process.isAlive() || stdoutThread.isAlive() || stderrThread.isAlive()) {
576575
try {
577-
Thread.sleep(10);
576+
process.waitFor();
578577
}
579578
catch (InterruptedException exc) {
580579
// Treat interruption as a request to shut down.
@@ -588,24 +587,21 @@ private void monitorLoop() {
588587
int exitCode = process.exitValue();
589588
if (exitCode != 0) debugService("<worker process terminated with exit code " + exitCode + ">");
590589
int taskCount = tasks.size();
591-
if (taskCount > 0) {
592-
debugService("<worker process terminated with " +
593-
taskCount + " pending task" + (taskCount == 1 ? "" : "s") + ">");
594-
}
595-
596-
Collection<Task> remainingTasks = tasks.values();
597-
if (!remainingTasks.isEmpty()) {
598-
// Notify any remaining tasks about the process crash.
599-
StringBuilder sb = new StringBuilder();
600-
String nl = System.lineSeparator();
601-
sb.append("Worker crashed with exit code ").append(exitCode).append(".").append(nl);
602-
String stdout = invalidLines.isEmpty() ? "<none>" : String.join(nl, invalidLines);
603-
String stderr = errorLines.isEmpty() ? "<none>" : String.join(nl, errorLines);
604-
sb.append(nl).append("[stdout]").append(nl).append(stdout).append(nl);
605-
sb.append(nl).append("[stderr]").append(nl).append(stderr).append(nl);
606-
String error = sb.toString();
607-
remainingTasks.forEach(task -> task.crash(error));
608-
}
590+
if (taskCount == 0) return; // No hanging tasks to clean up.
591+
592+
debugService("<worker process terminated with " +
593+
taskCount + " pending task" + (taskCount == 1 ? "" : "s") + ">");
594+
595+
// Notify remaining tasks about the process crash.
596+
StringBuilder sb = new StringBuilder();
597+
String nl = System.lineSeparator();
598+
sb.append("Worker crashed with exit code ").append(exitCode).append(".").append(nl);
599+
String stdout = invalidLines.isEmpty() ? "<none>" : String.join(nl, invalidLines);
600+
String stderr = errorLines.isEmpty() ? "<none>" : String.join(nl, errorLines);
601+
sb.append(nl).append("[stdout]").append(nl).append(stdout).append(nl);
602+
sb.append(nl).append("[stderr]").append(nl).append(stderr).append(nl);
603+
String error = sb.toString();
604+
tasks.values().forEach(task -> task.crash(error));
609605
tasks.clear();
610606
}
611607

src/main/java/org/apposed/appose/builder/BaseBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ public T channels(List<String> channels) {
121121
return typedThis();
122122
}
123123

124-
@Override
125-
public T flags(List<String> flags) {
126-
this.flags.addAll(flags);
127-
return typedThis();
128-
}
129-
130124
@Override
131125
public T content(String content) {
132126
this.content = content;
@@ -157,6 +151,12 @@ public T subscribeError(Consumer<String> subscriber) {
157151
return typedThis();
158152
}
159153

154+
@Override
155+
public T flags(List<String> flags) {
156+
this.flags.addAll(flags);
157+
return typedThis();
158+
}
159+
160160
// -- Internal methods --
161161

162162
@SuppressWarnings("unchecked")

src/test/java/org/apposed/appose/DumpApi.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public class DumpApi {
7575
PACKAGE_TO_MODULE.put("org.apposed.appose.Environment", "appose/environment.api");
7676
PACKAGE_TO_MODULE.put("org.apposed.appose.TaskException", "appose/service.api");
7777
PACKAGE_TO_MODULE.put("org.apposed.appose.Service", "appose/service.api");
78-
PACKAGE_TO_MODULE.put("org.apposed.appose.Service.Task", "appose/service.api");
7978
PACKAGE_TO_MODULE.put("org.apposed.appose.Service.TaskStatus", "appose/service.api");
8079
PACKAGE_TO_MODULE.put("org.apposed.appose.Service.RequestType", "appose/service.api");
8180
PACKAGE_TO_MODULE.put("org.apposed.appose.Service.ResponseType", "appose/service.api");
8281
PACKAGE_TO_MODULE.put("org.apposed.appose.TaskEvent", "appose/service.api");
82+
PACKAGE_TO_MODULE.put("org.apposed.appose.Service.Task", "appose/service.api");
8383
PACKAGE_TO_MODULE.put("org.apposed.appose.NDArray", "appose/shm.api");
8484
PACKAGE_TO_MODULE.put("org.apposed.appose.NDArray.DType", "appose/shm.api");
8585
PACKAGE_TO_MODULE.put("org.apposed.appose.NDArray.Shape", "appose/shm.api");
@@ -129,7 +129,8 @@ public class DumpApi {
129129
PACKAGE_TO_MODULE.put("org.apposed.appose.util.XML", "appose/util/xml.api");
130130

131131
// Workers.
132-
PACKAGE_TO_MODULE.put("org.apposed.appose.GroovyWorker", "appose/groovy_worker.api");
132+
PACKAGE_TO_MODULE.put("org.apposed.appose.GroovyWorker", "appose/worker.api");
133+
PACKAGE_TO_MODULE.put("org.apposed.appose.GroovyWorker.Task", "appose/worker.api");
133134

134135
// Test classes - map to tests/*.api files (aligned with Python structure).
135136
PACKAGE_TO_MODULE.put("org.apposed.appose.TestBase", "tests/test_base.api");
@@ -740,11 +741,19 @@ private static String prefix(Object o) {
740741
}
741742

742743
private static String nonClassName(NodeWithSimpleName<?> n) {
743-
return prefix(n) + toSnakeCase(n.getNameAsString());
744+
return prefix(n) + nonClassNameAfterPrefix(n);
744745
}
745746

746747
private static String nonClassName(NodeWithPublicModifier<?> p, NodeWithSimpleName<?> n) {
747-
return prefix(p) + toSnakeCase(n.getNameAsString());
748+
return prefix(p) + nonClassNameAfterPrefix(n);
749+
}
750+
751+
private static String nonClassNameAfterPrefix(NodeWithSimpleName<?> n) {
752+
String nodeName = n.getNameAsString();
753+
final String name;
754+
if ("toString".equals(nodeName)) name = "__str__";
755+
else name = nodeName;
756+
return toSnakeCase(name);
748757
}
749758

750759
/**
@@ -981,7 +990,8 @@ static String pythonType(Type type) {
981990
baseName.equals("TreeMap") || baseName.equals("LinkedHashMap") ||
982991
baseName.equals("Dictionary")) {
983992
if (args.size() == 2) {
984-
return "dict[" + pythonType(args.get(0)) + ", " + pythonType(args.get(1)) + "]";
993+
String typedDict = "dict[" + pythonType(args.get(0)) + ", " + pythonType(args.get(1)) + "]";
994+
return typedDict.equals("dict[str, Any]") ? "Args" : typedDict;
985995
}
986996
return "dict";
987997
}

0 commit comments

Comments
 (0)