com.github.kagkarlsson.scheduler.task.helper.ScheduleRecurringOnStartup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of db-scheduler Show documentation
Show all versions of db-scheduler Show documentation
Simple persistent scheduler for scheduled tasks, recurring or ad-hoc.
/**
* 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.helper;
import com.github.kagkarlsson.scheduler.Clock;
import com.github.kagkarlsson.scheduler.ScheduledExecution;
import com.github.kagkarlsson.scheduler.SchedulerClient;
import com.github.kagkarlsson.scheduler.task.ExecutionComplete;
import com.github.kagkarlsson.scheduler.task.Task;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.github.kagkarlsson.scheduler.task.schedule.Schedule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
class ScheduleRecurringOnStartup implements ScheduleOnStartup {
private static final Logger LOG = LoggerFactory.getLogger(ScheduleRecurringOnStartup.class);
private final Schedule schedule;
private final String instance;
private final T data;
ScheduleRecurringOnStartup(String instance, T data, Schedule schedule) {
this.instance = instance;
this.data = data;
this.schedule = schedule;
}
@Override
public void apply(SchedulerClient scheduler, Clock clock, Task task) {
final TaskInstance instanceWithoutData = task.instance(this.instance);
final Optional> preexistingExecution = scheduler.getScheduledExecution(instanceWithoutData);
if (schedule.isDisabled()) {
if (preexistingExecution.isPresent()) {
LOG.info("Task-instance '{}' has a Schedule that has been marked as disabled. Removing existing execution with execution-time '{}'.",
instanceWithoutData, preexistingExecution.get().getExecutionTime());
tryCancel(scheduler, instanceWithoutData);
} else {
LOG.info("Task-instance '{}' has a Schedule that has been marked as disabled. Will not schedule a new execution", instanceWithoutData);
}
return;
}
if (preexistingExecution.isPresent()) {
Optional newNextExecutionTime = checkForNewExecutionTime(clock, instanceWithoutData, preexistingExecution.get());
newNextExecutionTime.ifPresent(instant -> {
// Intentionally leaving out data here, since we do not want to update existing data
scheduler.reschedule(instanceWithoutData, instant);
});
} else {
// No preexisting execution, create initial one
final Instant initialExecutionTime = schedule.getInitialExecutionTime(clock.now());
LOG.info("Creating initial execution for task-instance '{}'. Next execution-time: {}", instanceWithoutData, initialExecutionTime);
scheduler.schedule(getSchedulableInstance(task), initialExecutionTime);
}
}
private void tryCancel(SchedulerClient scheduler, TaskInstance instanceWithoutData) {
try {
scheduler.cancel(instanceWithoutData);
} catch (RuntimeException e) {
LOG.warn("Failed to cancel existing execution for a Task with a Scheduled marked as disabled. " +
"May happen if another instance already did it, or if it is currently executing.", e);
}
}
Optional checkForNewExecutionTime(Clock clock, TaskInstance instanceWithoutData, ScheduledExecution