Java Code Examples for com.spotify.docker.client.DockerClient#listTasks()

The following examples show how to use com.spotify.docker.client.DockerClient#listTasks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwarmCluster.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 6 votes vote down vote up
private void fetchTasks(DockerClient dockerClient) throws DockerException, InterruptedException {
    final Map<String, DockerNode> dockerNodeMap = nodes.stream().distinct().collect(toMap(DockerNode::getId, node -> node));
    final List<Task> tasks = dockerClient.listTasks();
    LOG.info("Running tasks " + tasks.size());
    final Map<String, Service> serviceIdToService = serviceIdToServiceMap(dockerClient);

    for (Task task : tasks) {
        final Service service = serviceIdToService.get(task.serviceId());
        if (service == null || !createdByPlugin(service)) {
            continue;
        }

        final DockerTask dockerTask = new DockerTask(task, service);
        final DockerNode dockerNode = dockerNodeMap.get(dockerTask.getNodeId());
        if (dockerNode != null) {
            dockerNode.add(dockerTask);
        }
    }
}
 
Example 2
Source File: DockerServiceElasticAgent.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 5 votes vote down vote up
public static DockerServiceElasticAgent fromService(Service service, DockerClient client) throws DockerException, InterruptedException {
    DockerServiceElasticAgent agent = new DockerServiceElasticAgent();

    agent.id = service.id();
    agent.name = service.spec().name();
    agent.createdAt = service.createdAt();
    agent.jobIdentifier = JobIdentifier.fromJson(service.spec().labels().get(JOB_IDENTIFIER_LABEL_KEY));

    LogStream logStream = client.serviceLogs(service.id(), DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr());
    agent.logs = logStream.readFully();
    logStream.close();

    TaskSpec taskSpec = service.spec().taskTemplate();

    agent.image = taskSpec.containerSpec().image();
    agent.hostname = taskSpec.containerSpec().hostname();
    agent.limits = resourceToString(taskSpec.resources().limits());
    agent.reservations = resourceToString(taskSpec.resources().reservations());
    agent.command = listToString(taskSpec.containerSpec().command());
    agent.args = listToString(taskSpec.containerSpec().args());
    agent.placementConstraints = listToString(taskSpec.placement().constraints());
    agent.environments = toMap(taskSpec);
    agent.hosts = listToString(taskSpec.containerSpec().hosts());

    final List<Task> tasks = client.listTasks(Task.Criteria.builder().serviceName(service.id()).build());
    if (!tasks.isEmpty()) {
        for (Task task : tasks) {
            agent.tasksStatus.add(new TaskStatus(task));
        }
    }

    return agent;
}