use of bio.fkaiser.fit3d.web.model.Fit3DJob in project fit3d by fkaiserbio.
the class JobManager method init.
@PostConstruct
public void init() {
// schedule updating of load for execution
TimerTask loadMonitoringTask = new TimerTask() {
@Override
public void run() {
int runningJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isRunning).count();
int enqueuedJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isEnqueued).count();
int finishedJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isFinished).count();
int failedJobCount = (int) managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isFailed).count();
jobLoadManager.setRunningJobCount(runningJobCount);
jobLoadManager.setEnqueuedJobCount(enqueuedJobCount);
jobLoadManager.setFinishedJobCount(finishedJobCount);
jobLoadManager.setFailedJobCount(failedJobCount);
}
};
// schedule job cleaning
TimerTask jobCleaningTask = new TimerTask() {
@Override
public void run() {
logger.info("looking for expired jobs");
List<Fit3DJob> oldJobs = managedJobs.values().stream().flatMap(Collection::stream).filter(job -> job.getJobAgeInHours() > Fit3DWebConstants.JobManager.JOB_AGE_IN_HOURS).collect(Collectors.toList());
for (Fit3DJob oldJob : oldJobs) {
oldJob.cancel();
try {
oldJob.delete();
Optional<List<Fit3DJob>> jobList = managedJobs.values().stream().filter(jobs -> jobs.stream().anyMatch(job -> job.equals(oldJob))).findFirst();
if (jobList.isPresent()) {
Iterator<Fit3DJob> iterator = jobList.get().iterator();
while (iterator.hasNext()) {
Fit3DJob currentJob = iterator.next();
if (currentJob.equals(oldJob)) {
iterator.remove();
logger.info("removed job {} from managed jobs", oldJob);
}
}
}
} catch (IOException e) {
logger.error("failed to delete old job {}", oldJob);
}
}
}
};
TimerTask jobNotificationTask = new TimerTask() {
@Override
public void run() {
List<Fit3DJob> notifiableJobs = managedJobs.values().stream().flatMap(Collection::stream).filter(Fit3DJob::isSendMail).collect(Collectors.toList());
for (Fit3DJob job : notifiableJobs) {
mailNotifier.sendMail(job);
job.setSendMail(false);
mongoCollection.updateOne(eq("jobIdentifier", job.toString()), new Document("$set", new Document().append("sendMail", false)));
}
}
};
Timer timer = new Timer(true);
timer.schedule(loadMonitoringTask, 1000, Fit3DWebConstants.JobManager.LOAD_UPDATE_INTERVAL);
timer.schedule(jobCleaningTask, 1000, Fit3DWebConstants.JobManager.CLEANUP_INTERVAL);
timer.schedule(jobNotificationTask, 1000, 1000);
}
Aggregations