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);
}
}
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);
}
}
}
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);
}
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;
}
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());
}
}
}
}
Aggregations