Skip to content

Commit 0fb6b55

Browse files
committed
Add StreamCallback for execStart()
1 parent 00af4e4 commit 0fb6b55

File tree

1 file changed

+20
-10
lines changed
  • api-client/src/main/kotlin/de/gesellix/docker/remote/api/client

1 file changed

+20
-10
lines changed

api-client/src/main/kotlin/de/gesellix/docker/remote/api/client/ExecApi.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import de.gesellix.docker.remote.api.core.RequestConfig
2828
import de.gesellix.docker.remote.api.core.ResponseType
2929
import de.gesellix.docker.remote.api.core.ServerError
3030
import de.gesellix.docker.remote.api.core.ServerException
31+
import de.gesellix.docker.remote.api.core.StreamCallback
3132
import de.gesellix.docker.remote.api.core.Success
3233
import de.gesellix.docker.remote.api.core.SuccessStream
3334
import kotlinx.coroutines.cancel
@@ -234,28 +235,37 @@ class ExecApi(dockerClientConfig: DockerClientConfig = defaultClientConfig, prox
234235
* @throws ServerException If the API returns a server error response
235236
*/
236237
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
237-
fun execStart(id: String, execStartConfig: ExecStartConfig?) {
238+
@JvmOverloads
239+
fun execStart(
240+
id: String, execStartConfig: ExecStartConfig?,
241+
callback: StreamCallback<Frame?>? = null, timeoutMillis: Long? = null /*= 24.hours.toLongMilliseconds()*/
242+
) {
238243
val localVariableConfig = execStartRequestConfig(id = id, execStartConfig = execStartConfig)
239244

240-
// TODO do we need to inspect the exec, because it might have been created with tty==false?
241-
// val expectMultiplexedResponse = !(execInspect(id).processConfig?.tty ?: false)
242-
val expectMultiplexedResponse = !(execStartConfig?.tty ?: false)
245+
val expectMultiplexedResponse: Boolean = if (execStartConfig?.tty != null) {
246+
!(execStartConfig.tty ?: false)
247+
} else {
248+
!(execInspect(id).processConfig?.tty ?: false)
249+
}
243250
val localVarResponse = requestFrames(
244251
localVariableConfig, expectMultiplexedResponse
245252
)
246253

247-
// TODO the caller of #execStart() should decide about timeout and callback
248-
val timeout = Duration.of(1, ChronoUnit.HOURS)
249-
val callback = LoggingCallback<Frame>()
254+
val timeout = if (timeoutMillis == null) {
255+
Duration.of(10, ChronoUnit.MINUTES)
256+
} else {
257+
Duration.of(timeoutMillis, ChronoUnit.MILLIS)
258+
}
259+
val actualCallback = callback ?: LoggingCallback<Frame?>()
250260

251261
when (localVarResponse.responseType) {
252262
ResponseType.Success -> {
253263
runBlocking {
254264
launch {
255265
withTimeoutOrNull(timeout.toMillis()) {
256-
callback.onStarting(this@launch::cancel)
257-
((localVarResponse as SuccessStream<*>).data as Flow<Frame>).collect { callback.onNext(it) }
258-
callback.onFinished()
266+
actualCallback.onStarting(this@launch::cancel)
267+
((localVarResponse as SuccessStream<*>).data as Flow<Frame>).collect { actualCallback.onNext(it) }
268+
actualCallback.onFinished()
259269
}
260270
}
261271
}

0 commit comments

Comments
 (0)