7
7
import com .github .dockerjava .core .DefaultDockerClientConfig ;
8
8
import com .github .dockerjava .core .DockerClientImpl ;
9
9
import com .github .dockerjava .httpclient5 .ApacheDockerHttpClient ;
10
-
11
10
import org .slf4j .Logger ;
12
11
import org .slf4j .LoggerFactory ;
13
12
import org .springframework .beans .factory .DisposableBean ;
14
13
import org .springframework .lang .Nullable ;
15
14
import org .springframework .stereotype .Service ;
15
+
16
16
import org .togetherjava .jshellapi .Config ;
17
17
18
18
import java .io .*;
@@ -33,20 +33,20 @@ public DockerService(Config config) {
33
33
DefaultDockerClientConfig clientConfig =
34
34
DefaultDockerClientConfig .createDefaultConfigBuilder ().build ();
35
35
ApacheDockerHttpClient httpClient =
36
- new ApacheDockerHttpClient .Builder ()
37
- .dockerHost (clientConfig .getDockerHost ())
38
- .sslConfig (clientConfig .getSSLConfig ())
39
- .responseTimeout (Duration .ofSeconds (config .dockerResponseTimeout ()))
40
- .connectionTimeout (Duration .ofSeconds (config .dockerConnectionTimeout ()))
41
- .build ();
36
+ new ApacheDockerHttpClient .Builder ().dockerHost (clientConfig .getDockerHost ())
37
+ .sslConfig (clientConfig .getSSLConfig ())
38
+ .responseTimeout (Duration .ofSeconds (config .dockerResponseTimeout ()))
39
+ .connectionTimeout (Duration .ofSeconds (config .dockerConnectionTimeout ()))
40
+ .build ();
42
41
this .client = DockerClientImpl .getInstance (clientConfig , httpClient );
43
42
44
43
cleanupLeftovers (WORKER_UNIQUE_ID );
45
44
}
46
45
47
46
private void cleanupLeftovers (UUID currentId ) {
48
- for (Container container :
49
- client .listContainersCmd ().withLabelFilter (Set .of (WORKER_LABEL )).exec ()) {
47
+ for (Container container : client .listContainersCmd ()
48
+ .withLabelFilter (Set .of (WORKER_LABEL ))
49
+ .exec ()) {
50
50
String containerHumanName =
51
51
container .getId () + " " + Arrays .toString (container .getNames ());
52
52
LOGGER .info ("Found worker container '{}'" , containerHumanName );
@@ -57,50 +57,44 @@ private void cleanupLeftovers(UUID currentId) {
57
57
}
58
58
}
59
59
60
- public String spawnContainer (
61
- long maxMemoryMegs ,
62
- long cpus ,
63
- @ Nullable String cpuSetCpus ,
64
- String name ,
65
- Duration evalTimeout ,
66
- long sysoutLimit )
67
- throws InterruptedException {
60
+ public String spawnContainer (long maxMemoryMegs , long cpus , @ Nullable String cpuSetCpus ,
61
+ String name , Duration evalTimeout , long sysoutLimit ) throws InterruptedException {
68
62
String imageName = "togetherjava.org:5001/togetherjava/jshellwrapper" ;
69
- boolean presentLocally =
70
- client .listImagesCmd ().withFilter ("reference" , List .of (imageName )).exec ().stream ()
71
- .flatMap (it -> Arrays .stream (it .getRepoTags ()))
72
- .anyMatch (it -> it .endsWith (":master" ));
63
+ boolean presentLocally = client .listImagesCmd ()
64
+ .withFilter ("reference" , List .of (imageName ))
65
+ .exec ()
66
+ .stream ()
67
+ .flatMap (it -> Arrays .stream (it .getRepoTags ()))
68
+ .anyMatch (it -> it .endsWith (":master" ));
73
69
74
70
if (!presentLocally ) {
75
71
client .pullImageCmd (imageName )
76
- .withTag ("master" )
77
- .exec (new PullImageResultCallback ())
78
- .awaitCompletion (5 , TimeUnit .MINUTES );
72
+ .withTag ("master" )
73
+ .exec (new PullImageResultCallback ())
74
+ .awaitCompletion (5 , TimeUnit .MINUTES );
79
75
}
80
76
81
77
return client .createContainerCmd (imageName + ":master" )
82
- .withHostConfig (
83
- HostConfig .newHostConfig ()
84
- .withAutoRemove (true )
85
- .withInit (true )
86
- .withCapDrop (Capability .ALL )
87
- .withNetworkMode ("none" )
88
- .withPidsLimit (2000L )
89
- .withReadonlyRootfs (true )
90
- .withMemory (maxMemoryMegs * 1024 * 1024 )
91
- .withCpuCount (cpus )
92
- .withCpusetCpus (cpuSetCpus ))
93
- .withStdinOpen (true )
94
- .withAttachStdin (true )
95
- .withAttachStderr (true )
96
- .withAttachStdout (true )
97
- .withEnv (
98
- "evalTimeoutSeconds=" + evalTimeout .toSeconds (),
99
- "sysOutCharLimit=" + sysoutLimit )
100
- .withLabels (Map .of (WORKER_LABEL , WORKER_UNIQUE_ID .toString ()))
101
- .withName (name )
102
- .exec ()
103
- .getId ();
78
+ .withHostConfig (HostConfig .newHostConfig ()
79
+ .withAutoRemove (true )
80
+ .withInit (true )
81
+ .withCapDrop (Capability .ALL )
82
+ .withNetworkMode ("none" )
83
+ .withPidsLimit (2000L )
84
+ .withReadonlyRootfs (true )
85
+ .withMemory (maxMemoryMegs * 1024 * 1024 )
86
+ .withCpuCount (cpus )
87
+ .withCpusetCpus (cpuSetCpus ))
88
+ .withStdinOpen (true )
89
+ .withAttachStdin (true )
90
+ .withAttachStderr (true )
91
+ .withAttachStdout (true )
92
+ .withEnv ("evalTimeoutSeconds=" + evalTimeout .toSeconds (),
93
+ "sysOutCharLimit=" + sysoutLimit )
94
+ .withLabels (Map .of (WORKER_LABEL , WORKER_UNIQUE_ID .toString ()))
95
+ .withName (name )
96
+ .exec ()
97
+ .getId ();
104
98
}
105
99
106
100
public InputStream startAndAttachToContainer (String containerId , InputStream stdin )
@@ -109,31 +103,28 @@ public InputStream startAndAttachToContainer(String containerId, InputStream std
109
103
PipedOutputStream pipeOut = new PipedOutputStream (pipeIn );
110
104
111
105
client .attachContainerCmd (containerId )
112
- .withLogs (true )
113
- .withFollowStream (true )
114
- .withStdOut (true )
115
- .withStdErr (true )
116
- .withStdIn (stdin )
117
- .exec (
118
- new ResultCallback .Adapter <>() {
119
- @ Override
120
- public void onNext (Frame object ) {
121
- try {
122
- String payloadString =
123
- new String (object .getPayload (), StandardCharsets .UTF_8 );
124
- if (object .getStreamType () == StreamType .STDOUT ) {
125
- pipeOut .write (object .getPayload ());
126
- } else {
127
- LOGGER .warn (
128
- "Received STDERR from container {}: {}" ,
129
- containerId ,
130
- payloadString );
131
- }
132
- } catch (IOException e ) {
133
- throw new UncheckedIOException (e );
134
- }
135
- }
136
- });
106
+ .withLogs (true )
107
+ .withFollowStream (true )
108
+ .withStdOut (true )
109
+ .withStdErr (true )
110
+ .withStdIn (stdin )
111
+ .exec (new ResultCallback .Adapter <>() {
112
+ @ Override
113
+ public void onNext (Frame object ) {
114
+ try {
115
+ String payloadString =
116
+ new String (object .getPayload (), StandardCharsets .UTF_8 );
117
+ if (object .getStreamType () == StreamType .STDOUT ) {
118
+ pipeOut .write (object .getPayload ());
119
+ } else {
120
+ LOGGER .warn ("Received STDERR from container {}: {}" , containerId ,
121
+ payloadString );
122
+ }
123
+ } catch (IOException e ) {
124
+ throw new UncheckedIOException (e );
125
+ }
126
+ }
127
+ });
137
128
138
129
client .startContainerCmd (containerId ).exec ();
139
130
return pipeIn ;
0 commit comments