|
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 | 49 | */
|
| 50 | + @Deprecated |
50 | 51 | <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime);
|
51 | 52 |
|
| 53 | + @Deprecated |
52 | 54 | <T> void schedule(SchedulableInstance<T> schedulableInstance);
|
53 | 55 |
|
| 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 | + |
54 | 76 | /**
|
55 | 77 | * Update an existing execution to a new execution-time. If the execution does not exist or if it
|
56 | 78 | * is currently running, an exception is thrown.
|
@@ -256,11 +278,25 @@ class StandardSchedulerClient implements SchedulerClient {
|
256 | 278 |
|
257 | 279 | @Override
|
258 | 280 | 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) { |
259 | 287 | boolean success =
|
260 | 288 | taskRepository.createIfNotExists(SchedulableInstance.of(taskInstance, executionTime));
|
261 | 289 | if (success) {
|
262 | 290 | notifyListeners(ClientEvent.EventType.SCHEDULE, taskInstance, executionTime);
|
263 | 291 | }
|
| 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())); |
264 | 300 | }
|
265 | 301 |
|
266 | 302 | @Override
|
|
0 commit comments