Search in sources :

Example 1 with ContainerNameInfo

use of org.eclipse.che.plugin.docker.machine.DockerContainerNameGenerator.ContainerNameInfo in project che by eclipse.

the class DockerAbandonedResourcesCleaner method cleanContainers.

/**
     * Cleans up CHE docker containers which don't tracked by API any more.
     */
@VisibleForTesting
void cleanContainers() {
    List<String> activeContainers = new ArrayList<>();
    try {
        for (ContainerListEntry container : dockerConnector.listContainers()) {
            String containerName = container.getNames()[0];
            Optional<ContainerNameInfo> optional = nameGenerator.parse(containerName);
            if (optional.isPresent()) {
                try {
                    // container is orphaned if not found exception is thrown
                    environmentEngine.getMachine(optional.get().getWorkspaceId(), optional.get().getMachineId());
                    activeContainers.add(containerName);
                } catch (NotFoundException e) {
                    cleanUpContainer(container);
                } catch (Exception e) {
                    LOG.error(format("Failed to check activity for container with name '%s'. Cause: %s", containerName, e.getLocalizedMessage()), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("Failed to get list docker containers", e);
    } catch (Exception e) {
        LOG.error("Failed to clean up inactive containers", e);
    }
    LOG.info("List containers registered in the api: " + activeContainers);
}
Also used : ContainerNameInfo(org.eclipse.che.plugin.docker.machine.DockerContainerNameGenerator.ContainerNameInfo) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NotFoundException (org.eclipse.che.api.core.NotFoundException)1 ContainerListEntry (org.eclipse.che.plugin.docker.client.json.ContainerListEntry)1 ContainerNameInfo (org.eclipse.che.plugin.docker.machine.DockerContainerNameGenerator.ContainerNameInfo)1