Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to customize task repository #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public Scheduler scheduler(DbSchedulerCustomizer customizer) {
// Use custom executor service if provided
customizer.executorService().ifPresent(builder::executorService);

// Use custom task repository if provided
customizer.taskRepository().ifPresent(builder::taskRepository);

builder.deleteUnresolvedAfter(config.getDeleteUnresolvedAfter());

// Add recurring jobs and jobs that implements OnStartup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.github.kagkarlsson.scheduler.SchedulerName;
import com.github.kagkarlsson.scheduler.Serializer;
import com.github.kagkarlsson.scheduler.TaskRepository;
import com.github.kagkarlsson.scheduler.TaskResolver;
import java.util.Optional;
import java.util.concurrent.ExecutorService;

Expand Down Expand Up @@ -45,4 +47,12 @@ default Optional<Serializer> serializer() {
default Optional<ExecutorService> executorService() {
return Optional.empty();
}

/**
* Provide a custom {@link TaskRepository} implementation.
*/
default Optional<TaskRepository> taskRepository() {
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class SchedulerBuilder {
protected boolean enableImmediateExecution = false;
protected ExecutorService executorService;
protected Duration deleteUnresolvedAfter = Duration.ofDays(14);
protected TaskRepository taskRepository;

public SchedulerBuilder(DataSource dataSource, List<Task<?>> knownTasks) {
this.dataSource = dataSource;
Expand Down Expand Up @@ -138,12 +139,17 @@ public SchedulerBuilder deleteUnresolvedAfter(Duration deleteAfter) {
return this;
}

public SchedulerBuilder taskRepository(TaskRepository taskRepository) {
this.taskRepository = taskRepository;
return this;
}

public Scheduler build() {
if (pollingLimit < executorThreads) {
LOG.warn("Polling-limit is less than number of threads. Should be equal or higher.");
}
final TaskResolver taskResolver = new TaskResolver(statsRegistry, clock, knownTasks);
final JdbcTaskRepository taskRepository = new JdbcTaskRepository(dataSource, tableName, taskResolver, schedulerName, serializer);
final TaskRepository taskRepository = buildTaskRepository(taskResolver);

ExecutorService candidateExecutorService = executorService;
if (candidateExecutorService == null) {
Expand All @@ -161,4 +167,12 @@ public Scheduler build() {
schedulerName, waiter, heartbeatInterval, enableImmediateExecution, statsRegistry, pollingLimit,
deleteUnresolvedAfter, startTasks);
}

private TaskRepository buildTaskRepository(TaskResolver taskResolver) {
if (taskRepository != null) {
return taskRepository;
}

return new JdbcTaskRepository(dataSource, tableName, taskResolver, schedulerName, serializer);
}
}