@@ -31,7 +31,7 @@ public class DockerService implements DisposableBean {
31
31
private final DockerClient client ;
32
32
private final Config config ;
33
33
private final ExecutorService executor = Executors .newSingleThreadExecutor ();
34
- private final ConcurrentHashMap <StartupScriptId , String > cachedContainers =
34
+ private final ConcurrentHashMap <StartupScriptId , ContainerState > cachedContainers =
35
35
new ConcurrentHashMap <>();
36
36
private final StartupScriptsService startupScriptsService ;
37
37
@@ -93,12 +93,10 @@ private boolean isImagePresentLocally() {
93
93
* Pulls the Docker image.
94
94
*/
95
95
private void pullImage () throws InterruptedException {
96
- if (!isImagePresentLocally ()) {
97
- client .pullImageCmd (jshellWrapperBaseImageName )
98
- .withTag ("master" )
99
- .exec (new PullImageResultCallback ())
100
- .awaitCompletion (5 , TimeUnit .MINUTES );
101
- }
96
+ client .pullImageCmd (jshellWrapperBaseImageName )
97
+ .withTag ("master" )
98
+ .exec (new PullImageResultCallback ())
99
+ .awaitCompletion (5 , TimeUnit .MINUTES );
102
100
}
103
101
104
102
/**
@@ -107,7 +105,7 @@ private void pullImage() throws InterruptedException {
107
105
* @param name The name of the container to create.
108
106
* @return The ID of the created container.
109
107
*/
110
- public String createContainer (String name ) {
108
+ private String createContainer (String name ) {
111
109
HostConfig hostConfig = HostConfig .newHostConfig ()
112
110
.withAutoRemove (true )
113
111
.withInit (true )
@@ -146,13 +144,13 @@ public ContainerState initializeContainer(String name, StartupScriptId startupSc
146
144
if (startupScriptId == null || cachedContainers .isEmpty ()
147
145
|| !cachedContainers .containsKey (startupScriptId )) {
148
146
String containerId = createContainer (name );
149
- return setupContainerWithScript (containerId , false , startupScriptId );
147
+ return setupContainerWithScript (containerId , startupScriptId );
150
148
}
151
- String containerId = cachedContainers .get (startupScriptId );
149
+ ContainerState containerState = cachedContainers .get (startupScriptId );
152
150
executor .submit (() -> initializeCachedContainer (startupScriptId ));
153
151
154
- client .renameContainerCmd (containerId ).withName (name ).exec ();
155
- return setupContainerWithScript ( containerId , true , startupScriptId ) ;
152
+ client .renameContainerCmd (containerState . containerId () ).withName (name ).exec ();
153
+ return containerState ;
156
154
}
157
155
158
156
/**
@@ -165,58 +163,44 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
165
163
String id = createContainer (containerName );
166
164
startContainer (id );
167
165
168
- try (PipedInputStream containerInput = new PipedInputStream ();
169
- BufferedWriter writer = new BufferedWriter (
170
- new OutputStreamWriter (new PipedOutputStream (containerInput )))) {
171
- InputStream containerOutput = attachToContainer (id , containerInput , true );
172
-
173
- writer .write (Utils .sanitizeStartupScript (startupScriptsService .get (startupScriptId )));
174
- writer .newLine ();
175
- writer .flush ();
176
- containerOutput .close ();
177
-
178
- cachedContainers .put (startupScriptId , id );
166
+ try {
167
+ ContainerState containerState = setupContainerWithScript (id , startupScriptId );
168
+ cachedContainers .put (startupScriptId , containerState );
179
169
} catch (IOException e ) {
180
170
killContainerByName (containerName );
181
171
throw new RuntimeException (e );
182
172
}
183
173
}
184
174
185
175
/**
186
- *
187
176
* @param containerId The id of the container
188
- * @param isCached Indicator if the container is cached or new
189
177
* @param startupScriptId The startup script id of the session
190
178
* @return ContainerState of the spawned container.
191
179
* @throws IOException if an I/O error occurs
192
180
*/
193
- private ContainerState setupContainerWithScript (String containerId , boolean isCached ,
181
+ private ContainerState setupContainerWithScript (String containerId ,
194
182
StartupScriptId startupScriptId ) throws IOException {
195
- if (!isCached ) {
196
- startContainer (containerId );
197
- }
183
+ startContainer (containerId );
198
184
PipedInputStream containerInput = new PipedInputStream ();
199
185
BufferedWriter writer =
200
186
new BufferedWriter (new OutputStreamWriter (new PipedOutputStream (containerInput )));
201
187
202
- InputStream containerOutput = attachToContainer (containerId , containerInput , false );
188
+ InputStream containerOutput = attachToContainer (containerId , containerInput );
203
189
BufferedReader reader = new BufferedReader (new InputStreamReader (containerOutput ));
204
190
205
- if (!isCached ) {
206
- writer .write (Utils .sanitizeStartupScript (startupScriptsService .get (startupScriptId )));
207
- writer .newLine ();
208
- writer .flush ();
209
- }
191
+ writer .write (Utils .sanitizeStartupScript (startupScriptsService .get (startupScriptId )));
192
+ writer .newLine ();
193
+ writer .flush ();
210
194
211
- return new ContainerState (isCached , containerId , reader , writer );
195
+ return new ContainerState (containerId , reader , writer );
212
196
}
213
197
214
198
/**
215
199
* Creates a new container
216
200
*
217
201
* @param containerId the ID of the container to start
218
202
*/
219
- public void startContainer (String containerId ) {
203
+ private void startContainer (String containerId ) {
220
204
if (!isContainerRunning (containerId )) {
221
205
client .startContainerCmd (containerId ).exec ();
222
206
}
@@ -228,12 +212,11 @@ public void startContainer(String containerId) {
228
212
*
229
213
* @param containerId The ID of the running container to attach to.
230
214
* @param containerInput The input stream (containerInput) to send to the container.
231
- * @param isCached Indicator if the container is cached to prevent writing to output stream.
232
215
* @return InputStream to read the container's stdout
233
216
* @throws IOException if an I/O error occurs
234
217
*/
235
- public InputStream attachToContainer (String containerId , InputStream containerInput ,
236
- boolean isCached ) throws IOException {
218
+ private InputStream attachToContainer (String containerId , InputStream containerInput )
219
+ throws IOException {
237
220
PipedInputStream pipeIn = new PipedInputStream ();
238
221
PipedOutputStream pipeOut = new PipedOutputStream (pipeIn );
239
222
@@ -250,9 +233,7 @@ public void onNext(Frame object) {
250
233
String payloadString =
251
234
new String (object .getPayload (), StandardCharsets .UTF_8 );
252
235
if (object .getStreamType () == StreamType .STDOUT ) {
253
- if (!isCached ) {
254
- pipeOut .write (object .getPayload ()); // Write stdout data to pipeOut
255
- }
236
+ pipeOut .write (object .getPayload ()); // Write stdout data to pipeOut
256
237
} else {
257
238
LOGGER .warn ("Received STDERR from container {}: {}" , containerId ,
258
239
payloadString );
@@ -262,7 +243,6 @@ public void onNext(Frame object) {
262
243
}
263
244
}
264
245
});
265
-
266
246
return pipeIn ;
267
247
}
268
248
0 commit comments