|
235 | 235 | (def extract-facts run-function)
|
236 | 236 |
|
237 | 237 | (defn write-stdin [container-id content]
|
238 |
| - (let [buf (ByteBuffer/allocate 1024) |
| 238 | + (let [buf (ByteBuffer/allocate (* 2 (count content))) |
239 | 239 | address (UnixDomainSocketAddress/of "/var/run/docker.sock")
|
240 | 240 | client (SocketChannel/open address)]
|
241 | 241 |
|
|
245 | 245 | (.clear buf)
|
246 | 246 | (.put buf (.getBytes (String. (format "POST /containers/%s/attach?stdin=true&stream=true HTTP/1.1\n" container-id))))
|
247 | 247 | (.put buf (.getBytes (String. "Host: localhost\nConnection: Upgrade\nUpgrade: tcp\n\n")))
|
248 |
| - (.put buf (.getBytes content)) |
249 |
| - (.flip buf) |
250 |
| - |
251 |
| - (while (.hasRemaining buf) |
252 |
| - (.write client buf)) |
| 248 | + (try |
| 249 | + (.put buf (.getBytes content)) |
| 250 | + (.flip buf) |
| 251 | + (while (.hasRemaining buf) |
| 252 | + (.write client buf)) |
| 253 | + (catch Throwable t |
| 254 | + (println "could not write to stdin " t) |
| 255 | + client)) |
253 | 256 | client))
|
254 | 257 |
|
255 | 258 | (defn docker-stream-format->stdout [bytes]
|
|
261 | 264 |
|
262 | 265 | (catch Throwable t
|
263 | 266 | (println t)))
|
264 |
| - (String. (Arrays/copyOfRange bytes 8 (count bytes)))) |
| 267 | + (try |
| 268 | + (String. (Arrays/copyOfRange bytes 8 (count bytes))) |
| 269 | + (catch Throwable t |
| 270 | + (println t) |
| 271 | + ""))) |
265 | 272 |
|
266 | 273 | (defn function-call-with-stdin [m]
|
267 | 274 | (-pull m)
|
|
276 | 283 |
|
277 | 284 | (defn finish-call
|
278 | 285 | "This is a blocking call that waits for the container to finish and then returns the output and exit code."
|
279 |
| - [x] |
| 286 | + [{:keys [timeout] :or {timeout 10000} :as x}] |
| 287 | + ;; close stdin socket |
280 | 288 | (.close (:socket x))
|
281 |
| - (wait x) |
282 |
| - ;; body is raw PTY output |
283 |
| - (let [s (docker-stream-format->stdout |
284 |
| - (:body |
285 |
| - (attach-container-stdout-logs x))) |
286 |
| - info (inspect x)] |
287 |
| - (delete x) |
288 |
| - {:pty-output s |
289 |
| - :exit-code (-> info :State :ExitCode) |
290 |
| - :info info})) |
| 289 | + ;; timeout process |
| 290 | + (let [finished-channel (async/promise-chan)] |
| 291 | + (async/go |
| 292 | + (async/<! (async/timeout timeout)) |
| 293 | + (async/>! finished-channel {:timeout timeout |
| 294 | + :done :timeout |
| 295 | + :kill-container (kill-container x)})) |
| 296 | + ;; watch the container |
| 297 | + (async/go |
| 298 | + (wait x) |
| 299 | + (async/>! finished-channel {:done :exited})) |
| 300 | + ;; body is raw PTY output |
| 301 | + (try |
| 302 | + (let [finish-reason (async/<!! finished-channel) |
| 303 | + s (docker-stream-format->stdout |
| 304 | + (:body |
| 305 | + (attach-container-stdout-logs x))) |
| 306 | + info (inspect x)] |
| 307 | + (delete x) |
| 308 | + (merge |
| 309 | + finish-reason |
| 310 | + {:pty-output s |
| 311 | + :exit-code (-> info :State :ExitCode) |
| 312 | + :info info})) |
| 313 | + (catch Throwable t |
| 314 | + (delete x) |
| 315 | + {})))) |
291 | 316 |
|
292 | 317 | (comment
|
293 | 318 |
|
|
0 commit comments