Skip to content

Commit 340fc7b

Browse files
committed
feat: rename client.schedule(..) to clarify hidden behavior where nothing is scheduled if the instance already exists
1 parent 1ff9ff4 commit 340fc7b

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/Scheduler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ public <T> void schedule(SchedulableInstance<T> schedulableInstance) {
252252
this.delegate.schedule(schedulableInstance);
253253
}
254254

255+
@Override
256+
public <T> boolean scheduleIfNotExists(TaskInstance<T> taskInstance, Instant executionTime) {
257+
return this.delegate.scheduleIfNotExists(taskInstance, executionTime);
258+
}
259+
260+
@Override
261+
public <T> boolean scheduleIfNotExists(SchedulableInstance<T> schedulableInstance) {
262+
return this.delegate.scheduleIfNotExists(schedulableInstance);
263+
}
264+
255265
@Override
256266
public <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime) {
257267
this.delegate.schedule(taskInstance, executionTime);

db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/SchedulerClient.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,39 @@
4040
public interface SchedulerClient {
4141

4242
/**
43-
* Schedule a new execution.
43+
* Schedule a new execution if task instance does not already exists.
4444
*
4545
* @param taskInstance Task-instance, optionally with data
4646
* @param executionTime Instant it should run
4747
* @see java.time.Instant
4848
* @see com.github.kagkarlsson.scheduler.task.TaskInstance
4949
*/
50+
@Deprecated
5051
<T> void schedule(TaskInstance<T> taskInstance, Instant executionTime);
5152

53+
@Deprecated
5254
<T> void schedule(SchedulableInstance<T> schedulableInstance);
5355

56+
/**
57+
* Schedule a new execution if task instance does not already exists.
58+
*
59+
* @param taskInstance Task-instance, optionally with data
60+
* @param executionTime Instant it should run
61+
* @see java.time.Instant
62+
* @see com.github.kagkarlsson.scheduler.task.TaskInstance
63+
* @return true if scheduled successfully
64+
*/
65+
<T> boolean scheduleIfNotExists(TaskInstance<T> taskInstance, Instant executionTime);
66+
67+
/**
68+
* Schedule a new execution if task instance does not already exists.
69+
*
70+
* @param schedulableInstance Task-instance and time it should run
71+
* @see com.github.kagkarlsson.scheduler.task.SchedulableInstance
72+
* @return true if scheduled successfully
73+
*/
74+
<T> boolean scheduleIfNotExists(SchedulableInstance<T> schedulableInstance);
75+
5476
/**
5577
* Update an existing execution to a new execution-time. If the execution does not exist or if it
5678
* is currently running, an exception is thrown.
@@ -256,11 +278,25 @@ class StandardSchedulerClient implements SchedulerClient {
256278

257279
@Override
258280
public <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime) {
281+
// ignore result even if failed to schedule due to duplicates for backwards-compatibility
282+
scheduleIfNotExists(taskInstance, executionTime);
283+
}
284+
285+
@Override
286+
public <T> boolean scheduleIfNotExists(TaskInstance<T> taskInstance, Instant executionTime) {
259287
boolean success =
260288
taskRepository.createIfNotExists(SchedulableInstance.of(taskInstance, executionTime));
261289
if (success) {
262290
notifyListeners(ClientEvent.EventType.SCHEDULE, taskInstance, executionTime);
263291
}
292+
return success;
293+
}
294+
295+
@Override
296+
public <T> boolean scheduleIfNotExists(SchedulableInstance<T> schedulableInstance) {
297+
return scheduleIfNotExists(
298+
schedulableInstance.getTaskInstance(),
299+
schedulableInstance.getNextExecutionTime(clock.now()));
264300
}
265301

266302
@Override

db-scheduler/src/test/java/com/github/kagkarlsson/scheduler/SchedulerClientTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.hamcrest.MatcherAssert.assertThat;
66
import static org.hamcrest.Matchers.*;
77
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertFalse;
89
import static org.junit.jupiter.api.Assertions.assertTrue;
910

1011
import co.unruly.matchers.OptionalMatchers;
@@ -79,10 +80,20 @@ public void setUp() {
7980
@Test
8081
public void client_should_be_able_to_schedule_executions() {
8182
SchedulerClient client = create(DB.getDataSource()).build();
83+
84+
// test deprecated method
8285
client.schedule(oneTimeTaskA.instance("1"), settableClock.now());
86+
assertFalse(client.scheduleIfNotExists(oneTimeTaskA.instance("1"), settableClock.now()));
8387

8488
scheduler.runAnyDueExecutions();
8589
assertThat(onetimeTaskHandlerA.timesExecuted.get(), CoreMatchers.is(1));
90+
91+
// test new method
92+
client.scheduleIfNotExists(oneTimeTaskA.instance("1"), settableClock.now());
93+
assertFalse(client.scheduleIfNotExists(oneTimeTaskA.instance("1"), settableClock.now()));
94+
95+
scheduler.runAnyDueExecutions();
96+
assertThat(onetimeTaskHandlerA.timesExecuted.get(), CoreMatchers.is(2));
8697
}
8798

8899
@Test

0 commit comments

Comments
 (0)