Search in sources :

Example 61 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class HeliosSoloDeployment method undeployLeftoverJobs.

/**
   * Undeploy jobs left over by {@link TemporaryJobs}. TemporaryJobs should clean these up,
   * but sometimes a few are left behind for whatever reason.
   */
@VisibleForTesting
protected void undeployLeftoverJobs() {
    try {
        // See if there are jobs running on any helios agent. If we are using TemporaryJobs,
        // that class should've undeployed them at this point.
        // Any jobs still running at this point have only been partially cleaned up.
        // We look for jobs via hostStatus() because the job may have been deleted from the master,
        // but the agent may still not have had enough time to undeploy the job from itself.
        final List<String> hosts = heliosClient.listHosts().get();
        for (final String host : hosts) {
            final HostStatus hostStatus = heliosClient.hostStatus(host).get();
            final Map<JobId, TaskStatus> statuses = hostStatus.getStatuses();
            for (final Map.Entry<JobId, TaskStatus> status : statuses.entrySet()) {
                final JobId jobId = status.getKey();
                final Goal goal = status.getValue().getGoal();
                if (goal != Goal.UNDEPLOY) {
                    log.info("Job {} is still set to {} on host {}. Undeploying it now.", jobId, goal, host);
                    final JobUndeployResponse undeployResponse = heliosClient.undeploy(jobId, host).get();
                    log.info("Undeploy response for job {} is {}.", jobId, undeployResponse.getStatus());
                    if (undeployResponse.getStatus() != JobUndeployResponse.Status.OK) {
                        log.warn("Undeploy response for job {} was not OK. This could mean that something " + "beat the helios-solo master in telling the helios-solo agent to " + "undeploy.", jobId);
                    }
                }
                log.info("Waiting for job {} to actually be undeployed...", jobId);
                awaitJobUndeployed(heliosClient, host, jobId, jobUndeployWaitSeconds, TimeUnit.SECONDS);
                log.info("Job {} successfully undeployed.", jobId);
            }
        }
    } catch (Exception e) {
        log.warn("Exception occurred when trying to clean up leftover jobs.", e);
    }
}
Also used : Goal(com.spotify.helios.common.descriptors.Goal) JobUndeployResponse(com.spotify.helios.common.protocol.JobUndeployResponse) HostStatus(com.spotify.helios.common.descriptors.HostStatus) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) JobId(com.spotify.helios.common.descriptors.JobId) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) DockerException(com.spotify.docker.client.exceptions.DockerException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 62 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class HeliosSoloLogService method runOneIteration.

@Override
protected void runOneIteration() throws Exception {
    try {
        // fetch all the jobs running on the solo deployment
        for (final String host : get(heliosClient.listHosts())) {
            final HostStatus hostStatus = get(heliosClient.hostStatus(host));
            if (hostStatus == null) {
                continue;
            }
            final Map<JobId, TaskStatus> statuses = hostStatus.getStatuses();
            for (final TaskStatus status : statuses.values()) {
                final JobId jobId = status.getJob().getId();
                final String containerId = status.getContainerId();
                if (isNullOrEmpty(containerId)) {
                    continue;
                }
                if (!logFutures.containsKey(containerId)) {
                    // for any containers we're not already tracking, attach to their stdout/stderr
                    final Future<?> future = this.executor().submit(new LogFollowJob(containerId, jobId));
                    logFutures.put(containerId, future);
                }
            }
        }
    } catch (Exception e) {
        // Ignore TimeoutException as that is to be expected sometimes
        if (!(Throwables.getRootCause(e) instanceof TimeoutException)) {
            log.warn("Caught exception, will ignore", e);
        }
    }
}
Also used : HostStatus(com.spotify.helios.common.descriptors.HostStatus) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) TimeoutException(java.util.concurrent.TimeoutException) DockerException(com.spotify.docker.client.exceptions.DockerException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConnectionClosedException(org.apache.http.ConnectionClosedException) TimeoutException(java.util.concurrent.TimeoutException)

Example 63 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class WildcardJobCommand method run.

@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException, IOException {
    final String jobIdString = options.getString(jobArg.getDest());
    final Map<JobId, Job> jobs = client.jobs(jobIdString).get();
    if (jobs.size() == 0) {
        if (!json) {
            out.printf("Unknown job: %s%n", jobIdString);
        } else {
            final JobDeployResponse jobDeployResponse = new JobDeployResponse(JobDeployResponse.Status.JOB_NOT_FOUND, null, null);
            out.print(jobDeployResponse.toJsonString());
        }
        return 1;
    } else if (jobs.size() > 1) {
        if (!json) {
            out.printf("Ambiguous job reference: %s%n", jobIdString);
        } else {
            final JobDeployResponse jobDeployResponse = new JobDeployResponse(JobDeployResponse.Status.AMBIGUOUS_JOB_REFERENCE, null, null);
            out.print(jobDeployResponse.toJsonString());
        }
        return 1;
    }
    final JobId jobId = Iterables.getOnlyElement(jobs.keySet());
    return runWithJobId(options, client, out, json, jobId, stdin);
}
Also used : Job(com.spotify.helios.common.descriptors.Job) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) JobId(com.spotify.helios.common.descriptors.JobId)

