From e706eed6d6b7959050ebb87669c75760c52580d1 Mon Sep 17 00:00:00 2001 From: Jan Sykora Date: Wed, 9 Nov 2022 08:31:34 +0100 Subject: [PATCH] feat: dc ps --services called only once (#382) Avoid multiple calls dc ps --services calls. The result of the call is used to check presence of service. --- .../gradle/dockercompose/ComposeExecutor.groovy | 15 ++++++++++----- .../gradle/dockercompose/tasks/ComposeUp.groovy | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy b/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy index 79ceec8..5fcd552 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy @@ -130,15 +130,20 @@ abstract class ComposeExecutor implements BuildService, AutoCloseabl cachedVersion = VersionNumber.parse(rawVersion.startsWith('v') ? rawVersion.substring(1) : rawVersion) } - Iterable getContainerIds(String serviceName) { + Map> getContainerIds(List serviceNames) { // `docker-compose ps -q serviceName` returns an exit code of 1 when the service // doesn't exist. To guard against this, check the service list first. def services = execute('ps', '--services').readLines() - if (services.contains(serviceName)) { - return execute('ps', '-q', serviceName).readLines() + def result = [:] + for (String serviceName: serviceNames) { + if (services.contains(serviceName)) { + def containerIds = execute('ps', '-q', serviceName).readLines() + result[serviceName] = containerIds + } else { + result[serviceName] = [] + } } - - return [] + return result } private Set> threadsToInterruptOnClose = ConcurrentHashMap.newKeySet() diff --git a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy index aaefedb..c57dfaa 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy @@ -215,7 +215,7 @@ abstract class ComposeUp extends DefaultTask { protected Iterable loadServicesInfo(Iterable servicesNames) { // this code is little bit complicated - the aim is to execute `docker inspect` just once (for all the containers) - Map> serviceToContainersIds = servicesNames.collectEntries { [(it) : composeExecutor.get().getContainerIds(it)] } + Map> serviceToContainersIds = composeExecutor.get().getContainerIds(servicesNames) Map> inspections = dockerExecutor.getInspections(*serviceToContainersIds.values().flatten().unique()) serviceToContainersIds.collect { pair -> new ServiceInfo(name: pair.key, containerInfos: pair.value.collect { createContainerInfo(inspections.get(it), pair.key) }.collectEntries { [(it.instanceName): it] } ) } }