|
40 | 40 | public interface SchedulerClient {
|
41 | 41 |
|
42 | 42 | /**
|
43 |
| - * Schedule a new execution. |
| 43 | + * Schedule a new execution if task instance does not already exists. |
44 | 44 | *
|
45 | 45 | * @param taskInstance Task-instance, optionally with data
|
46 | 46 | * @param executionTime Instant it should run
|
47 | 47 | * @see java.time.Instant
|
48 | 48 | * @see com.github.kagkarlsson.scheduler.task.TaskInstance
|
| 49 | + * @deprecated use {@link #scheduleIfNotExists(TaskInstance, Instant)} instead. |
49 | 50 | */
|
| 51 | + @Deprecated |
50 | 52 | <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime);
|
51 | 53 |
|
| 54 | + /** |
| 55 | + * @deprecated use {@link #scheduleIfNotExists(SchedulableInstance)} instead. |
| 56 | + */ |
| 57 | + @Deprecated |
52 | 58 | <T> void schedule(SchedulableInstance<T> schedulableInstance);
|
53 | 59 |
|
| 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 | + |
54 | 80 | /**
|
55 | 81 | * Update an existing execution to a new execution-time. If the execution does not exist or if it
|
56 | 82 | * is currently running, an exception is thrown.
|
@@ -260,11 +286,25 @@ class StandardSchedulerClient implements SchedulerClient {
|
260 | 286 |
|
261 | 287 | @Override
|
262 | 288 | 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) { |
263 | 295 | boolean success =
|
264 | 296 | taskRepository.createIfNotExists(SchedulableInstance.of(taskInstance, executionTime));
|
265 | 297 | if (success) {
|
266 | 298 | notifyListeners(ClientEvent.EventType.SCHEDULE, taskInstance, executionTime);
|
267 | 299 | }
|
| 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())); |
268 | 308 | }
|
269 | 309 |
|
270 | 310 | @Override
|
|
0 commit comments