use of io.mantisrx.master.api.akka.route.utils.JobDiscoveryHeartbeats.SCHED_INFO_HB_INSTANCE in project mantis by Netflix.
the class JobDiscoveryRouteHandlerAkkaImpl method schedulingInfoStream.
@Override
public CompletionStage<JobDiscoveryRouteProto.SchedInfoResponse> schedulingInfoStream(final GetJobSchedInfoRequest request, final boolean sendHeartbeats) {
CompletionStage<GetJobSchedInfoResponse> response = schedInfoCache.get(request);
try {
AtomicBoolean isJobCompleted = new AtomicBoolean(false);
final String jobId = request.getJobId().getId();
final JobSchedulingInfo completedJobSchedulingInfo = new JobSchedulingInfo(jobId, new HashMap<>());
CompletionStage<JobDiscoveryRouteProto.SchedInfoResponse> jobSchedInfoObsCS = response.thenApply(getJobSchedInfoResp -> {
Optional<BehaviorSubject<JobSchedulingInfo>> jobStatusSubjectO = getJobSchedInfoResp.getJobSchedInfoSubject();
if (getJobSchedInfoResp.responseCode.equals(BaseResponse.ResponseCode.SUCCESS) && jobStatusSubjectO.isPresent()) {
BehaviorSubject<JobSchedulingInfo> jobSchedulingInfoObs = jobStatusSubjectO.get();
Observable<JobSchedulingInfo> heartbeats = Observable.interval(5, serverIdleConnectionTimeout.getSeconds() - 1, TimeUnit.SECONDS).map(x -> {
if (!isJobCompleted.get()) {
return SCHED_INFO_HB_INSTANCE;
} else {
return completedJobSchedulingInfo;
}
}).takeWhile(x -> sendHeartbeats == true);
// Job SchedulingInfo obs completes on job shutdown. Use the do On completed as a signal to inform the user that there are no workers to connect to.
// TODO For future a more explicit key in the payload saying the job is completed.
Observable<JobSchedulingInfo> jobSchedulingInfoWithHBObs = Observable.merge(jobSchedulingInfoObs.doOnCompleted(() -> isJobCompleted.set(true)), heartbeats);
return new JobDiscoveryRouteProto.SchedInfoResponse(getJobSchedInfoResp.requestId, getJobSchedInfoResp.responseCode, getJobSchedInfoResp.message, jobSchedulingInfoWithHBObs);
} else {
logger.info("Failed to get Sched info stream for {}", request.getJobId().getId());
schedInfoStreamErrors.increment();
return new JobDiscoveryRouteProto.SchedInfoResponse(getJobSchedInfoResp.requestId, getJobSchedInfoResp.responseCode, getJobSchedInfoResp.message);
}
});
return jobSchedInfoObsCS;
} catch (Exception e) {
logger.error("caught exception fetching sched info stream for {}", request.getJobId().getId(), e);
schedInfoStreamErrors.increment();
return CompletableFuture.completedFuture(new JobDiscoveryRouteProto.SchedInfoResponse(0, BaseResponse.ResponseCode.SERVER_ERROR, "Failed to get SchedulingInfo stream for jobId " + request.getJobId().getId() + " error: " + e.getMessage()));
}
}
Aggregations