diff --git a/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java b/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java index c5abcbb..a2ffb69 100644 --- a/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java +++ b/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java @@ -131,7 +131,13 @@ public void onNext(Frame object) { } public void killContainerByName(String name) { - for (Container container : client.listContainersCmd().withNameFilter(Set.of(name)).exec()) { + LOGGER.debug("Fetching container to kill {}.", name); + List containers = client.listContainersCmd().withNameFilter(Set.of(name)).exec(); + LOGGER.debug("Number of containers to kill: {} for name {}.", containers.size(), name); + if (containers.size() != 1) { + LOGGER.error("There is more than 1 container for name {}.", name); + } + for (Container container : containers) { client.killContainerCmd(container.getId()).exec(); } } diff --git a/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java b/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java index 7d40afa..9ecc0c8 100644 --- a/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java +++ b/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java @@ -206,6 +206,7 @@ public String id() { @Override public void close() { + LOGGER.debug("Close called for session {}.", id); try { dockerService.killContainerByName(containerName()); try { diff --git a/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java b/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java index c6411ef..496dd0e 100644 --- a/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java +++ b/JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java @@ -28,6 +28,7 @@ public class JShellSessionService { private void initScheduler() { scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(() -> { + LOGGER.info("Scheduler heartbeat: started."); jshellSessions.keySet() .stream() .filter(id -> jshellSessions.get(id).isClosed()) @@ -36,6 +37,7 @@ private void initScheduler() { .stream() .filter(id -> jshellSessions.get(id).shouldDie()) .toList(); + LOGGER.info("Scheduler heartbeat: sessions ready to die: {}", toDie); for (String id : toDie) { try { deleteSession(id); @@ -49,10 +51,13 @@ private void initScheduler() { void notifyDeath(String id) { JShellService shellService = jshellSessions.remove(id); - if (shellService == null) + if (shellService == null) { + LOGGER.debug("Notify death on already removed session {}.", id); return; + } if (!shellService.isClosed()) { LOGGER.error("JShell Service isn't dead when it should for id {}.", id); + return; } LOGGER.info("Session {} died.", id); } @@ -82,8 +87,11 @@ public JShellService oneTimeSession(@Nullable StartupScriptId startupScriptId) public void deleteSession(String id) throws DockerException { JShellService service = jshellSessions.remove(id); - service.stop(); - scheduler.schedule(service::close, 500, TimeUnit.MILLISECONDS); + try { + service.stop(); + } finally { + scheduler.schedule(service::close, 500, TimeUnit.MILLISECONDS); + } } private synchronized JShellService createSession(SessionInfo sessionInfo) diff --git a/README.md b/README.md index b2029e4..d5299a6 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,21 @@ JShellAPI is a REST API, and whenever some code is received, it will create a se # How to build JShellAPI in and run it in Docker - Launch Docker - Run `jibDockerBuild` to create the image +- Create a folder outside the project +- `cd` to this folder +- Copy `docker-compose.yaml` inside it - Optionally, create `config/application.yaml` where you can put custom config (see the actual [application.yaml](JShellAPI/src/main/resources/application.yaml)) -- If you don't create it, delete the `command: ["--spring.config.location=file:///home/backend/config/application.yaml"]` line in the `docker-compose.yaml` +* * If you don't create it, delete the `command: ["--spring.config.location=file:///home/backend/config/application.yaml"]` line in the `docker-compose.yaml` - Run `docker compose build` or `docker-compose build` in the folder, depending on your version of Docker. - Run `docker compose start` or `docker-compose start` in the folder, depending on your version of Docker. +- Note that some folders or files may be created automatically inside this folder +- File tree representation: +``` +-folder outside the project + -docker-compose.yaml + -config (optionnal) + -application.yaml +``` ## How to use JShellApi ? See [JShellAPI README](JShellAPI/README.MD)