11
11
import org .slf4j .Logger ;
12
12
import org .slf4j .LoggerFactory ;
13
13
import org .springframework .beans .factory .DisposableBean ;
14
+ import org .springframework .lang .Nullable ;
14
15
import org .springframework .stereotype .Service ;
15
16
16
17
import org .togetherjava .jshellapi .Config ;
@@ -58,7 +59,7 @@ public DockerService(Config config, StartupScriptsService startupScriptsService)
58
59
pullImage ();
59
60
}
60
61
cleanupLeftovers (WORKER_UNIQUE_ID );
61
- executor .submit (() -> initializeCachedContainer (StartupScriptId .EMPTY ));
62
+ executor .submit (() -> initializeCachedContainer (StartupScriptId .CUSTOM_DEFAULT ));
62
63
}
63
64
64
65
private void cleanupLeftovers (UUID currentId ) {
@@ -106,6 +107,7 @@ private void pullImage() throws InterruptedException {
106
107
* @return The ID of the created container.
107
108
*/
108
109
private String createContainer (String name ) {
110
+ LOGGER .debug ("Creating container '{}'" , name );
109
111
HostConfig hostConfig = HostConfig .newHostConfig ()
110
112
.withAutoRemove (true )
111
113
.withInit (true )
@@ -137,10 +139,13 @@ private String createContainer(String name) {
137
139
*
138
140
* @param name Name of the container.
139
141
* @param startupScriptId Script to initialize the container with.
142
+ * @throws IOException if an I/O error occurs.
140
143
* @return The ContainerState of the newly created container.
141
144
*/
142
- public ContainerState initializeContainer (String name , StartupScriptId startupScriptId )
143
- throws IOException {
145
+ public ContainerState initializeContainer (String name ,
146
+ @ Nullable StartupScriptId startupScriptId ) throws IOException {
147
+ LOGGER .info ("Initializing container '{}' with Startup script ID: {}" , name ,
148
+ startupScriptId );
144
149
if (startupScriptId == null || cachedContainers .isEmpty ()
145
150
|| !cachedContainers .containsKey (startupScriptId )) {
146
151
String containerId = createContainer (name );
@@ -159,27 +164,34 @@ public ContainerState initializeContainer(String name, StartupScriptId startupSc
159
164
* @param startupScriptId Script to initialize the container with.
160
165
*/
161
166
private void initializeCachedContainer (StartupScriptId startupScriptId ) {
162
- String containerName = cachedContainerName ();
167
+ LOGGER .info ("Initializing cached container with Startup script ID: {}" , startupScriptId );
168
+ String containerName = newCachedContainerName ();
163
169
String id = createContainer (containerName );
164
170
startContainer (id );
165
171
166
172
try {
167
173
ContainerState containerState = setupContainerWithScript (id , startupScriptId );
168
174
cachedContainers .put (startupScriptId , containerState );
169
175
} catch (IOException e ) {
176
+ LOGGER .error ("Could not initialize container {}" , id , e );
170
177
killContainerByName (containerName );
171
178
throw new RuntimeException (e );
172
179
}
173
180
}
174
181
175
182
/**
183
+ * Setup container with startup script and also initializes input and output streams for the
184
+ * container.
185
+ *
176
186
* @param containerId The id of the container
177
187
* @param startupScriptId The startup script id of the session
178
188
* @return ContainerState of the spawned container.
179
189
* @throws IOException if an I/O error occurs
180
190
*/
181
191
private ContainerState setupContainerWithScript (String containerId ,
182
192
StartupScriptId startupScriptId ) throws IOException {
193
+ LOGGER .info ("Setting up container with id {} with Startup script ID: {}" , containerId ,
194
+ startupScriptId );
183
195
startContainer (containerId );
184
196
PipedInputStream containerInput = new PipedInputStream ();
185
197
BufferedWriter writer =
@@ -201,9 +213,13 @@ private ContainerState setupContainerWithScript(String containerId,
201
213
* @param containerId the ID of the container to start
202
214
*/
203
215
private void startContainer (String containerId ) {
204
- if (!isContainerRunning (containerId )) {
205
- client .startContainerCmd (containerId ).exec ();
216
+ boolean isRunning = isContainerRunning (containerId );
217
+ if (isRunning ) {
218
+ LOGGER .debug ("Container {} is already running." , containerId );
219
+ return ;
206
220
}
221
+ LOGGER .debug ("Container {} is not running. Starting it now." , containerId );
222
+ client .startContainerCmd (containerId ).exec ();
207
223
}
208
224
209
225
/**
@@ -233,7 +249,7 @@ public void onNext(Frame object) {
233
249
String payloadString =
234
250
new String (object .getPayload (), StandardCharsets .UTF_8 );
235
251
if (object .getStreamType () == StreamType .STDOUT ) {
236
- pipeOut .write (object .getPayload ()); // Write stdout data to pipeOut
252
+ pipeOut .write (object .getPayload ());
237
253
} else {
238
254
LOGGER .warn ("Received STDERR from container {}: {}" , containerId ,
239
255
payloadString );
@@ -257,7 +273,7 @@ public boolean isContainerRunning(String containerId) {
257
273
return Boolean .TRUE .equals (containerResponse .getState ().getRunning ());
258
274
}
259
275
260
- private String cachedContainerName () {
276
+ private String newCachedContainerName () {
261
277
return "cached_session_" + UUID .randomUUID ();
262
278
}
263
279
0 commit comments