Example 64 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class JobListCommand method run.

@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException {
    final boolean full = options.getBoolean(fullArg.getDest());
    final boolean quiet = options.getBoolean(quietArg.getDest());
    final String pattern = options.getString(patternArg.getDest());
    final boolean deployed = options.getBoolean(deployedArg.getDest());
    final Map<JobId, Job> jobs;
    if (pattern == null) {
        jobs = client.jobs().get();
    } else {
        jobs = client.jobs(pattern).get();
    }
    if (!Strings.isNullOrEmpty(pattern) && jobs.isEmpty()) {
        if (json) {
            out.println(Json.asPrettyStringUnchecked(jobs));
        } else if (!quiet) {
            out.printf("job pattern %s matched no jobs%n", pattern);
        }
        return 1;
    }
    final Map<JobId, JobStatus> jobStatuses = getJobStatuses(client, jobs, deployed);
    final Set<JobId> sortedJobIds = Sets.newTreeSet(jobStatuses.keySet());
    if (json) {
        if (quiet) {
            out.println(Json.asPrettyStringUnchecked(sortedJobIds));
        } else {
            final Map<JobId, Job> filteredJobs = Maps.newHashMap();
            for (final Entry<JobId, Job> entry : jobs.entrySet()) {
                if (jobStatuses.containsKey(entry.getKey())) {
                    filteredJobs.put(entry.getKey(), entry.getValue());
                }
            }
            out.println(Json.asPrettyStringUnchecked(filteredJobs));
        }
    } else {
        if (quiet) {
            for (final JobId jobId : sortedJobIds) {
                out.println(jobId);
            }
        } else {
            final Table table = table(out);
            table.row("JOB ID", "NAME", "VERSION", "HOSTS", "COMMAND", "ENVIRONMENT");
            for (final JobId jobId : sortedJobIds) {
                final Job job = jobs.get(jobId);
                final String command = on(' ').join(escape(job.getCommand()));
                final String env = Joiner.on(" ").withKeyValueSeparator("=").join(job.getEnv());
                final JobStatus status = jobStatuses.get(jobId);
                table.row(full ? jobId : jobId.toShortString(), jobId.getName(), jobId.getVersion(), status != null ? status.getDeployments().keySet().size() : 0, command, env);
            }
            table.print();
        }
    }
    return 0;
}
Also used : JobStatus(com.spotify.helios.common.descriptors.JobStatus) Table(com.spotify.helios.cli.Table) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId)

Example 65 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class JobWatchCommand method showReport.

private static void showReport(PrintStream out, boolean exact, final List<String> prefixes, final Set<JobId> jobIds, final DateTimeFormatter formatter, final HeliosClient client) throws ExecutionException, InterruptedException {
    final Map<JobId, JobStatus> statuses = client.jobStatuses(jobIds).get();
    for (final JobId jobId : jobIds) {
        final JobStatus jobStatus = statuses.get(jobId);
        if (jobStatus == null) {
            continue;
        }
        final Map<String, TaskStatus> taskStatuses = jobStatus.getTaskStatuses();
        if (exact) {
            for (final String host : prefixes) {
                final TaskStatus ts = taskStatuses.get(host);
                out.printf("%-20s %-30s %-8s %s%n", chop(jobId.toShortString(), 20), chop(host, 30), ts != null ? ts.getState() : "UNKNOWN", ts != null ? ts.getThrottled() : "UNKNOWN");
            }
        } else {
            for (final String host : taskStatuses.keySet()) {
                if (!hostMatches(prefixes, host)) {
                    continue;
                }
                final TaskStatus ts = taskStatuses.get(host);
                out.printf("%-20s %-30s %-8s %s%n", chop(jobId.toShortString(), 20), chop(host, 30), ts.getState(), ts.getThrottled());
            }
        }
    }
}
Also used : JobStatus(com.spotify.helios.common.descriptors.JobStatus) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId)

Aggregations

JobId (com.spotify.helios.common.descriptors.JobId)115 Test (org.junit.Test)68 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)41 Job (com.spotify.helios.common.descriptors.Job)37 HeliosClient (com.spotify.helios.client.HeliosClient)35 Deployment (com.spotify.helios.common.descriptors.Deployment)29 Matchers.containsString (org.hamcrest.Matchers.containsString)25 DockerClient (com.spotify.docker.client.DockerClient)19 JobStatus (com.spotify.helios.common.descriptors.JobStatus)19 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)16 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)13 IOException (java.io.IOException)12 HostStatus (com.spotify.helios.common.descriptors.HostStatus)11 Map (java.util.Map)11 LogStream (com.spotify.docker.client.LogStream)10 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)10 KeeperException (org.apache.zookeeper.KeeperException)9 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)8 AgentMain (com.spotify.helios.agent.AgentMain)7 PortMapping (com.spotify.helios.common.descriptors.PortMapping)7