Skip to content

Commit 584f0f9

Browse files
authored
added custom spotless config (#28)
* update * rewrite if * tj bot spotless config
1 parent fdea411 commit 584f0f9

26 files changed

+711
-503
lines changed

JShellAPI/src/main/java/org/togetherjava/jshellapi/Config.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@
44
import org.springframework.lang.Nullable;
55

66
@ConfigurationProperties("jshellapi")
7-
public record Config(
8-
long regularSessionTimeoutSeconds,
9-
long oneTimeSessionTimeoutSeconds,
10-
long evalTimeoutSeconds,
11-
long evalTimeoutValidationLeeway,
12-
int sysOutCharLimit,
13-
long maxAliveSessions,
14-
int dockerMaxRamMegaBytes,
15-
double dockerCPUsUsage,
16-
@Nullable String dockerCPUSetCPUs,
17-
long schedulerSessionKillScanRateSeconds,
18-
long dockerResponseTimeout,
19-
long dockerConnectionTimeout) {
7+
public record Config(long regularSessionTimeoutSeconds, long oneTimeSessionTimeoutSeconds,
8+
long evalTimeoutSeconds, long evalTimeoutValidationLeeway, int sysOutCharLimit,
9+
long maxAliveSessions, int dockerMaxRamMegaBytes, double dockerCPUsUsage,
10+
@Nullable String dockerCPUSetCPUs, long schedulerSessionKillScanRateSeconds,
11+
long dockerResponseTimeout, long dockerConnectionTimeout) {
2012
public Config {
2113
if (regularSessionTimeoutSeconds <= 0)
2214
throw new IllegalArgumentException("Invalid value " + regularSessionTimeoutSeconds);
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package org.togetherjava.jshellapi.dto;
22

3-
public record JShellEvalAbortion(
4-
String sourceCause, String remainingSource, JShellEvalAbortionCause cause) {}
3+
public record JShellEvalAbortion(String sourceCause, String remainingSource,
4+
JShellEvalAbortionCause cause) {
5+
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/dto/JShellEvalAbortionCause.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
public sealed interface JShellEvalAbortionCause {
1010

1111
@JsonTypeName("TIMEOUT")
12-
record TimeoutAbortionCause() implements JShellEvalAbortionCause {}
12+
record TimeoutAbortionCause() implements JShellEvalAbortionCause {
13+
}
1314

1415
@JsonTypeName("UNCAUGHT_EXCEPTION")
15-
record UnhandledExceptionAbortionCause(String exceptionClass, String exceptionMessage)
16-
implements JShellEvalAbortionCause {}
16+
record UnhandledExceptionAbortionCause(String exceptionClass,
17+
String exceptionMessage) implements JShellEvalAbortionCause {
18+
}
1719

1820
@JsonTypeName("COMPILE_TIME_ERROR")
19-
record CompileTimeErrorAbortionCause(List<String> errors) implements JShellEvalAbortionCause {}
21+
record CompileTimeErrorAbortionCause(List<String> errors) implements JShellEvalAbortionCause {
22+
}
2023

2124
@JsonTypeName("SYNTAX_ERROR")
22-
record SyntaxErrorAbortionCause() implements JShellEvalAbortionCause {}
25+
record SyntaxErrorAbortionCause() implements JShellEvalAbortionCause {
26+
}
2327
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/dto/JShellResult.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
import java.util.List;
66

7-
public record JShellResult(
8-
List<JShellSnippetResult> snippetsResults,
9-
@Nullable JShellEvalAbortion abortion,
10-
boolean stdoutOverflow,
11-
String stdout) {
7+
public record JShellResult(List<JShellSnippetResult> snippetsResults,
8+
@Nullable JShellEvalAbortion abortion, boolean stdoutOverflow, String stdout) {
129
public JShellResult {
1310
snippetsResults = List.copyOf(snippetsResults);
1411
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package org.togetherjava.jshellapi.dto;
22

3-
public record JShellResultWithId(String id, JShellResult result) {}
3+
public record JShellResultWithId(String id, JShellResult result) {
4+
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/dto/JShellSnippetResult.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
import org.springframework.lang.Nullable;
44

5-
public record JShellSnippetResult(
6-
SnippetStatus status, SnippetType type, int id, String source, @Nullable String result) {}
5+
public record JShellSnippetResult(SnippetStatus status, SnippetType type, int id, String source,
6+
@Nullable String result) {
7+
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/exceptions/DockerException.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ public DockerException(Throwable cause) {
1919
super(cause);
2020
}
2121

22-
public DockerException(
23-
String message,
24-
Throwable cause,
25-
boolean enableSuppression,
22+
public DockerException(String message, Throwable cause, boolean enableSuppression,
2623
boolean writableStackTrace) {
2724
super(message, cause, enableSuppression, writableStackTrace);
2825
}

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

+23-42
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.http.HttpStatus;
55
import org.springframework.web.bind.annotation.*;
66
import org.springframework.web.server.ResponseStatusException;
7+
78
import org.togetherjava.jshellapi.dto.JShellResult;
89
import org.togetherjava.jshellapi.dto.JShellResultWithId;
910
import org.togetherjava.jshellapi.exceptions.DockerException;
@@ -21,64 +22,45 @@ public class JShellController {
2122
private StartupScriptsService startupScriptsService;
2223

2324
@PostMapping("/eval/{id}")
24-
public JShellResult eval(
25-
@PathVariable String id,
25+
public JShellResult eval(@PathVariable String id,
2626
@RequestParam(required = false) StartupScriptId startupScriptId,
27-
@RequestBody String code)
28-
throws DockerException {
27+
@RequestBody String code) throws DockerException {
2928
validateId(id);
3029
return service.session(id, startupScriptId)
31-
.eval(code)
32-
.orElseThrow(
33-
() ->
34-
new ResponseStatusException(
35-
HttpStatus.CONFLICT, "An operation is already running"));
30+
.eval(code)
31+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
32+
"An operation is already running"));
3633
}
3734

3835
@PostMapping("/eval")
39-
public JShellResultWithId eval(
40-
@RequestParam(required = false) StartupScriptId startupScriptId,
41-
@RequestBody String code)
42-
throws DockerException {
36+
public JShellResultWithId eval(@RequestParam(required = false) StartupScriptId startupScriptId,
37+
@RequestBody String code) throws DockerException {
4338
JShellService jShellService = service.session(startupScriptId);
44-
return new JShellResultWithId(
45-
jShellService.id(),
46-
jShellService
47-
.eval(code)
48-
.orElseThrow(
49-
() ->
50-
new ResponseStatusException(
51-
HttpStatus.CONFLICT,
52-
"An operation is already running")));
39+
return new JShellResultWithId(jShellService.id(),
40+
jShellService.eval(code)
41+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
42+
"An operation is already running")));
5343
}
5444

5545
@PostMapping("/single-eval")
56-
public JShellResult singleEval(
57-
@RequestParam(required = false) StartupScriptId startupScriptId,
58-
@RequestBody String code)
59-
throws DockerException {
46+
public JShellResult singleEval(@RequestParam(required = false) StartupScriptId startupScriptId,
47+
@RequestBody String code) throws DockerException {
6048
JShellService jShellService = service.oneTimeSession(startupScriptId);
61-
return jShellService
62-
.eval(code)
63-
.orElseThrow(
64-
() ->
65-
new ResponseStatusException(
66-
HttpStatus.CONFLICT, "An operation is already running"));
49+
return jShellService.eval(code)
50+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
51+
"An operation is already running"));
6752
}
6853

6954
@GetMapping("/snippets/{id}")
70-
public List<String> snippets(
71-
@PathVariable String id, @RequestParam(required = false) boolean includeStartupScript)
72-
throws DockerException {
55+
public List<String> snippets(@PathVariable String id,
56+
@RequestParam(required = false) boolean includeStartupScript) throws DockerException {
7357
validateId(id);
7458
if (!service.hasSession(id))
7559
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Id " + id + " not found");
7660
return service.session(id, null)
77-
.snippets(includeStartupScript)
78-
.orElseThrow(
79-
() ->
80-
new ResponseStatusException(
81-
HttpStatus.CONFLICT, "An operation is already running"));
61+
.snippets(includeStartupScript)
62+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
63+
"An operation is already running"));
8264
}
8365

8466
@DeleteMapping("/{id}")
@@ -106,8 +88,7 @@ public void setStartupScriptsService(StartupScriptsService startupScriptsService
10688

10789
private static void validateId(String id) throws ResponseStatusException {
10890
if (!id.matches("[a-zA-Z0-9][a-zA-Z0-9_.-]+")) {
109-
throw new ResponseStatusException(
110-
HttpStatus.BAD_REQUEST,
91+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
11192
"Id " + id + " doesn't match the regex [a-zA-Z0-9][a-zA-Z0-9_.-]+");
11293
}
11394
}

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

+62-71
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import com.github.dockerjava.core.DefaultDockerClientConfig;
88
import com.github.dockerjava.core.DockerClientImpl;
99
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
10-
1110
import org.slf4j.Logger;
1211
import org.slf4j.LoggerFactory;
1312
import org.springframework.beans.factory.DisposableBean;
1413
import org.springframework.lang.Nullable;
1514
import org.springframework.stereotype.Service;
15+
1616
import org.togetherjava.jshellapi.Config;
1717

1818
import java.io.*;
@@ -33,20 +33,20 @@ public DockerService(Config config) {
3333
DefaultDockerClientConfig clientConfig =
3434
DefaultDockerClientConfig.createDefaultConfigBuilder().build();
3535
ApacheDockerHttpClient httpClient =
36-
new ApacheDockerHttpClient.Builder()
37-
.dockerHost(clientConfig.getDockerHost())
38-
.sslConfig(clientConfig.getSSLConfig())
39-
.responseTimeout(Duration.ofSeconds(config.dockerResponseTimeout()))
40-
.connectionTimeout(Duration.ofSeconds(config.dockerConnectionTimeout()))
41-
.build();
36+
new ApacheDockerHttpClient.Builder().dockerHost(clientConfig.getDockerHost())
37+
.sslConfig(clientConfig.getSSLConfig())
38+
.responseTimeout(Duration.ofSeconds(config.dockerResponseTimeout()))
39+
.connectionTimeout(Duration.ofSeconds(config.dockerConnectionTimeout()))
40+
.build();
4241
this.client = DockerClientImpl.getInstance(clientConfig, httpClient);
4342

4443
cleanupLeftovers(WORKER_UNIQUE_ID);
4544
}
4645

4746
private void cleanupLeftovers(UUID currentId) {
48-
for (Container container :
49-
client.listContainersCmd().withLabelFilter(Set.of(WORKER_LABEL)).exec()) {
47+
for (Container container : client.listContainersCmd()
48+
.withLabelFilter(Set.of(WORKER_LABEL))
49+
.exec()) {
5050
String containerHumanName =
5151
container.getId() + " " + Arrays.toString(container.getNames());
5252
LOGGER.info("Found worker container '{}'", containerHumanName);
@@ -57,50 +57,44 @@ private void cleanupLeftovers(UUID currentId) {
5757
}
5858
}
5959

60-
public String spawnContainer(
61-
long maxMemoryMegs,
62-
long cpus,
63-
@Nullable String cpuSetCpus,
64-
String name,
65-
Duration evalTimeout,
66-
long sysoutLimit)
67-
throws InterruptedException {
60+
public String spawnContainer(long maxMemoryMegs, long cpus, @Nullable String cpuSetCpus,
61+
String name, Duration evalTimeout, long sysoutLimit) throws InterruptedException {
6862
String imageName = "togetherjava.org:5001/togetherjava/jshellwrapper";
69-
boolean presentLocally =
70-
client.listImagesCmd().withFilter("reference", List.of(imageName)).exec().stream()
71-
.flatMap(it -> Arrays.stream(it.getRepoTags()))
72-
.anyMatch(it -> it.endsWith(":master"));
63+
boolean presentLocally = client.listImagesCmd()
64+
.withFilter("reference", List.of(imageName))
65+
.exec()
66+
.stream()
67+
.flatMap(it -> Arrays.stream(it.getRepoTags()))
68+
.anyMatch(it -> it.endsWith(":master"));
7369

7470
if (!presentLocally) {
7571
client.pullImageCmd(imageName)
76-
.withTag("master")
77-
.exec(new PullImageResultCallback())
78-
.awaitCompletion(5, TimeUnit.MINUTES);
72+
.withTag("master")
73+
.exec(new PullImageResultCallback())
74+
.awaitCompletion(5, TimeUnit.MINUTES);
7975
}
8076

8177
return client.createContainerCmd(imageName + ":master")
82-
.withHostConfig(
83-
HostConfig.newHostConfig()
84-
.withAutoRemove(true)
85-
.withInit(true)
86-
.withCapDrop(Capability.ALL)
87-
.withNetworkMode("none")
88-
.withPidsLimit(2000L)
89-
.withReadonlyRootfs(true)
90-
.withMemory(maxMemoryMegs * 1024 * 1024)
91-
.withCpuCount(cpus)
92-
.withCpusetCpus(cpuSetCpus))
93-
.withStdinOpen(true)
94-
.withAttachStdin(true)
95-
.withAttachStderr(true)
96-
.withAttachStdout(true)
97-
.withEnv(
98-
"evalTimeoutSeconds=" + evalTimeout.toSeconds(),
99-
"sysOutCharLimit=" + sysoutLimit)
100-
.withLabels(Map.of(WORKER_LABEL, WORKER_UNIQUE_ID.toString()))
101-
.withName(name)
102-
.exec()
103-
.getId();
78+
.withHostConfig(HostConfig.newHostConfig()
79+
.withAutoRemove(true)
80+
.withInit(true)
81+
.withCapDrop(Capability.ALL)
82+
.withNetworkMode("none")
83+
.withPidsLimit(2000L)
84+
.withReadonlyRootfs(true)
85+
.withMemory(maxMemoryMegs * 1024 * 1024)
86+
.withCpuCount(cpus)
87+
.withCpusetCpus(cpuSetCpus))
88+
.withStdinOpen(true)
89+
.withAttachStdin(true)
90+
.withAttachStderr(true)
91+
.withAttachStdout(true)
92+
.withEnv("evalTimeoutSeconds=" + evalTimeout.toSeconds(),
93+
"sysOutCharLimit=" + sysoutLimit)
94+
.withLabels(Map.of(WORKER_LABEL, WORKER_UNIQUE_ID.toString()))
95+
.withName(name)
96+
.exec()
97+
.getId();
10498
}
10599

106100
public InputStream startAndAttachToContainer(String containerId, InputStream stdin)
@@ -109,31 +103,28 @@ public InputStream startAndAttachToContainer(String containerId, InputStream std
109103
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
110104

111105
client.attachContainerCmd(containerId)
112-
.withLogs(true)
113-
.withFollowStream(true)
114-
.withStdOut(true)
115-
.withStdErr(true)
116-
.withStdIn(stdin)
117-
.exec(
118-
new ResultCallback.Adapter<>() {
119-
@Override
120-
public void onNext(Frame object) {
121-
try {
122-
String payloadString =
123-
new String(object.getPayload(), StandardCharsets.UTF_8);
124-
if (object.getStreamType() == StreamType.STDOUT) {
125-
pipeOut.write(object.getPayload());
126-
} else {
127-
LOGGER.warn(
128-
"Received STDERR from container {}: {}",
129-
containerId,
130-
payloadString);
131-
}
132-
} catch (IOException e) {
133-
throw new UncheckedIOException(e);
134-
}
135-
}
136-
});
106+
.withLogs(true)
107+
.withFollowStream(true)
108+
.withStdOut(true)
109+
.withStdErr(true)
110+
.withStdIn(stdin)
111+
.exec(new ResultCallback.Adapter<>() {
112+
@Override
113+
public void onNext(Frame object) {
114+
try {
115+
String payloadString =
116+
new String(object.getPayload(), StandardCharsets.UTF_8);
117+
if (object.getStreamType() == StreamType.STDOUT) {
118+
pipeOut.write(object.getPayload());
119+
} else {
120+
LOGGER.warn("Received STDERR from container {}: {}", containerId,
121+
payloadString);
122+
}
123+
} catch (IOException e) {
124+
throw new UncheckedIOException(e);
125+
}
126+
}
127+
});
137128

138129
client.startContainerCmd(containerId).exec();
139130
return pipeIn;

0 commit comments

Comments
 (0)