diff --git a/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/Scheduler.java b/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/Scheduler.java index 60e0c9a2..8abedbe4 100644 --- a/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/Scheduler.java +++ b/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/Scheduler.java @@ -252,6 +252,16 @@ public void schedule(SchedulableInstance schedulableInstance) { this.delegate.schedule(schedulableInstance); } + @Override + public boolean scheduleIfNotExists(TaskInstance taskInstance, Instant executionTime) { + return this.delegate.scheduleIfNotExists(taskInstance, executionTime); + } + + @Override + public boolean scheduleIfNotExists(SchedulableInstance schedulableInstance) { + return this.delegate.scheduleIfNotExists(schedulableInstance); + } + @Override public void schedule(TaskInstance taskInstance, Instant executionTime) { this.delegate.schedule(taskInstance, executionTime); diff --git a/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/SchedulerClient.java b/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/SchedulerClient.java index 624d448a..3f89067a 100644 --- a/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/SchedulerClient.java +++ b/db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/SchedulerClient.java @@ -40,17 +40,43 @@ public interface SchedulerClient { /** - * Schedule a new execution. + * Schedule a new execution if task instance does not already exists. * * @param taskInstance Task-instance, optionally with data * @param executionTime Instant it should run * @see java.time.Instant * @see com.github.kagkarlsson.scheduler.task.TaskInstance + * @deprecated use {@link #scheduleIfNotExists(TaskInstance, Instant)} instead. */ + @Deprecated void schedule(TaskInstance taskInstance, Instant executionTime); + /** + * @deprecated use {@link #scheduleIfNotExists(SchedulableInstance)} instead. + */ + @Deprecated void schedule(SchedulableInstance schedulableInstance); + /** + * Schedule a new execution if task instance does not already exists. + * + * @param taskInstance Task-instance, optionally with data + * @param executionTime Instant it should run + * @see java.time.Instant + * @see com.github.kagkarlsson.scheduler.task.TaskInstance + * @return true if scheduled successfully + */ + boolean scheduleIfNotExists(TaskInstance taskInstance, Instant executionTime); + + /** + * Schedule a new execution if task instance does not already exists. + * + * @param schedulableInstance Task-instance and time it should run + * @see com.github.kagkarlsson.scheduler.task.SchedulableInstance + * @return true if scheduled successfully + */ + boolean scheduleIfNotExists(SchedulableInstance schedulableInstance); + /** * Update an existing execution to a new execution-time. If the execution does not exist or if it * is currently running, an exception is thrown. @@ -260,11 +286,25 @@ class StandardSchedulerClient implements SchedulerClient { @Override public void schedule(TaskInstance taskInstance, Instant executionTime) { + // ignore result even if failed to schedule due to duplicates for backwards-compatibility + scheduleIfNotExists(taskInstance, executionTime); + } + + @Override + public boolean scheduleIfNotExists(TaskInstance taskInstance, Instant executionTime) { boolean success = taskRepository.createIfNotExists(SchedulableInstance.of(taskInstance, executionTime)); if (success) { notifyListeners(ClientEvent.EventType.SCHEDULE, taskInstance, executionTime); } + return success; + } + + @Override + public boolean scheduleIfNotExists(SchedulableInstance schedulableInstance) { + return scheduleIfNotExists( + schedulableInstance.getTaskInstance(), + schedulableInstance.getNextExecutionTime(clock.now())); } @Override diff --git a/db-scheduler/src/test/java/com/github/kagkarlsson/scheduler/SchedulerClientTest.java b/db-scheduler/src/test/java/com/github/kagkarlsson/scheduler/SchedulerClientTest.java index d156c17a..9d3eee0d 100644 --- a/db-scheduler/src/test/java/com/github/kagkarlsson/scheduler/SchedulerClientTest.java +++ b/db-scheduler/src/test/java/com/github/kagkarlsson/scheduler/SchedulerClientTest.java @@ -5,6 +5,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import co.unruly.matchers.OptionalMatchers; @@ -79,10 +80,20 @@ public void setUp() { @Test public void client_should_be_able_to_schedule_executions() { SchedulerClient client = create(DB.getDataSource()).build(); + + // test deprecated method client.schedule(oneTimeTaskA.instance("1"), settableClock.now()); + assertFalse(client.scheduleIfNotExists(oneTimeTaskA.instance("1"), settableClock.now())); scheduler.runAnyDueExecutions(); assertThat(onetimeTaskHandlerA.timesExecuted.get(), CoreMatchers.is(1)); + + // test new method + client.scheduleIfNotExists(oneTimeTaskA.instance("1"), settableClock.now()); + assertFalse(client.scheduleIfNotExists(oneTimeTaskA.instance("1"), settableClock.now())); + + scheduler.runAnyDueExecutions(); + assertThat(onetimeTaskHandlerA.timesExecuted.get(), CoreMatchers.is(2)); } @Test