Skip to content

Commit 679332b

Browse files
authored
feat: rename client.schedule(..) to clarify hidden behavior where nothing is scheduled if the instance already exists (#496)
Rename `client.schedule(...)` method to `client.scheduleIfNotExists(...)` to clarify default but slightly hidden behavior where nothing is scheduled if the instance already exists. Deprecated old `client.schedule(...)` methods. Fixes #326
1 parent 309c355 commit 679332b

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-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: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,43 @@
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
49+
* @deprecated use {@link #scheduleIfNotExists(TaskInstance, Instant)} instead.
4950
*/
51+
@Deprecated
5052
<T> void schedule(TaskInstance<T> taskInstance, Instant executionTime);
5153

54+
/**
55+
* @deprecated use {@link #scheduleIfNotExists(SchedulableInstance)} instead.
56+
*/
57+
@Deprecated
5258
<T> void schedule(SchedulableInstance<T> schedulableInstance);
5359

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

261287
@Override
262288
public <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime) {
289+
// ignore result even if failed to schedule due to duplicates for backwards-compatibility
290+
scheduleIfNotExists(taskInstance, executionTime);
291+
}
292+
293+
@Override
294+
public <T> boolean scheduleIfNotExists(TaskInstance<T> taskInstance, Instant executionTime) {
263295
boolean success =
264296
taskRepository.createIfNotExists(SchedulableInstance.of(taskInstance, executionTime));
265297
if (success) {
266298
notifyListeners(ClientEvent.EventType.SCHEDULE, taskInstance, executionTime);
267299
}
300+
return success;
301+
}
302+
303+
@Override
304+
public <T> boolean scheduleIfNotExists(SchedulableInstance<T> schedulableInstance) {
305+
return scheduleIfNotExists(
306+
schedulableInstance.getTaskInstance(),
307+
schedulableInstance.getNextExecutionTime(clock.now()));
268308
}
269309

270310
@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)