Skip to content

Commit e706eed

Browse files
authored
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.
1 parent 40349b3 commit e706eed

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,20 @@ abstract class ComposeExecutor implements BuildService<Parameters>, AutoCloseabl
130130
cachedVersion = VersionNumber.parse(rawVersion.startsWith('v') ? rawVersion.substring(1) : rawVersion)
131131
}
132132

133-
Iterable<String> getContainerIds(String serviceName) {
133+
Map<String,Iterable<String>> getContainerIds(List<String> serviceNames) {
134134
// `docker-compose ps -q serviceName` returns an exit code of 1 when the service
135135
// doesn't exist. To guard against this, check the service list first.
136136
def services = execute('ps', '--services').readLines()
137-
if (services.contains(serviceName)) {
138-
return execute('ps', '-q', serviceName).readLines()
137+
def result = [:]
138+
for (String serviceName: serviceNames) {
139+
if (services.contains(serviceName)) {
140+
def containerIds = execute('ps', '-q', serviceName).readLines()
141+
result[serviceName] = containerIds
142+
} else {
143+
result[serviceName] = []
144+
}
139145
}
140-
141-
return []
146+
return result
142147
}
143148

144149
private Set<WeakReference<Thread>> threadsToInterruptOnClose = ConcurrentHashMap.newKeySet()

src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ abstract class ComposeUp extends DefaultTask {
215215

216216
protected Iterable<ServiceInfo> loadServicesInfo(Iterable<String> servicesNames) {
217217
// this code is little bit complicated - the aim is to execute `docker inspect` just once (for all the containers)
218-
Map<String, Iterable<String>> serviceToContainersIds = servicesNames.collectEntries { [(it) : composeExecutor.get().getContainerIds(it)] }
218+
Map<String, Iterable<String>> serviceToContainersIds = composeExecutor.get().getContainerIds(servicesNames)
219219
Map<String, Map<String, Object>> inspections = dockerExecutor.getInspections(*serviceToContainersIds.values().flatten().unique())
220220
serviceToContainersIds.collect { pair -> new ServiceInfo(name: pair.key, containerInfos: pair.value.collect { createContainerInfo(inspections.get(it), pair.key) }.collectEntries { [(it.instanceName): it] } ) }
221221
}

0 commit comments

Comments
 (0)