Search in sources :

Example 1 with LongRunningTask

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);
}
Also used : OptionalString(io.datarouter.web.handler.types.optional.OptionalString) LongRunningTask(io.datarouter.tasktracker.storage.LongRunningTask)

Example 2 with LongRunningTask

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);
}
Also used : HashMap(java.util.HashMap) LongRunningTask(io.datarouter.tasktracker.storage.LongRunningTask) SortedSet(java.util.SortedSet)

Example 3 with LongRunningTask

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);
}
Also used : Scanner(io.datarouter.scanner.Scanner) ObjectTool(io.datarouter.util.lang.ObjectTool) TaskTracker(io.datarouter.instrumentation.task.TaskTracker) Instant(java.time.Instant) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) List(java.util.List) Databean(io.datarouter.model.databean.Databean) LongRunningTaskDao(io.datarouter.tasktracker.storage.LongRunningTaskDao) DatarouterTaskTrackerSettingRoot(io.datarouter.tasktracker.config.DatarouterTaskTrackerSettingRoot) ListTool(io.datarouter.util.collection.ListTool) LongRunningTask(io.datarouter.tasktracker.storage.LongRunningTask) Instant(java.time.Instant) ArrayList(java.util.ArrayList) LongRunningTask(io.datarouter.tasktracker.storage.LongRunningTask)

Example 4 with LongRunningTask

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);
}
Also used : ArrayList(java.util.ArrayList) LongRunningTask(io.datarouter.tasktracker.storage.LongRunningTask) Test(org.testng.annotations.Test)

Example 5 with LongRunningTask

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;
}
Also used : DatarouterTaskTrackerFiles(io.datarouter.tasktracker.config.DatarouterTaskTrackerFiles) CurrentUserSessionInfoService(io.datarouter.web.user.session.CurrentUserSessionInfoService) LongRunningTaskKey(io.datarouter.tasktracker.storage.LongRunningTaskKey) Mav(io.datarouter.web.handler.mav.Mav) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) ArrayList(java.util.ArrayList) LongRunningTaskJspDto(io.datarouter.tasktracker.web.LongRunningTasksHandler.LongRunningTaskJspDto) Inject(javax.inject.Inject) List(java.util.List) LongRunningTaskDao(io.datarouter.tasktracker.storage.LongRunningTaskDao) Duration(java.time.Duration) BaseHandler(io.datarouter.web.handler.BaseHandler) LongRunningTask(io.datarouter.tasktracker.storage.LongRunningTask) Mav(io.datarouter.web.handler.mav.Mav) LongRunningTaskJspDto(io.datarouter.tasktracker.web.LongRunningTasksHandler.LongRunningTaskJspDto) ZoneId(java.time.ZoneId) ArrayList(java.util.ArrayList) LongRunningTask(io.datarouter.tasktracker.storage.LongRunningTask) LongRunningTaskKey(io.datarouter.tasktracker.storage.LongRunningTaskKey) BaseHandler(io.datarouter.web.handler.BaseHandler)

Aggregations

LongRunningTask (io.datarouter.tasktracker.storage.LongRunningTask)5 ArrayList (java.util.ArrayList)3 LongRunningTaskDao (io.datarouter.tasktracker.storage.LongRunningTaskDao)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 TaskTracker (io.datarouter.instrumentation.task.TaskTracker)1 Databean (io.datarouter.model.databean.Databean)1 Scanner (io.datarouter.scanner.Scanner)1 DatarouterTaskTrackerFiles (io.datarouter.tasktracker.config.DatarouterTaskTrackerFiles)1 DatarouterTaskTrackerSettingRoot (io.datarouter.tasktracker.config.DatarouterTaskTrackerSettingRoot)1 LongRunningTaskKey (io.datarouter.tasktracker.storage.LongRunningTaskKey)1 LongRunningTaskJspDto (io.datarouter.tasktracker.web.LongRunningTasksHandler.LongRunningTaskJspDto)1 ListTool (io.datarouter.util.collection.ListTool)1 ObjectTool (io.datarouter.util.lang.ObjectTool)1 BaseHandler (io.datarouter.web.handler.BaseHandler)1 Mav (io.datarouter.web.handler.mav.Mav)1 OptionalString (io.datarouter.web.handler.types.optional.OptionalString)1 CurrentUserSessionInfoService (io.datarouter.web.user.session.CurrentUserSessionInfoService)1 Duration (java.time.Duration)1