-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using docker-java client to run docker containers
- Loading branch information
1 parent
f4ad65c
commit 7d2fe29
Showing
5 changed files
with
176 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import com.github.dockerjava.api.async.ResultCallback | ||
import com.github.dockerjava.api.model.Frame | ||
import com.github.dockerjava.api.model.StreamType | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.runInterruptible | ||
import java.io.Closeable | ||
import java.io.OutputStream | ||
|
||
suspend fun <T> List<Deferred<T>>.awaitAny(): T { | ||
val firstCompleted = CompletableDeferred<T>() | ||
var counter = 0 | ||
|
||
forEach { job -> | ||
job.invokeOnCompletion { exception -> | ||
counter++ | ||
|
||
if (exception == null && !firstCompleted.isCompleted) { | ||
firstCompleted.complete(job.getCompleted()) | ||
} else { | ||
if (counter == size) { | ||
firstCompleted.completeExceptionally(IllegalStateException("All executed jobs failed: ", exception)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return firstCompleted.await() | ||
} | ||
|
||
class DefferedResultCallback<T : Any> : ResultCallback<T> { | ||
val job = CompletableDeferred<T>() | ||
private lateinit var item: T | ||
|
||
override fun close() { | ||
|
||
} | ||
|
||
override fun onStart(closeable: Closeable?) { | ||
|
||
} | ||
|
||
override fun onError(throwable: Throwable) { | ||
job.completeExceptionally(throwable) | ||
} | ||
|
||
override fun onComplete() { | ||
job.complete(item) | ||
} | ||
|
||
override fun onNext(item: T) { | ||
this.item = item | ||
} | ||
|
||
} | ||
|
||
suspend fun <T : Any> runAsync(block: (DefferedResultCallback<T>) -> Unit): T { | ||
val callback = DefferedResultCallback<T>() | ||
|
||
runInterruptible { | ||
block(callback) | ||
} | ||
|
||
return callback.job.await() | ||
} | ||
|
||
class StreamLoggerCallback( | ||
private val logStream: OutputStream, | ||
private val errorStream: OutputStream | ||
) : ResultCallback.Adapter<Frame>() { | ||
|
||
override fun onNext(frame: Frame) { | ||
if (frame.streamType == StreamType.STDOUT) { | ||
logStream.write(frame.payload) | ||
} else if (frame.streamType == StreamType.STDERR) { | ||
errorStream.write(frame.payload) | ||
} | ||
} | ||
|
||
override fun close() { | ||
super.close() | ||
logStream.close() | ||
errorStream.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters