use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.
the class JobHistoryCommand method run.
@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException {
final String jobIdString = options.getString(jobIdArg.getDest());
final Map<JobId, Job> jobs = client.jobs(jobIdString).get();
if (jobs.size() == 0) {
out.printf("Unknown job: %s%n", jobIdString);
return 1;
} else if (jobs.size() > 1) {
out.printf("Ambiguous job id: %s%n", jobIdString);
return 1;
}
final JobId jobId = getLast(jobs.keySet());
final TaskStatusEvents result = client.jobHistory(jobId).get();
if (json) {
out.println(Json.asPrettyStringUnchecked(result));
return 0;
}
final Table table = table(out);
table.row("HOST", "TIMESTAMP", "STATE", "THROTTLED", "CONTAINERID");
final List<TaskStatusEvent> events = result.getEvents();
final DateTimeFormatter format = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS");
for (final TaskStatusEvent event : events) {
final String host = checkNotNull(event.getHost());
final long timestamp = checkNotNull(event.getTimestamp());
final TaskStatus status = checkNotNull(event.getStatus());
final State state = checkNotNull(status.getState());
String containerId = status.getContainerId();
containerId = containerId == null ? "<none>" : containerId;
table.row(host, format.print(timestamp), state, status.getThrottled(), containerId);
}
table.print();
return 0;
}
use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.
the class HistoryResource method jobHistory.
/**
* Returns the {@link TaskStatusEvents} for the specified job.
* @param jobId The ID of the job.
* @return The history of the jobs.
* @throws HeliosException If an unexpected error occurs.
*/
@GET
@Produces(APPLICATION_JSON)
@Path("jobs/{id}")
@Timed
@ExceptionMetered
public TaskStatusEvents jobHistory(@PathParam("id") @Valid final JobId jobId) throws HeliosException {
if (!jobId.isFullyQualified()) {
throw badRequest("Invalid id");
}
try {
final List<TaskStatusEvent> events = model.getJobHistory(jobId);
metrics.jobsHistoryEventSize(events.size());
final TaskStatusEvents result = new TaskStatusEvents(events, OK);
return result;
} catch (JobDoesNotExistException e) {
return new TaskStatusEvents(ImmutableList.<TaskStatusEvent>of(), JOB_ID_NOT_FOUND);
}
}
use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.
the class HealthCheckTest method testContainerDiesDuringHealthcheck.
@Test
public void testContainerDiesDuringHealthcheck() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost(), "--service-registry=" + registryAddress);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final HealthCheck healthCheck = TcpHealthCheck.of("health");
final Job job = pokeJob(healthCheck);
final JobId jobId = createJob(job);
deployJob(jobId, testHost());
awaitTaskState(jobId, testHost(), HEALTHCHECKING);
// kill the underlying container
final JobStatus jobStatus = getOrNull(client.jobStatus(jobId));
final TaskStatus taskStatus = jobStatus.getTaskStatuses().get(testHost());
getNewDockerClient().killContainer(taskStatus.getContainerId());
// ensure the job is marked as failed
final int timeout = WAIT_TIMEOUT_SECONDS;
Polling.await(timeout, SECONDS, new Callable<Object>() {
@Override
public Object call() throws Exception {
final TaskStatusEvents jobHistory = getOrNull(client.jobHistory(jobId));
for (final TaskStatusEvent event : jobHistory.getEvents()) {
if (event.getStatus().getState() == FAILED) {
return true;
}
}
return null;
}
});
// wait for the job to come back up and start healthchecking again
awaitTaskState(jobId, testHost(), HEALTHCHECKING);
pokeAndVerifyRegistration(client, jobId, timeout);
}
use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.
the class JobHistoryTest method testJobHistory.
@Test
public void testJobHistory() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost());
awaitHostStatus(testHost(), Status.UP, LONG_WAIT_SECONDS, SECONDS);
final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
deployJob(jobId, testHost());
awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
undeployJob(jobId, testHost());
awaitTaskGone(client, testHost(), jobId, LONG_WAIT_SECONDS, SECONDS);
final TaskStatusEvents events = Polling.await(WAIT_TIMEOUT_SECONDS, SECONDS, new Callable<TaskStatusEvents>() {
@Override
public TaskStatusEvents call() throws Exception {
final TaskStatusEvents events = client.jobHistory(jobId).get();
final int size = events.getEvents().size();
if (size == 0) {
return null;
}
// We sometimes get more than one PULLING_IMAGE in the history if a pull tempfails.
int requiredEventCount = -1;
for (int i = 0; i < size; i++) {
if (events.getEvents().get(i).getStatus().getState() != State.PULLING_IMAGE) {
requiredEventCount = i + 5;
break;
}
}
if (requiredEventCount == -1) {
return null;
}
if (size < requiredEventCount) {
return null;
}
return events;
}
});
final ListIterator<TaskStatusEvent> it = events.getEvents().listIterator();
while (true) {
final TaskStatusEvent event = it.next();
if (event.getStatus().getState() != State.PULLING_IMAGE) {
//rewind so that this event is the one returned by the next call to it.next() below
it.previous();
break;
}
assertThat(event, not(hasContainerId()));
}
assertThat(it.next(), allOf(hasState(State.CREATING), not(hasContainerId())));
assertThat(it.next(), allOf(hasState(State.STARTING), hasContainerId()));
assertThat(it.next(), hasState(State.RUNNING));
assertThat(it.next(), hasState(State.STOPPING));
assertThat(it.next(), hasState(State.EXITED, State.STOPPED));
}
Aggregations