use of io.datarouter.tasktracker.storage.LongRunningTask in project datarouter by hotpads.
the class JobHandler method jobToTriggerJspDto.
private TriggerJspDto jobToTriggerJspDto(int rowId, JobPackage jobPackage, LongRunningTaskSummaryDto longRunningTaskSummary) {
String taskName = LongRunningTaskTrackerFactory.taskNameForClass(jobPackage.jobClass);
LongRunningTask currentlyRunningTask = longRunningTaskSummary.currentlyRunningTasks.get(taskName);
String heartbeatStatus = Optional.ofNullable(currentlyRunningTask).map(LongRunningTask::getHeartbeatStatus).map(LongRunningTaskHeartBeatStatus::getPersistentString).orElse(null);
LongRunningTask lastFinishedTask = longRunningTaskSummary.lastCompletions.get(taskName);
Set<String> servers = longRunningTaskSummary.runningOnServers.getOrDefault(taskName, new TreeSet<>());
String serversCsv = String.join(",", servers);
return new TriggerJspDto(rowId, jobPackage.jobClass.getName(), jobPackage.jobClass.getSimpleName(), jobPackage.shouldRunSupplier.get(), heartbeatStatus, jobPackage.usesLocking() ? "locked" : "parallel", jobPackage.getCronExpressionString().orElse(""), jobPackage.jobCategoryName, lastFinishedTask == null ? null : lastFinishedTask.getFinishTimeString(), lastFinishedTask == null ? -1 : lastFinishedTask.getFinishTime().getTime(), serversCsv);
}
use of io.datarouter.tasktracker.storage.LongRunningTask in project datarouter by hotpads.
the class LongRunningTaskService method getSummary.
public LongRunningTaskSummaryDto getSummary() {
Map<String, LongRunningTask> lastCompletions = new HashMap<>();
Map<String, LongRunningTask> currentlyRunningTasks = new HashMap<>();
Map<String, SortedSet<String>> runningOnServers = new HashMap<>();
for (LongRunningTask task : dao.scan().iterable()) {
String name = task.getKey().getName();
if (task.isRunning()) {
// currentlyRunningTasks
LongRunningTask current = currentlyRunningTasks.get(name);
if (current == null || task.getStartTime().after(current.getStartTime())) {
currentlyRunningTasks.put(name, task);
}
// runningOnServers
runningOnServers.putIfAbsent(name, new TreeSet<>());
runningOnServers.get(name).add(task.getKey().getServerName());
}
// lastCompletions
if (task.isSuccess()) {
LongRunningTask current = lastCompletions.get(name);
if (current == null || task.getFinishTimeInstant().isAfter(current.getFinishTimeInstant())) {
lastCompletions.put(name, task);
}
}
}
return new LongRunningTaskSummaryDto(lastCompletions, currentlyRunningTasks, runningOnServers);
}
use of io.datarouter.tasktracker.storage.LongRunningTask in project datarouter by hotpads.
the class LongRunningTaskVacuumService method vacuumRelatedTasks.
private void vacuumRelatedTasks(List<LongRunningTask> tasks) {
// remove really old entries
Instant tooOldCutoff = Instant.now().minus(settings.maxAge.get().toJavaDuration());
List<LongRunningTask> tooOld = tasks.stream().filter(task -> task.getKey().getTriggerTime().toInstant().isBefore(tooOldCutoff)).collect(Collectors.toList());
Scanner.of(tooOld).map(Databean::getKey).then(dao::deleteBatched);
// keep the latest N
List<LongRunningTask> remaining = new ArrayList<>(tasks);
remaining.removeAll(tooOld);
if (remaining.size() <= settings.countToKeep.get()) {
return;
}
Scanner.of(remaining).limit(remaining.size() - settings.countToKeep.get()).map(Databean::getKey).then(dao::deleteBatched);
}
use of io.datarouter.tasktracker.storage.LongRunningTask in project datarouter by hotpads.
the class LongRunningTaskDaoTests method testGetOldestRunSinceLastSuccess.
@Test
public void testGetOldestRunSinceLastSuccess() {
List<LongRunningTask> tasks = new ArrayList<>();
// null when empty
Assert.assertNull(getOldestRunSinceLastSuccess(tasks));
LongRunningTask first = new LongRunningTask();
tasks.add(first);
first.setJobExecutionStatus(LongRunningTaskStatus.SUCCESS);
// only one item, which is SUCCESS, so return null
Assert.assertNull(getOldestRunSinceLastSuccess(tasks));
first.setJobExecutionStatus(LongRunningTaskStatus.INTERRUPTED);
// one item, not SUCCESS, so return it
Assert.assertEquals(getOldestRunSinceLastSuccess(tasks), first);
LongRunningTask second = new LongRunningTask();
tasks.add(second);
second.setJobExecutionStatus(LongRunningTaskStatus.SUCCESS);
// most recent item is SUCCESS, so null
Assert.assertNull(getOldestRunSinceLastSuccess(tasks));
second.setJobExecutionStatus(LongRunningTaskStatus.RUNNING);
// all items are not SUCCESS, so return oldest one
Assert.assertEquals(getOldestRunSinceLastSuccess(tasks), first);
first.setJobExecutionStatus(LongRunningTaskStatus.SUCCESS);
// only one item since last SUCCESS, so return it
Assert.assertEquals(getOldestRunSinceLastSuccess(tasks), second);
}
use of io.datarouter.tasktracker.storage.LongRunningTask in project datarouter by hotpads.
the class JobsHealthHandler method uniqueTasks.
@Handler(defaultHandler = true)
Mav uniqueTasks() {
Mav mav = new Mav(files.jsp.admin.datarouter.tasktracker.jobsHealthJsp);
List<LongRunningTask> allTasks = longRunningTaskDao.scan().include(task -> task.getStartTime() == null || task.getStartTime().getTime() > System.currentTimeMillis() - Duration.ofDays(1).toMillis()).list();
List<String> uniqueJobs = allTasks.stream().map(LongRunningTask::getKey).map(LongRunningTaskKey::getName).distinct().collect(Collectors.toList());
Integer numRunningJobs = 0;
ZoneId zoneId = currentUserSessionInfoService.getZoneId(request);
List<LongRunningTaskJspDto> allBadTasks = new ArrayList<>();
for (LongRunningTask task : allTasks) {
if (task.isBadState()) {
allBadTasks.add(new LongRunningTaskJspDto(task, zoneId));
} else if (task.isRunning()) {
numRunningJobs++;
}
}
mav.put("allBadTasks", allBadTasks);
mav.put("uniqueJobs", uniqueJobs);
mav.put("numUniqueJobs", uniqueJobs.size());
mav.put("numRunningJobs", numRunningJobs);
mav.put("legend", LongRunningTasksHandler.legend());
return mav;
}
Aggregations