no.bekk.dbscheduler.ui.service.TaskLogic Maven / Gradle / Ivy
Show all versions of db-scheduler-ui Show documentation
/*
* 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