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