Skip to content

Commit 8348db8

Browse files
authored
Fix console not able to shutdown on exit and not close errored transaction (#120)
## What is the goal of this PR? Fix two issues in the console. First, we properly shutdown the console when users type `exit` command in both levels of the REPL. Second, when a transaction is errored, we properly return from the transaction REPL as the transaction has been closed and not usable anymore. ## What are the changes implemented in this PR? - Call `executorService.shutdown();` on `exit` command. Fix #118 - Throw exceptions from query jobs to the main thread so that the transaction is properly closed. Fix #119
1 parent 4075db7 commit 8348db8

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

GraknConsole.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,12 @@ private void runRepl(GraknClient client) {
107107
ReplCommand command;
108108
try {
109109
command = ReplCommand.getCommand(reader, printer, "> ");
110-
} catch (InterruptedException e1) {
111-
executorService.shutdown();
112-
try {
113-
if (!executorService.awaitTermination(800, TimeUnit.MILLISECONDS)) {
114-
executorService.shutdownNow();
115-
}
116-
} catch (InterruptedException e2) {
117-
executorService.shutdownNow();
118-
}
110+
} catch (InterruptedException e) {
111+
executorService.shutdownNow();
119112
break;
120113
}
121114
if (command.isExit()) {
115+
executorService.shutdownNow();
122116
break;
123117
} else if (command.isHelp()) {
124118
printer.info(ReplCommand.getHelpMenu());
@@ -171,6 +165,7 @@ private boolean runTransactionRepl(GraknClient client, String database, GraknCli
171165
break;
172166
}
173167
if (command.isExit()) {
168+
executorService.shutdownNow();
174169
return true;
175170
} else if (command.isClear()) {
176171
reader.getTerminal().puts(InfoCmp.Capability.clear_screen);
@@ -214,8 +209,6 @@ private void runQuery(GraknClient.Transaction tx, String queryString) {
214209
printer.error(e.getMessage());
215210
return;
216211
}
217-
218-
219212
for (GraqlQuery query : queries) {
220213
if (query instanceof GraqlDefine) {
221214
tx.query().define(query.asDefine()).get();
@@ -261,13 +254,14 @@ private <T> void printCancellableResult(Stream<T> results, Consumer<T> printFn)
261254
terminal.handle(Terminal.Signal.INT, s -> answerPrintingJob.cancel(true));
262255
answerPrintingJob.get();
263256
Instant end = Instant.now();
264-
printer.info("answers: " + counter + ", duration: " + Duration.between(start, end).toMillis() +" ms");
265-
} catch (InterruptedException | ExecutionException e) {
257+
printer.info("answers: " + counter + ", duration: " + Duration.between(start, end).toMillis() + " ms");
258+
} catch (InterruptedException e) {
266259
e.printStackTrace();
260+
} catch (ExecutionException e) {
261+
throw (RuntimeException)e.getCause();
267262
} catch (CancellationException e) {
268263
Instant end = Instant.now();
269-
printer.info("answers: " + counter + ", duration: " + Duration.between(start, end).toMillis() +" ms");
270-
264+
printer.info("answers: " + counter + ", duration: " + Duration.between(start, end).toMillis() + " ms");
271265
printer.info("The query has been cancelled. It may take some time for the cancellation to finish on the server side.");
272266
} finally {
273267
terminal.handle(Terminal.Signal.INT, Terminal.SignalHandler.SIG_IGN);

0 commit comments

Comments
 (0)