Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Taz03 committed May 22, 2024
1 parent fdea411 commit 5fe48f3
Show file tree
Hide file tree
Showing 26 changed files with 1,039 additions and 506 deletions.
28 changes: 15 additions & 13 deletions JShellAPI/src/main/java/org/togetherjava/jshellapi/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

@ConfigurationProperties("jshellapi")
public record Config(
long regularSessionTimeoutSeconds,
long oneTimeSessionTimeoutSeconds,
long evalTimeoutSeconds,
long evalTimeoutValidationLeeway,
int sysOutCharLimit,
long maxAliveSessions,
int dockerMaxRamMegaBytes,
double dockerCPUsUsage,
@Nullable String dockerCPUSetCPUs,
long schedulerSessionKillScanRateSeconds,
long dockerResponseTimeout,
long dockerConnectionTimeout) {
long regularSessionTimeoutSeconds,
long oneTimeSessionTimeoutSeconds,
long evalTimeoutSeconds,
long evalTimeoutValidationLeeway,
int sysOutCharLimit,
long maxAliveSessions,
int dockerMaxRamMegaBytes,
double dockerCPUsUsage,
@Nullable String dockerCPUSetCPUs,
long schedulerSessionKillScanRateSeconds,
long dockerResponseTimeout,
long dockerConnectionTimeout
) {
public Config {
if (regularSessionTimeoutSeconds <= 0)
throw new IllegalArgumentException("Invalid value " + regularSessionTimeoutSeconds);
Expand All @@ -38,7 +39,8 @@ public record Config(
throw new IllegalArgumentException("Invalid value " + dockerCPUSetCPUs);
if (schedulerSessionKillScanRateSeconds <= 0)
throw new IllegalArgumentException(
"Invalid value " + schedulerSessionKillScanRateSeconds);
"Invalid value " + schedulerSessionKillScanRateSeconds
);
if (dockerResponseTimeout <= 0)
throw new IllegalArgumentException("Invalid value " + dockerResponseTimeout);
if (dockerConnectionTimeout <= 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package org.togetherjava.jshellapi.dto;

public record JShellEvalAbortion(
String sourceCause, String remainingSource, JShellEvalAbortionCause cause) {}
String sourceCause,
String remainingSource,
JShellEvalAbortionCause cause
) {}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.togetherjava.jshellapi.dto;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;

import java.util.List;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
public sealed interface JShellEvalAbortionCause {

@JsonTypeName("TIMEOUT")
record TimeoutAbortionCause() implements JShellEvalAbortionCause {}

@JsonTypeName("UNCAUGHT_EXCEPTION")
record UnhandledExceptionAbortionCause(String exceptionClass, String exceptionMessage)
implements JShellEvalAbortionCause {}
record UnhandledExceptionAbortionCause(
String exceptionClass,
String exceptionMessage
) implements JShellEvalAbortionCause {}

@JsonTypeName("COMPILE_TIME_ERROR")
record CompileTimeErrorAbortionCause(List<String> errors) implements JShellEvalAbortionCause {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.togetherjava.jshellapi.dto;

import org.springframework.lang.Nullable;

import java.util.List;

import org.springframework.lang.Nullable;

public record JShellResult(
List<JShellSnippetResult> snippetsResults,
@Nullable JShellEvalAbortion abortion,
boolean stdoutOverflow,
String stdout) {
List<JShellSnippetResult> snippetsResults,
@Nullable JShellEvalAbortion abortion,
boolean stdoutOverflow,
String stdout
) {
public JShellResult {
snippetsResults = List.copyOf(snippetsResults);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
import org.springframework.lang.Nullable;

public record JShellSnippetResult(
SnippetStatus status, SnippetType type, int id, String source, @Nullable String result) {}
SnippetStatus status,
SnippetType type,
int id,
String source,
@Nullable String result
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public DockerException(Throwable cause) {
}

public DockerException(
String message,
Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
String message,
Throwable cause,
boolean enableSuppression,
boolean writableStackTrace
) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.togetherjava.jshellapi.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
Expand All @@ -12,8 +14,6 @@
import org.togetherjava.jshellapi.service.StartupScriptId;
import org.togetherjava.jshellapi.service.StartupScriptsService;

import java.util.List;

@RequestMapping("jshell")
@RestController
public class JShellController {
Expand All @@ -22,63 +22,72 @@ public class JShellController {

@PostMapping("/eval/{id}")
public JShellResult eval(
@PathVariable String id,
@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code)
throws DockerException {
@PathVariable String id,
@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code
) throws DockerException {
validateId(id);
return service.session(id, startupScriptId)
.eval(code)
.orElseThrow(
() ->
new ResponseStatusException(
HttpStatus.CONFLICT, "An operation is already running"));
.eval(code)
.orElseThrow(
() -> new ResponseStatusException(
HttpStatus.CONFLICT,
"An operation is already running"
)
);
}

@PostMapping("/eval")
public JShellResultWithId eval(
@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code)
throws DockerException {
@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code
) throws DockerException {
JShellService jShellService = service.session(startupScriptId);
return new JShellResultWithId(
jShellService.id(),
jShellService
.eval(code)
.orElseThrow(
() ->
new ResponseStatusException(
HttpStatus.CONFLICT,
"An operation is already running")));
jShellService.id(),
jShellService
.eval(code)
.orElseThrow(
() -> new ResponseStatusException(
HttpStatus.CONFLICT,
"An operation is already running"
)
)
);
}

@PostMapping("/single-eval")
public JShellResult singleEval(
@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code)
throws DockerException {
@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code
) throws DockerException {
JShellService jShellService = service.oneTimeSession(startupScriptId);
return jShellService
.eval(code)
.orElseThrow(
() ->
new ResponseStatusException(
HttpStatus.CONFLICT, "An operation is already running"));
.eval(code)
.orElseThrow(
() -> new ResponseStatusException(
HttpStatus.CONFLICT,
"An operation is already running"
)
);
}

@GetMapping("/snippets/{id}")
public List<String> snippets(
@PathVariable String id, @RequestParam(required = false) boolean includeStartupScript)
throws DockerException {
@PathVariable String id,
@RequestParam(required = false) boolean includeStartupScript
) throws DockerException {
validateId(id);
if (!service.hasSession(id))
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Id " + id + " not found");
return service.session(id, null)
.snippets(includeStartupScript)
.orElseThrow(
() ->
new ResponseStatusException(
HttpStatus.CONFLICT, "An operation is already running"));
.snippets(includeStartupScript)
.orElseThrow(
() -> new ResponseStatusException(
HttpStatus.CONFLICT,
"An operation is already running"
)
);
}

@DeleteMapping("/{id}")
Expand Down Expand Up @@ -107,8 +116,9 @@ public void setStartupScriptsService(StartupScriptsService startupScriptsService
private static void validateId(String id) throws ResponseStatusException {
if (!id.matches("[a-zA-Z0-9][a-zA-Z0-9_.-]+")) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Id " + id + " doesn't match the regex [a-zA-Z0-9][a-zA-Z0-9_.-]+");
HttpStatus.BAD_REQUEST,
"Id " + id + " doesn't match the regex [a-zA-Z0-9][a-zA-Z0-9_.-]+"
);
}
}
}
Loading

0 comments on commit 5fe48f3

Please sign in to comment.