Skip to content

Commit 1713e0a

Browse files
committed
Fixing bug snippet endpoint with startup script + improving logs
1 parent 6ea2570 commit 1713e0a

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

JShellAPI/src/main/java/org/togetherjava/jshellapi/rest/JShellController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public JShellResult singleEval(@RequestParam(required = false) StartupScriptId s
4040
public List<String> snippets(@PathVariable String id, @RequestParam(required = false) boolean includeStartupScript) throws DockerException {
4141
validateId(id);
4242
if(!service.hasSession(id)) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Id " + id + " not found");
43-
return service.session(id, null).snippets().map(l -> includeStartupScript || l.size() < 2 ? l : l.subList(2, l.size())).orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running"));
43+
return service.session(id, null).snippets(includeStartupScript).orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running"));
4444
}
4545

4646
@DeleteMapping("/{id}")

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class JShellService implements Closeable {
2727
private final long evalTimeout;
2828
private boolean doingOperation;
2929
private final DockerService dockerService;
30+
private final int startupScriptSize;
3031

3132
public JShellService(DockerService dockerService, JShellSessionService sessionService, String id, long timeout, boolean renewable, long evalTimeout, long evalTimeoutValidationLeeway, int sysOutCharLimit, int maxMemory, double cpus, String startupScript) throws DockerException {
3233
this.dockerService = dockerService;
@@ -38,7 +39,7 @@ public JShellService(DockerService dockerService, JShellSessionService sessionSe
3839
this.evalTimeoutValidationLeeway = evalTimeoutValidationLeeway;
3940
this.lastTimeoutUpdate = Instant.now();
4041
if(!dockerService.isDead(containerName())) {
41-
LOGGER.error("Tried to create an existing container {}.", containerName());
42+
LOGGER.warn("Tried to create an existing container {}.", containerName());
4243
throw new DockerException("The session isn't completely destroyed, try again later.");
4344
}
4445
try {
@@ -58,9 +59,12 @@ public JShellService(DockerService dockerService, JShellSessionService sessionSe
5859
reader = new BufferedReader(new InputStreamReader(containerOutput));
5960
writer.write(sanitize(startupScript));
6061
writer.newLine();
61-
} catch (IOException | InterruptedException e) {
62+
writer.flush();
63+
checkContainerOK();
64+
startupScriptSize = Integer.parseInt(reader.readLine());
65+
} catch (Exception e) {
6266
LOGGER.warn("Unexpected error during creation.", e);
63-
throw new DockerException(e);
67+
throw new DockerException("Creation of the session failed.", e);
6468
}
6569
this.doingOperation = false;
6670
}
@@ -136,7 +140,7 @@ private JShellResult readResult() throws IOException, NumberFormatException, Doc
136140
return new JShellResult(snippetResults, abortion, stdoutOverflow, stdout);
137141
}
138142

139-
public Optional<List<String>> snippets() throws DockerException {
143+
public Optional<List<String>> snippets(boolean includeStartupScript) throws DockerException {
140144
synchronized (this) {
141145
if(!tryStartOperation()) {
142146
return Optional.empty();
@@ -155,7 +159,9 @@ public Optional<List<String>> snippets() throws DockerException {
155159
while(!(snippet = reader.readLine()).isEmpty()) {
156160
snippets.add(cleanCode(snippet));
157161
}
158-
return Optional.of(snippets);
162+
return Optional.of(
163+
includeStartupScript ? snippets : snippets.subList(startupScriptSize, snippets.size())
164+
);
159165
} catch (IOException ex) {
160166
LOGGER.warn("Unexpected error.", ex);
161167
close();

0 commit comments

Comments
 (0)