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

com.github.kagkarlsson.scheduler.task.CompletionHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) Gustav Karlsson
 *
 * 

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 com.github.kagkarlsson.scheduler.task; import com.github.kagkarlsson.scheduler.task.schedule.Schedule; import java.time.Instant; import java.util.function.Function; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public interface CompletionHandler { void complete(ExecutionComplete executionComplete, ExecutionOperations executionOperations); class OnCompleteRemove implements CompletionHandler { @Override public void complete( ExecutionComplete executionComplete, ExecutionOperations executionOperations) { executionOperations.stop(); } } class OnCompleteReschedule implements CompletionHandler { private static final Logger LOG = LoggerFactory.getLogger(OnCompleteReschedule.class); private final Schedule schedule; private final boolean setNewData; private T newData; public OnCompleteReschedule(Schedule schedule) { this.schedule = schedule; this.setNewData = false; } public OnCompleteReschedule(Schedule schedule, T newData) { this.schedule = schedule; this.newData = newData; this.setNewData = true; } @Override public void complete( ExecutionComplete executionComplete, ExecutionOperations executionOperations) { Instant nextExecution = schedule.getNextExecutionTime(executionComplete); LOG.debug( "Rescheduling task {} to {}", executionComplete.getExecution().taskInstance, nextExecution); if (setNewData) { executionOperations.reschedule(executionComplete, nextExecution, newData); } else { executionOperations.reschedule(executionComplete, nextExecution); } } @Override public String toString() { return "OnCompleteReschedule with " + schedule; } } class OnCompleteReplace implements CompletionHandler { private static final Logger LOG = LoggerFactory.getLogger(OnCompleteReplace.class); private String newTaskName = ""; // used for logging purposes only private final Function, SchedulableInstance> newInstanceCreator; public OnCompleteReplace(String newTaskName) { this(newTaskName, null); } public OnCompleteReplace(String newTaskName, T newData) { this( (TaskInstance currentInstance) -> { return SchedulableInstance.of( new TaskInstance<>(newTaskName, currentInstance.getId(), newData), Instant.now()); }); this.newTaskName = newTaskName; } public OnCompleteReplace(TaskDescriptor newTask, T newData) { this( (TaskInstance currentInstance) -> { return SchedulableInstance.of( new TaskInstance<>(newTask.getTaskName(), currentInstance.getId(), newData), Instant.now()); }); this.newTaskName = newTask.getTaskName(); } public OnCompleteReplace(Function, SchedulableInstance> newInstanceCreator) { this.newInstanceCreator = newInstanceCreator; } @Override @SuppressWarnings({"unchecked"}) public void complete( ExecutionComplete executionComplete, ExecutionOperations executionOperations) { TaskInstance currentInstance = executionComplete.getExecution().taskInstance; SchedulableInstance nextInstance = newInstanceCreator.apply(currentInstance); LOG.debug( "Removing instance {} and scheduling instance {}", executionComplete.getExecution().taskInstance, nextInstance); executionOperations.removeAndScheduleNew(nextInstance); } @Override public String toString() { return "OnCompleteReplace with task '" + newTaskName + "'"; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy