All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.bekk.dbscheduler.ui.service.TaskLogic Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/*
 * Copyright (C) Bekk
 *
 * 

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at * *

http://www.apache.org/licenses/LICENSE-2.0 * *

Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package no.bekk.dbscheduler.ui.service; import static no.bekk.dbscheduler.ui.util.QueryUtils.filterExecutions; import com.github.kagkarlsson.scheduler.ScheduledExecution; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.TaskInstanceId; import java.time.Instant; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import no.bekk.dbscheduler.ui.model.GetTasksResponse; import no.bekk.dbscheduler.ui.model.PollResponse; import no.bekk.dbscheduler.ui.model.TaskDetailsRequestParams; import no.bekk.dbscheduler.ui.model.TaskModel; import no.bekk.dbscheduler.ui.model.TaskRequestParams; import no.bekk.dbscheduler.ui.util.Caching; import no.bekk.dbscheduler.ui.util.QueryUtils; import no.bekk.dbscheduler.ui.util.mapper.TaskMapper; import org.springframework.http.HttpStatus; import org.springframework.web.server.ResponseStatusException; public class TaskLogic { private final Scheduler scheduler; private final Caching caching; private final boolean showData; public TaskLogic(Scheduler scheduler, Caching caching, boolean showData) { this.scheduler = scheduler; this.caching = caching; this.showData = showData; } public void runTaskNow(String taskId, String taskName, Instant scheduleTime) { Optional> scheduledExecutionOpt = scheduler.getScheduledExecution(TaskInstanceId.of(taskName, taskId)); if (scheduledExecutionOpt.isPresent() && !scheduledExecutionOpt.get().isPicked()) { scheduler.reschedule( scheduledExecutionOpt.get().getTaskInstance(), scheduleTime != null ? scheduleTime : Instant.now()); } else { throw new ResponseStatusException( HttpStatus.NOT_FOUND, "No ScheduledExecution found for taskName: " + taskName + ", taskId: " + taskId); } } public void runTaskGroupNow(String taskName, boolean onlyFailed) { caching .getExecutionsFromCacheOrDB(false, scheduler) .forEach( (execution) -> { if ((!onlyFailed || execution.getConsecutiveFailures() > 0) && taskName.equals(execution.getTaskInstance().getTaskName())) { try { runTaskNow( execution.getTaskInstance().getId(), execution.getTaskInstance().getTaskName(), Instant.now()); } catch (ResponseStatusException e) { System.out.println("Failed to run task: " + e.getMessage()); } } }); } public void deleteTask(String taskId, String taskName) { Optional> scheduledExecutionOpt = scheduler.getScheduledExecution(TaskInstanceId.of(taskName, taskId)); if (scheduledExecutionOpt.isPresent()) { TaskInstanceId taskInstance = scheduledExecutionOpt.get().getTaskInstance(); scheduler.cancel(taskInstance); } else { throw new ResponseStatusException( HttpStatus.NOT_FOUND, "No ScheduledExecution found for taskName: " + taskName + ", taskId: " + taskId); } } public GetTasksResponse getAllTasks(TaskRequestParams params) { List> executions = caching.getExecutionsFromCacheOrDB(params.isRefresh(), scheduler); List tasks = TaskMapper.mapAllExecutionsToTaskModelUngrouped(executions); tasks = QueryUtils.searchByTaskName( tasks, params.getSearchTermTaskName(), params.isTaskNameExactMatch()); tasks = QueryUtils.searchByTaskInstance( tasks, params.getSearchTermTaskInstance(), params.isTaskInstanceExactMatch()); if (!showData) { tasks.forEach(e -> e.setTaskData(List.of())); } tasks = TaskMapper.groupTasks(tasks); tasks = QueryUtils.sortTasks( QueryUtils.filterTasks(tasks, params.getFilter()), params.getSorting(), params.isAsc()); List pagedTasks = QueryUtils.paginate(tasks, params.getPageNumber(), params.getSize()); return new GetTasksResponse(tasks.size(), pagedTasks, params.getSize()); } public GetTasksResponse getTask(TaskDetailsRequestParams params) { List> executions = caching.getExecutionsFromCacheOrDB(params.isRefresh(), scheduler); List tasks = params.getTaskId() != null ? TaskMapper.mapAllExecutionsToTaskModelUngrouped(executions).stream() .filter( task -> { return task.getTaskName().equals(params.getTaskName()) && task.getTaskInstance().get(0).equals(params.getTaskId()); }) .collect(Collectors.toList()) : TaskMapper.mapAllExecutionsToTaskModelUngrouped(executions).stream() .filter( task -> { return task.getTaskName().equals(params.getTaskName()); }) .collect(Collectors.toList()); if (tasks.isEmpty()) { throw new ResponseStatusException( HttpStatus.NOT_FOUND, "No tasks found for taskName: " + params.getTaskName() + ", taskId: " + params.getTaskId()); } tasks = QueryUtils.searchByTaskName( tasks, params.getSearchTermTaskName(), params.isTaskNameExactMatch()); tasks = QueryUtils.searchByTaskInstance( tasks, params.getSearchTermTaskInstance(), params.isTaskInstanceExactMatch()); tasks = QueryUtils.sortTasks( QueryUtils.filterTasks(tasks, params.getFilter()), params.getSorting(), params.isAsc()); if (!showData) { List list = new ArrayList<>() { { add(null); } }; tasks.forEach(e -> e.setTaskData(list)); } List pagedTasks = QueryUtils.paginate(tasks, params.getPageNumber(), params.getSize()); return new GetTasksResponse(tasks.size(), pagedTasks, params.getSize()); } public PollResponse pollTasks(TaskDetailsRequestParams params) { List> allTasks = filterExecutions( caching.getExecutionsFromDBWithoutUpdatingCache(scheduler), TaskRequestParams.TaskFilter.ALL, params.getTaskName()); Set newTaskNames = new HashSet<>(); Set newFailureTaskNames = new HashSet<>(); Set newRunningTaskNames = new HashSet<>(); int stoppedFailing = 0; int finishedRunning = 0; for (ScheduledExecution task : allTasks) { String taskName = task.getTaskInstance().getTaskName(); String status = getStatus(task); String cachedStatus = caching.getStatusFromCache(task.getTaskInstance()); if (cachedStatus == null) { handleNewTask( params, newTaskNames, newFailureTaskNames, newRunningTaskNames, taskName, status); continue; } if (!cachedStatus.equals(status)) { handleStatusChange( params, newFailureTaskNames, newRunningTaskNames, taskName, status, cachedStatus, stoppedFailing, finishedRunning); } } return new PollResponse( newFailureTaskNames.size(), newRunningTaskNames.size(), newTaskNames.size(), stoppedFailing, finishedRunning); } private void handleNewTask( TaskDetailsRequestParams params, Set newTaskNames, Set newFailureTaskNames, Set newRunningTaskNames, String taskName, String status) { if (newTaskNames.contains(taskName) && params.getTaskName() == null) { return; } newTaskNames.add(taskName); if (status.charAt(0) == '1') { newFailureTaskNames.add(taskName); } if (status.charAt(1) == '1') { newRunningTaskNames.add(taskName); } } private void handleStatusChange( TaskDetailsRequestParams params, Set newFailureTaskNames, Set newRunningTaskNames, String taskName, String status, String cachedStatus, int stoppedFailing, int finishedRunning) { if (cachedStatus.charAt(0) == '0' && status.charAt(0) == '1' && (!newFailureTaskNames.contains(taskName) || params.getTaskName() != null)) { newFailureTaskNames.add(taskName); } if (cachedStatus.charAt(0) == '1' && status.charAt(0) == '0') { stoppedFailing++; } if (cachedStatus.charAt(1) == '0' && status.charAt(1) == '1' && (!newRunningTaskNames.contains(taskName) || params.getTaskName() != null)) { newRunningTaskNames.add(taskName); } if (cachedStatus.charAt(1) == '1' && status.charAt(1) == '0') { finishedRunning++; } } private String getStatus(ScheduledExecution task) { return (task.getConsecutiveFailures() > 0 ? "1" : "0") + (task.getPickedBy() != null ? "1" : "0"); } }