forked from kagkarlsson/db-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchedulerClient.java
348 lines (298 loc) · 14.4 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
/**
* Copyright (C) Gustav Karlsson
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.github.kagkarlsson.scheduler.exceptions.TaskInstanceNotFoundException;
import com.github.kagkarlsson.scheduler.exceptions.TaskInstanceCurrentlyExecutingException;
import com.github.kagkarlsson.scheduler.jdbc.DefaultJdbcCustomization;
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.Task;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.github.kagkarlsson.scheduler.task.TaskInstanceId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
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 static java.util.Optional.ofNullable;
public interface SchedulerClient {
/**
* Schedule a new execution.
*
* @param taskInstance Task-instance, optionally with data
* @param executionTime Instant it should run
* @return void
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstance
*/
<T> void schedule(TaskInstance<T> taskInstance, Instant executionTime);
<T> void schedule(SchedulableInstance<T> schedulableInstance);
/**
* 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
* @param newExecutionTime the new execution-time
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstanceId
*/
void 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
* @see java.time.Instant
* @see com.github.kagkarlsson.scheduler.task.TaskInstanceId
*/
<T> void 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
*/
<T> void reschedule(SchedulableInstance<T> schedulableInstance);
/**
* Removes/Cancels an execution.
*
* @param taskInstanceId
* @return void
* @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
* @return void
*/
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
* @return void
*/
<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<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 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 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;
}
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();
TaskRepository taskRepository = new JdbcTaskRepository(
dataSource,
false,
ofNullable(jdbcCustomization).orElse(new DefaultJdbcCustomization()),
tableName,
taskResolver,
new SchedulerClientName(),
serializer,
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 SchedulerClientEventListener schedulerClientEventListener;
StandardSchedulerClient(TaskRepository taskRepository, Clock clock) {
this(taskRepository, SchedulerClientEventListener.NOOP, clock);
}
StandardSchedulerClient(TaskRepository taskRepository, SchedulerClientEventListener schedulerClientEventListener, Clock clock) {
this.taskRepository = taskRepository;
this.schedulerClientEventListener = schedulerClientEventListener;
this.clock = clock;
}
@Override
public <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime) {
boolean success = taskRepository.createIfNotExists(SchedulableInstance.of(taskInstance, executionTime));
if (success) {
notifyListeners(ClientEvent.EventType.SCHEDULE, taskInstance, executionTime);
}
}
@Override
public <T> void schedule(SchedulableInstance<T> schedulableInstance) {
schedule(schedulableInstance.getTaskInstance(), schedulableInstance.getNextExecutionTime(clock.now()));
}
@Override
public void reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime) {
reschedule(taskInstanceId, newExecutionTime, null);
}
@Override
public <T> void reschedule(SchedulableInstance<T> schedulableInstance) {
reschedule(schedulableInstance, schedulableInstance.getNextExecutionTime(clock.now()), schedulableInstance.getTaskInstance().getData());
}
@Override
public <T> void reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime, T newData) {
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);
}
boolean success;
if (newData == null) {
success = taskRepository.reschedule(execution.get(), newExecutionTime, null, null, 0);
} else {
success = taskRepository.reschedule(execution.get(), newExecutionTime, newData, null, null, 0);
}
if (success) {
notifyListeners(ClientEvent.EventType.RESCHEDULE, taskInstanceId, newExecutionTime);
}
} else {
throw new TaskInstanceNotFoundException(taskName, instanceId);
}
}
@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());
notifyListeners(ClientEvent.EventType.CANCEL, taskInstanceId, execution.get().executionTime);
} 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));
}
private void notifyListeners(ClientEvent.EventType eventType, TaskInstanceId taskInstanceId, Instant executionTime) {
try {
schedulerClientEventListener.newEvent(new ClientEvent(new ClientEvent.ClientEventContext(eventType, taskInstanceId, executionTime)));
} catch (Exception e) {
LOG.error("Error when notifying SchedulerClientEventListener.", e);
}
}
}
class SchedulerClientName implements SchedulerName {
@Override
public String getName() {
return "SchedulerClient";
}
}
}