Skip to content

Commit 7c741b0

Browse files
committed
Improved logs and fixed potencial bugs
1 parent 71e27d2 commit 7c741b0

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package org.togetherjava.jshellapi;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
57
import org.springframework.boot.context.properties.EnableConfigurationProperties;
68

79
@EnableConfigurationProperties(Config.class)
810
@SpringBootApplication
911
public class Main {
12+
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
13+
1014
public static void main(String[] args) {
1115
SpringApplication.run(Main.class, args);
12-
System.out.println("Running");
16+
LOGGER.info("Running.");
1317
}
1418
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class JShellSessionService {
2727
private void initScheduler() {
2828
scheduler = Executors.newSingleThreadScheduledExecutor();
2929
scheduler.scheduleAtFixedRate(() -> {
30+
jshellSessions.keySet()
31+
.stream()
32+
.filter(id -> jshellSessions.get(id).isClosed())
33+
.forEach(this::notifyDeath);
3034
List<String> toDie = jshellSessions.keySet()
3135
.stream()
3236
.filter(id -> jshellSessions.get(id).shouldDie())
@@ -41,13 +45,12 @@ private void initScheduler() {
4145
}, config.schedulerSessionKillScanRateSeconds(), config.schedulerSessionKillScanRateSeconds(), TimeUnit.SECONDS);
4246
}
4347
void notifyDeath(String id) {
44-
JShellService shellService = jshellSessions.get(id);
48+
JShellService shellService = jshellSessions.remove(id);
4549
if(shellService == null) return;
4650
if(!shellService.isClosed()) {
47-
throw new IllegalStateException("JShell Service isn't dead when it should for id " + id);
51+
LOGGER.error("JShell Service isn't dead when it should for id {}.", id);
4852
}
4953
LOGGER.info("Session {} died.", id);
50-
jshellSessions.remove(id);
5154
}
5255

5356
public boolean hasSession(String id) {
@@ -56,15 +59,15 @@ public boolean hasSession(String id) {
5659

5760
public JShellService session(String id, @Nullable StartupScriptId startupScriptId) throws DockerException {
5861
if(!hasSession(id)) {
59-
return createSession(new SessionInfo(id, true, startupScriptId, config));
62+
return createSession(new SessionInfo(id, true, startupScriptId, false, config));
6063
}
6164
return jshellSessions.get(id);
6265
}
6366
public JShellService session(@Nullable StartupScriptId startupScriptId) throws DockerException {
64-
return createSession(new SessionInfo(UUID.randomUUID().toString(), false, startupScriptId, config));
67+
return createSession(new SessionInfo(UUID.randomUUID().toString(), false, startupScriptId, false, config));
6568
}
6669
public JShellService oneTimeSession(@Nullable StartupScriptId startupScriptId) throws DockerException {
67-
return createSession(new SessionInfo(UUID.randomUUID().toString(), false, startupScriptId, config));
70+
return createSession(new SessionInfo(UUID.randomUUID().toString(), false, startupScriptId, true, config));
6871
}
6972

7073
public void deleteSession(String id) throws DockerException {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.togetherjava.jshellapi.Config;
55

66
public record SessionInfo(String id, long sessionTimeout, boolean renewable, long evalTimeout, long evalTimeoutValidationLeeway, int sysOutCharLimit, @Nullable StartupScriptId startupScriptId) {
7-
public SessionInfo(String id, boolean renewable, StartupScriptId startupScriptId, Config config) {
8-
this(id, config.regularSessionTimeoutSeconds(), renewable, config.evalTimeoutSeconds(), config.evalTimeoutValidationLeeway(), config.sysOutCharLimit(), startupScriptId);
7+
public SessionInfo(String id, boolean renewable, StartupScriptId startupScriptId, boolean isOneTime, Config config) {
8+
this(id, isOneTime ? config.oneTimeSessionTimeoutSeconds() : config.regularSessionTimeoutSeconds(), renewable, config.evalTimeoutSeconds(), config.evalTimeoutValidationLeeway(), config.sysOutCharLimit(), startupScriptId);
99
}
1010
}

0 commit comments

Comments
 (0)