Skip to content

Commit

Permalink
Merge pull request #46 from avast/CaptureContainerOutput
Browse files Browse the repository at this point in the history
Optionally capture output of all containers and send it to Gradle output
  • Loading branch information
alenkacz committed Oct 9, 2016
2 parents c82e083 + 974c54f commit 266ed83
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dockerCompose.isRequiredBy(test) // hooks 'dependsOn composeUp' and 'finalizedBy
dockerCompose {
// useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f <file>'
// captureContainersOutput = true // prinnts output of all containers to Gradle output - very useful for debugging
// stopContainers = false // useful for debugging
// removeContainers = false
// removeImages = "None" // Other accepted values are: "All" and "Local"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ComposeExtension {

boolean buildBeforeUp = true
boolean waitForTcpPorts = true
boolean captureContainersOutput = false
Duration waitAfterTcpProbeFailure = Duration.ofSeconds(1)
Duration waitForTcpPortsTimeout = Duration.ofMinutes(15)
Duration waitForTcpPortsDisconnectionProbeTimeout = Duration.ofMillis(1000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.process.ExecSpec
import org.yaml.snakeyaml.Yaml

import java.time.Instant
import java.util.concurrent.Executors

class ComposeUp extends DefaultTask {

Expand Down Expand Up @@ -40,6 +41,9 @@ class ComposeUp extends DefaultTask {
e.commandLine extension.composeCommand('up', '-d')
}
try {
if (extension.captureContainersOutput) {
captureContainersOutput()
}
servicesInfos = loadServicesInfo().collectEntries { [(it.name): (it)] }
waitForHealthyContainers(servicesInfos.values())
if (extension.waitForTcpPorts) {
Expand All @@ -52,6 +56,34 @@ class ComposeUp extends DefaultTask {
}
}

protected void captureContainersOutput() {
def t = Executors.defaultThreadFactory().newThread(new Runnable() {
@Override
void run() {
project.exec { ExecSpec e ->
e.commandLine extension.composeCommand('logs', '-f', '--no-color')
e.standardOutput = new OutputStream() {
def buffer = new ArrayList<Byte>()
@Override
void write(int b) throws IOException {
if (b == 10 || b == 13) {
if (buffer.size() > 0) {
def toPrint = buffer.collect { it as byte }.toArray() as byte[]
logger.lifecycle(new String(toPrint))
buffer.clear()
}
} else {
buffer.add(b as Byte)
}
}
}
}
}
})
t.daemon = true
t.start()
}

protected Iterable<ServiceInfo> loadServicesInfo() {
getServiceNames().collect { createServiceInfo(it) }
}
Expand Down

0 comments on commit 266ed83

Please sign in to comment.