Skip to content

Commit

Permalink
feat: dc ps --services called only once (#382)
Browse files Browse the repository at this point in the history
Avoid multiple calls dc ps --services calls. The result of the call
is used to check presence of service.
  • Loading branch information
jansyk13 authored Nov 9, 2022
1 parent 40349b3 commit e706eed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,20 @@ abstract class ComposeExecutor implements BuildService<Parameters>, AutoCloseabl
cachedVersion = VersionNumber.parse(rawVersion.startsWith('v') ? rawVersion.substring(1) : rawVersion)
}

Iterable<String> getContainerIds(String serviceName) {
Map<String,Iterable<String>> getContainerIds(List<String> 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<WeakReference<Thread>> threadsToInterruptOnClose = ConcurrentHashMap.newKeySet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ abstract class ComposeUp extends DefaultTask {

protected Iterable<ServiceInfo> loadServicesInfo(Iterable<String> servicesNames) {
// this code is little bit complicated - the aim is to execute `docker inspect` just once (for all the containers)
Map<String, Iterable<String>> serviceToContainersIds = servicesNames.collectEntries { [(it) : composeExecutor.get().getContainerIds(it)] }
Map<String, Iterable<String>> serviceToContainersIds = composeExecutor.get().getContainerIds(servicesNames)
Map<String, Map<String, Object>> 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] } ) }
}
Expand Down

0 comments on commit e706eed

Please sign in to comment.