forked from kagkarlsson/db-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchedulerClient.java
626 lines (542 loc) · 22.9 KB
/
SchedulerClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* Copyright (C) Gustav Karlsson
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.kagkarlsson.scheduler;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import com.github.kagkarlsson.scheduler.SchedulerClient.ScheduleOptions.WhenExists;
import com.github.kagkarlsson.scheduler.event.SchedulerListeners;
import com.github.kagkarlsson.scheduler.exceptions.TaskInstanceCurrentlyExecutingException;
import com.github.kagkarlsson.scheduler.exceptions.TaskInstanceNotFoundException;
import com.github.kagkarlsson.scheduler.jdbc.AutodetectJdbcCustomization;
import com.github.kagkarlsson.scheduler.jdbc.JdbcCustomization;
import com.github.kagkarlsson.scheduler.jdbc.JdbcTaskRepository;
import com.github.kagkarlsson.scheduler.serializer.Serializer;
import com.github.kagkarlsson.scheduler.stats.StatsRegistry;
import com.github.kagkarlsson.scheduler.task.Execution;
import com.github.kagkarlsson.scheduler.task.SchedulableInstance;
import com.github.kagkarlsson.scheduler.task.ScheduledTaskInstance;
import com.github.kagkarlsson.scheduler.task.Task;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.github.kagkarlsson.scheduler.task.TaskInstanceId;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public interface SchedulerClient {
int DEFAULT_BATCH_SIZE = 100;
/**
* Schedule a new execution for the given task instance.
*
* <p>If the task instance already exists, the specified policy in <code>scheduleOptions</code>
* applies.
*
* <p>An exception is thrown if the execution is currently running.
*
* @param taskInstance Task-instance, optionally with data
* @param executionTime Instant it should run
* @param scheduleOptions policy for when the instance exists
* @return true if the task-instance actually was scheduled
* @throws TaskInstanceCurrentlyExecutingException if the execution is currently running
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstance
* @see com.github.kagkarlsson.scheduler.exceptions.TaskInstanceCurrentlyExecutingException
*/
<T> boolean schedule(
TaskInstance<T> taskInstance, Instant executionTime, ScheduleOptions scheduleOptions);
/**
* Schedule a new execution for the given task instance.
*
* <p>If the task instance already exists, the specified policy in <code>scheduleOptions</code>
* applies.
*
* <p>An exception is thrown if the execution is currently running.
*
* @param schedulableInstance Task-instance and time it should run
* @param scheduleOptions policy for when the instance exists
* @return true if the task-instance actually was scheduled
* @throws TaskInstanceCurrentlyExecutingException if the execution is currently running
* @see com.github.kagkarlsson.scheduler.task.SchedulableInstance
* @see com.github.kagkarlsson.scheduler.exceptions.TaskInstanceCurrentlyExecutingException
*/
<T> boolean schedule(SchedulableInstance<T> schedulableInstance, ScheduleOptions scheduleOptions);
/**
* 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
<T> void schedule(TaskInstance<T> taskInstance, Instant executionTime);
/**
* @deprecated use {@link #scheduleIfNotExists(SchedulableInstance)} instead.
*/
@Deprecated
<T> void schedule(SchedulableInstance<T> 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
*/
<T> boolean scheduleIfNotExists(TaskInstance<T> 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
*/
<T> boolean scheduleIfNotExists(SchedulableInstance<T> schedulableInstance);
/**
* Schedule a batch of executions. If any of the executions already exists, the scheduling will
* fail and an exception will be thrown.
*
* @param taskInstances Task-instance to schedule, optionally with data
* @param executionTime Instant it should run
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstance
*/
void scheduleBatch(List<TaskInstance<?>> taskInstances, Instant executionTime);
/**
* Schedule a batch of executions. If any of the executions already exists, the scheduling will
* fail and an exception will be thrown.
*
* @param schedulableInstances Task-instances with individual execution-times
* @see com.github.kagkarlsson.scheduler.task.SchedulableInstance
*/
void scheduleBatch(List<SchedulableInstance<?>> schedulableInstances);
/**
* Schedule a batch of executions. If any of the executions already exists, the scheduling will
* fail and an exception will be thrown.
*
* @param taskInstances Task-instances to schedule, optionally with data
* @param executionTime Instant it should run
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstance
*/
default void scheduleBatch(Stream<TaskInstance<?>> taskInstances, Instant executionTime) {
StreamUtils.chunkStream(taskInstances, DEFAULT_BATCH_SIZE)
.forEach(chunk -> scheduleBatch(chunk, executionTime));
}
/**
* Schedule a batch of executions. If any of the executions already exists, the scheduling will
* fail and an exception will be thrown.
*
* @param schedulableInstances Task-instances with individual execution-times
* @see com.github.kagkarlsson.scheduler.task.SchedulableInstance
*/
default void scheduleBatch(Stream<SchedulableInstance<?>> schedulableInstances) {
StreamUtils.chunkStream(schedulableInstances, DEFAULT_BATCH_SIZE).forEach(this::scheduleBatch);
}
/**
* 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.
*
* @param taskInstanceId Task-instance to reschedule, expected to exist
* @param newExecutionTime the new execution-time
* @return true if rescheduled successfully
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstanceId
*/
boolean reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime);
/**
* Update an existing execution with a new execution-time and new task-data. If the execution does
* not exist or if it is currently running, an exception is thrown.
*
* @param taskInstanceId
* @param newExecutionTime the new execution-time
* @param newData the new task-data
* @return true if rescheduled successfully
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstanceId
*/
<T> boolean reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime, T newData);
/**
* Update an existing execution with a new execution-time and new task-data. If the execution does
* not exist or if it is currently running, an exception is thrown.
*
* @param schedulableInstance the updated instance
* @return true if rescheduled successfully
*/
<T> boolean reschedule(SchedulableInstance<T> schedulableInstance);
/**
* Removes/Cancels an execution.
*
* @param taskInstanceId
* @see com.github.kagkarlsson.scheduler.task.TaskInstanceId
*/
void cancel(TaskInstanceId taskInstanceId);
/**
* Gets all scheduled executions and supplies them to the provided Consumer. A Consumer is used to
* avoid forcing the SchedulerClient to load all executions in memory. Currently running
* executions are not returned.
*
* @param consumer Consumer for the executions
*/
void fetchScheduledExecutions(Consumer<ScheduledExecution<Object>> consumer);
void fetchScheduledExecutions(
ScheduledExecutionsFilter filter, Consumer<ScheduledExecution<Object>> consumer);
/**
* @see #fetchScheduledExecutions(Consumer)
*/
default List<ScheduledExecution<Object>> getScheduledExecutions() {
List<ScheduledExecution<Object>> executions = new ArrayList<>();
fetchScheduledExecutions(executions::add);
return executions;
}
/**
* @see #fetchScheduledExecutions(Consumer)
*/
default List<ScheduledExecution<Object>> getScheduledExecutions(
ScheduledExecutionsFilter filter) {
List<ScheduledExecution<Object>> executions = new ArrayList<>();
fetchScheduledExecutions(filter, executions::add);
return executions;
}
/**
* Gets all scheduled executions for a task and supplies them to the provided Consumer. A Consumer
* is used to avoid forcing the SchedulerClient to load all executions in memory. Currently
* running executions are not returned.
*
* @param taskName the name of the task to get scheduled-executions for
* @param dataClass the task data-class the data will be serialized and cast to
* @param consumer Consumer for the executions
*/
<T> void fetchScheduledExecutionsForTask(
String taskName, Class<T> dataClass, Consumer<ScheduledExecution<T>> consumer);
<T> void fetchScheduledExecutionsForTask(
String taskName,
Class<T> dataClass,
ScheduledExecutionsFilter filter,
Consumer<ScheduledExecution<T>> consumer);
/**
* @see #fetchScheduledExecutionsForTask(String, Class, Consumer)
*/
default <T> List<ScheduledExecution<Object>> getScheduledExecutionsForTask(String taskName) {
List<ScheduledExecution<Object>> executions = new ArrayList<>();
fetchScheduledExecutionsForTask(taskName, Object.class, executions::add);
return executions;
}
/**
* @see #fetchScheduledExecutionsForTask(String, Class, Consumer)
*/
default <T> List<ScheduledExecution<T>> getScheduledExecutionsForTask(
String taskName, Class<T> dataClass) {
List<ScheduledExecution<T>> executions = new ArrayList<>();
fetchScheduledExecutionsForTask(taskName, dataClass, executions::add);
return executions;
}
/**
* @see #fetchScheduledExecutionsForTask(String, Class, Consumer)
*/
default <T> List<ScheduledExecution<T>> getScheduledExecutionsForTask(
String taskName, Class<T> dataClass, ScheduledExecutionsFilter filter) {
List<ScheduledExecution<T>> executions = new ArrayList<>();
fetchScheduledExecutionsForTask(taskName, dataClass, filter, executions::add);
return executions;
}
/**
* Gets the details for a specific scheduled execution. Currently running executions are also
* returned.
*
* @param taskInstanceId
* @return Optional.empty() if no matching execution found
* @see com.github.kagkarlsson.scheduler.task.TaskInstanceId
* @see com.github.kagkarlsson.scheduler.ScheduledExecution
*/
Optional<ScheduledExecution<Object>> getScheduledExecution(TaskInstanceId taskInstanceId);
class ScheduleOptions {
public static final ScheduleOptions WHEN_EXISTS_DO_NOTHING =
defaultOptions().whenExistsDoNothing();
public static final ScheduleOptions WHEN_EXISTS_RESCHEDULE =
defaultOptions().whenExistsReschedule();
public enum WhenExists {
RESCHEDULE,
DO_NOTHING;
}
private WhenExists whenExists;
public static ScheduleOptions defaultOptions() {
return new ScheduleOptions();
}
public ScheduleOptions whenExistsReschedule() {
this.whenExists = WhenExists.RESCHEDULE;
return this;
}
public ScheduleOptions whenExistsDoNothing() {
this.whenExists = WhenExists.DO_NOTHING;
return this;
}
public WhenExists getWhenExists() {
return whenExists;
}
}
class Builder {
private final DataSource dataSource;
private List<Task<?>> knownTasks;
private Serializer serializer = Serializer.DEFAULT_JAVA_SERIALIZER;
private String tableName = JdbcTaskRepository.DEFAULT_TABLE_NAME;
private JdbcCustomization jdbcCustomization;
private boolean priority = false;
private Builder(DataSource dataSource, List<Task<?>> knownTasks) {
this.dataSource = dataSource;
this.knownTasks = knownTasks;
}
public static Builder create(DataSource dataSource, Task<?>... knownTasks) {
return new Builder(dataSource, Arrays.asList(knownTasks));
}
public static Builder create(DataSource dataSource, List<Task<?>> knownTasks) {
return new Builder(dataSource, knownTasks);
}
public Builder serializer(Serializer serializer) {
this.serializer = serializer;
return this;
}
public Builder tableName(String tableName) {
this.tableName = tableName;
return this;
}
/** Will cause getScheduledExecutions(..) to return executions in priority order. */
public Builder enablePriority() {
this.priority = true;
return this;
}
public Builder jdbcCustomization(JdbcCustomization jdbcCustomization) {
this.jdbcCustomization = jdbcCustomization;
return this;
}
public SchedulerClient build() {
TaskResolver taskResolver = new TaskResolver(StatsRegistry.NOOP, knownTasks);
final SystemClock clock = new SystemClock();
final JdbcCustomization jdbcCustomization =
ofNullable(this.jdbcCustomization)
.orElseGet(() -> new AutodetectJdbcCustomization(dataSource));
TaskRepository taskRepository =
new JdbcTaskRepository(
dataSource,
false,
jdbcCustomization,
tableName,
taskResolver,
new SchedulerClientName(),
serializer,
priority,
clock);
return new StandardSchedulerClient(taskRepository, clock);
}
}
class StandardSchedulerClient implements SchedulerClient {
private static final Logger LOG = LoggerFactory.getLogger(StandardSchedulerClient.class);
protected final TaskRepository taskRepository;
private final Clock clock;
private final SchedulerListeners schedulerListeners;
StandardSchedulerClient(TaskRepository taskRepository, Clock clock) {
this(taskRepository, SchedulerListeners.NOOP, clock);
}
StandardSchedulerClient(
TaskRepository taskRepository, SchedulerListeners schedulerListeners, Clock clock) {
this.taskRepository = taskRepository;
this.schedulerListeners = schedulerListeners;
this.clock = clock;
}
@Override
public <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime) {
// ignore result even if failed to schedule due to duplicates for backwards-compatibility
scheduleIfNotExists(taskInstance, executionTime);
}
@Override
public <T> boolean scheduleIfNotExists(TaskInstance<T> taskInstance, Instant executionTime) {
boolean success =
taskRepository.createIfNotExists(SchedulableInstance.of(taskInstance, executionTime));
if (success) {
schedulerListeners.onExecutionScheduled(taskInstance, executionTime);
}
return success;
}
@Override
public <T> boolean scheduleIfNotExists(SchedulableInstance<T> schedulableInstance) {
return scheduleIfNotExists(
schedulableInstance.getTaskInstance(),
schedulableInstance.getNextExecutionTime(clock.now()));
}
@Override
public void scheduleBatch(List<TaskInstance<?>> taskInstances, Instant executionTime) {
List<ScheduledTaskInstance> batchToSchedule =
taskInstances.stream()
.map(taskInstance -> new ScheduledTaskInstance(taskInstance, executionTime))
.collect(toList());
taskRepository.createBatch(batchToSchedule);
notifyListenersOfScheduledBatch(batchToSchedule);
}
@Override
public void scheduleBatch(List<SchedulableInstance<?>> schedulableInstances) {
List<ScheduledTaskInstance> batchToSchedule =
schedulableInstances.stream()
.map(schedulable -> ScheduledTaskInstance.fixExecutionTime(schedulable, clock))
.collect(toList());
taskRepository.createBatch(batchToSchedule);
notifyListenersOfScheduledBatch(batchToSchedule);
}
private void notifyListenersOfScheduledBatch(List<ScheduledTaskInstance> batchToSchedule) {
batchToSchedule.forEach(
instance ->
schedulerListeners.onExecutionScheduled(instance, instance.getExecutionTime()));
}
@Override
public <T> void schedule(SchedulableInstance<T> schedulableInstance) {
schedule(
schedulableInstance.getTaskInstance(),
schedulableInstance.getNextExecutionTime(clock.now()));
}
@Override
public <T> boolean schedule(
TaskInstance<T> taskInstance, Instant executionTime, ScheduleOptions scheduleOptions) {
boolean successfulSchedule = scheduleIfNotExists(taskInstance, executionTime);
if (successfulSchedule) {
return true;
}
WhenExists whenExists = scheduleOptions.getWhenExists();
if (whenExists == WhenExists.DO_NOTHING) {
// failed to schedule, but user has chosen to ignore
LOG.debug("Task instance already exists. Keeping existing. task-instance={}", taskInstance);
return false;
} else if (whenExists == WhenExists.RESCHEDULE) {
// successfulSchedule = false, whenExists = RESCHEDULE
// i.e. failed to schedule because it already exists -> try reschedule
var existing = getScheduledExecution(taskInstance);
if (existing.isEmpty()) {
// something must have processed it and deleted it
LOG.warn(
"Task-instance should already exist, but failed to find it. "
+ "It must have been processed and deleted. task-instance={}",
taskInstance);
return false;
}
LOG.debug("Task instance already exists. Rescheduling. task-instance={}", taskInstance);
return reschedule(taskInstance, executionTime, taskInstance.getData());
} else {
throw new IllegalArgumentException("Unknown WhenExists value: " + whenExists);
}
}
@Override
public <T> boolean schedule(
SchedulableInstance<T> schedulableInstance, ScheduleOptions whenExists) {
return schedule(
schedulableInstance.getTaskInstance(),
schedulableInstance.getNextExecutionTime(clock.now()),
whenExists);
}
@Override
public boolean reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime) {
return reschedule(taskInstanceId, newExecutionTime, null);
}
@Override
public <T> boolean reschedule(SchedulableInstance<T> schedulableInstance) {
return reschedule(
schedulableInstance,
schedulableInstance.getNextExecutionTime(clock.now()),
schedulableInstance.getTaskInstance().getData());
}
@Override
public <T> boolean reschedule(
TaskInstanceId taskInstanceId, Instant newExecutionTime, T newData) {
String taskName = taskInstanceId.getTaskName();
String instanceId = taskInstanceId.getId();
Execution execution =
taskRepository
.getExecution(taskName, instanceId)
.orElseThrow(() -> new TaskInstanceNotFoundException(taskName, instanceId));
if (execution.isPicked()) {
throw new TaskInstanceCurrentlyExecutingException(taskName, instanceId);
}
boolean success;
if (newData == null) {
success = taskRepository.reschedule(execution, newExecutionTime, null, null, 0);
} else {
success = taskRepository.reschedule(execution, newExecutionTime, newData, null, null, 0);
}
if (success) {
schedulerListeners.onExecutionScheduled(taskInstanceId, newExecutionTime);
} else {
LOG.warn("Failed to reschedule task instance: {}", taskInstanceId);
}
return success;
}
@Override
public void cancel(TaskInstanceId taskInstanceId) {
String taskName = taskInstanceId.getTaskName();
String instanceId = taskInstanceId.getId();
Optional<Execution> execution = taskRepository.getExecution(taskName, instanceId);
if (execution.isPresent()) {
if (execution.get().isPicked()) {
throw new TaskInstanceCurrentlyExecutingException(taskName, instanceId);
}
taskRepository.remove(execution.get());
} else {
throw new TaskInstanceNotFoundException(taskName, instanceId);
}
}
@Override
public void fetchScheduledExecutions(Consumer<ScheduledExecution<Object>> consumer) {
fetchScheduledExecutions(ScheduledExecutionsFilter.all().withPicked(false), consumer);
}
@Override
public void fetchScheduledExecutions(
ScheduledExecutionsFilter filter, Consumer<ScheduledExecution<Object>> consumer) {
taskRepository.getScheduledExecutions(
filter, execution -> consumer.accept(new ScheduledExecution<>(Object.class, execution)));
}
@Override
public <T> void fetchScheduledExecutionsForTask(
String taskName, Class<T> dataClass, Consumer<ScheduledExecution<T>> consumer) {
fetchScheduledExecutionsForTask(
taskName, dataClass, ScheduledExecutionsFilter.all().withPicked(false), consumer);
}
@Override
public <T> void fetchScheduledExecutionsForTask(
String taskName,
Class<T> dataClass,
ScheduledExecutionsFilter filter,
Consumer<ScheduledExecution<T>> consumer) {
taskRepository.getScheduledExecutions(
filter,
taskName,
execution -> consumer.accept(new ScheduledExecution<>(dataClass, execution)));
}
@Override
public Optional<ScheduledExecution<Object>> getScheduledExecution(
TaskInstanceId taskInstanceId) {
Optional<Execution> e =
taskRepository.getExecution(taskInstanceId.getTaskName(), taskInstanceId.getId());
return e.map(oe -> new ScheduledExecution<>(Object.class, oe));
}
}
class SchedulerClientName implements SchedulerName {
@Override
public String getName() {
return "SchedulerClient";
}
}
}