Skip to content

Commit

Permalink
feat: set default priority
Browse files Browse the repository at this point in the history
  • Loading branch information
geirsagberg committed Jan 20, 2025
1 parent 5744bd5 commit 3b64eb2
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 21 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ Set the priority per instance using the `TaskInstance.Builder`:
.scheduledTo(Instant.now()));
```

You can also set the default priority for all tasks of a given type:

```java
Tasks.recurring("my-task", FixedDelay.ofSeconds(5))
.setDefaultPriority(Priority.LOW)
.execute(...);
```

**Note:**
* When enabling this feature, make sure you have the new necessary indexes defined. If you
regularly have a state with large amounts of executions both due and future, it might be beneficial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,32 @@ public abstract class AbstractTask<T> implements Task<T> {
private final DeadExecutionHandler<T> deadExecutionHandler;
private final Class<T> dataClass;

private final int defaultPriority;

public AbstractTask(
String name,
Class<T> dataClass,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler) {
this(name, dataClass, failureHandler, deadExecutionHandler, DEFAULT_PRIORITY);
}

public AbstractTask(
String name,
Class<T> dataClass,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler,
int defaultPriority) {
this.name = name;
this.dataClass = dataClass;
this.failureHandler = failureHandler;
this.deadExecutionHandler = deadExecutionHandler;
this.defaultPriority = defaultPriority;
}

@Override
public int getDefaultPriority() {
return defaultPriority;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ default String getTaskName() {
}

default int getDefaultPriority() {
return Priority.MEDIUM;
return DEFAULT_PRIORITY;
}

int DEFAULT_PRIORITY = Priority.MEDIUM;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public abstract class CustomTask<T> extends AbstractTask<T> implements OnStartup {
private final NextExecutionTime defaultExecutionTime;
private ScheduleOnStartup<T> scheduleOnStartup;
private final ScheduleOnStartup<T> scheduleOnStartup;

public CustomTask(
String name,
Expand All @@ -36,7 +36,25 @@ public CustomTask(
Function<Instant, Instant> defaultExecutionTime,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler) {
super(name, dataClass, failureHandler, deadExecutionHandler);
this(
name,
dataClass,
scheduleOnStartup,
defaultExecutionTime,
failureHandler,
deadExecutionHandler,
DEFAULT_PRIORITY);
}

public CustomTask(
String name,
Class<T> dataClass,
ScheduleOnStartup<T> scheduleOnStartup,
Function<Instant, Instant> defaultExecutionTime,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler,
int defaultPriority) {
super(name, dataClass, failureHandler, deadExecutionHandler, defaultPriority);
this.scheduleOnStartup = scheduleOnStartup;
this.defaultExecutionTime = NextExecutionTime.from(defaultExecutionTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ public OneTimeTask(
Class<T> dataClass,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler) {
super(name, dataClass, failureHandler, deadExecutionHandler);
this(name, dataClass, failureHandler, deadExecutionHandler, DEFAULT_PRIORITY);
}

public OneTimeTask(
String name,
Class<T> dataClass,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler,
int defaultPriority) {
super(name, dataClass, failureHandler, deadExecutionHandler, defaultPriority);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class RecurringTask<T> extends AbstractTask<T> implements OnStar
private final OnCompleteReschedule<T> onComplete;
private final Schedule schedule;
private final ScheduleOnStartup<T> scheduleOnStartup;
public static final int DEFAULT_PRIORITY = Priority.HIGH;

public RecurringTask(String name, Schedule schedule, Class<T> dataClass) {
this(
Expand Down Expand Up @@ -65,17 +66,30 @@ public RecurringTask(
ScheduleRecurringOnStartup<T> scheduleOnStartup,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler) {
super(name, dataClass, failureHandler, deadExecutionHandler);
this(
name,
schedule,
dataClass,
scheduleOnStartup,
failureHandler,
deadExecutionHandler,
DEFAULT_PRIORITY);
}

public RecurringTask(
String name,
Schedule schedule,
Class<T> dataClass,
ScheduleRecurringOnStartup<T> scheduleOnStartup,
FailureHandler<T> failureHandler,
DeadExecutionHandler<T> deadExecutionHandler,
int defaultPriority) {
super(name, dataClass, failureHandler, deadExecutionHandler, defaultPriority);
onComplete = new OnCompleteReschedule<>(schedule);
this.schedule = schedule;
this.scheduleOnStartup = scheduleOnStartup;
}

@Override
public int getDefaultPriority() {
return Priority.HIGH;
}

@Override
public SchedulableInstance<T> schedulableInstance(String id) {
return new SchedulableTaskInstance<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@
public abstract class RecurringTaskWithPersistentSchedule<T extends ScheduleAndData>
extends AbstractTask<T> {

public static final int DEFAULT_PRIORITY = RecurringTask.DEFAULT_PRIORITY;

public RecurringTaskWithPersistentSchedule(String name, Class<T> dataClass) {
this(name, dataClass, new FailureHandler.OnFailureRescheduleUsingTaskDataSchedule<>());
}

public RecurringTaskWithPersistentSchedule(
String name, Class<T> dataClass, FailureHandler<T> onFailure) {
super(name, dataClass, onFailure, new DeadExecutionHandler.ReviveDeadExecution<>());
this(name, dataClass, onFailure, DEFAULT_PRIORITY);
}

public RecurringTaskWithPersistentSchedule(
String name, Class<T> dataClass, FailureHandler<T> onFailure, int defaultPriority) {
super(
name,
dataClass,
onFailure,
new DeadExecutionHandler.ReviveDeadExecution<>(),
defaultPriority);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static class RecurringTaskBuilder<T> {
private FailureHandler<T> onFailure;
private DeadExecutionHandler<T> onDeadExecution;
private ScheduleRecurringOnStartup<T> scheduleOnStartup;
private int defaultPriority = RecurringTask.DEFAULT_PRIORITY;

public RecurringTaskBuilder(String name, Schedule schedule, Class<T> dataClass) {
this.name = name;
Expand Down Expand Up @@ -116,6 +117,11 @@ public RecurringTaskBuilder<T> initialData(T initialData) {
return this;
}

public RecurringTaskBuilder<T> setDefaultPriority(int priority) {
this.defaultPriority = priority;
return this;
}

/**
* Disable 'scheduleOnStartup' to get control over when and show the executions is scheduled.
* Schedules will not be updated etc, so not really recommended.
Expand All @@ -132,8 +138,14 @@ public void apply(SchedulerClient scheduler, Clock clock, Task<T> task) {
}

public RecurringTask<T> execute(VoidExecutionHandler<T> executionHandler) {
return new RecurringTask<T>(
name, schedule, dataClass, scheduleOnStartup, onFailure, onDeadExecution) {
return new RecurringTask<>(
name,
schedule,
dataClass,
scheduleOnStartup,
onFailure,
onDeadExecution,
defaultPriority) {

@Override
public void executeRecurringly(
Expand All @@ -144,8 +156,14 @@ public void executeRecurringly(
}

public RecurringTask<T> executeStateful(StateReturningExecutionHandler<T> executionHandler) {
return new RecurringTask<T>(
name, schedule, dataClass, scheduleOnStartup, onFailure, onDeadExecution) {
return new RecurringTask<>(
name,
schedule,
dataClass,
scheduleOnStartup,
onFailure,
onDeadExecution,
defaultPriority) {

@Override
public CompletionHandler<T> execute(
Expand All @@ -169,11 +187,18 @@ public static class RecurringTaskWithPersistentScheduleBuilder<T extends Schedul
private FailureHandler<T> onFailure =
new FailureHandler.OnFailureRescheduleUsingTaskDataSchedule<>();

private int defaultPriority = RecurringTaskWithPersistentSchedule.DEFAULT_PRIORITY;

public RecurringTaskWithPersistentScheduleBuilder(String name, Class<T> dataClass) {
this.name = name;
this.dataClass = dataClass;
}

public RecurringTaskWithPersistentScheduleBuilder<T> setDefaultPriority(int priority) {
this.defaultPriority = priority;
return this;
}

public RecurringTaskWithPersistentScheduleBuilder<T> onFailure(
FailureHandler<T> failureHandler) {
this.onFailure = failureHandler;
Expand All @@ -182,7 +207,8 @@ public RecurringTaskWithPersistentScheduleBuilder<T> onFailure(

public RecurringTaskWithPersistentSchedule<T> execute(
VoidExecutionHandler<T> executionHandler) {
return new RecurringTaskWithPersistentSchedule<T>(name, dataClass, onFailure) {
return new RecurringTaskWithPersistentSchedule<>(
name, dataClass, onFailure, defaultPriority) {
@Override
public CompletionHandler<T> execute(
TaskInstance<T> taskInstance, ExecutionContext executionContext) {
Expand All @@ -199,7 +225,8 @@ public CompletionHandler<T> execute(

public RecurringTaskWithPersistentSchedule<T> executeStateful(
StateReturningExecutionHandler<T> executionHandler) {
return new RecurringTaskWithPersistentSchedule<T>(name, dataClass, onFailure) {
return new RecurringTaskWithPersistentSchedule<>(
name, dataClass, onFailure, defaultPriority) {

@Override
public CompletionHandler<T> execute(
Expand All @@ -222,6 +249,7 @@ public static class OneTimeTaskBuilder<T> {
private final Class<T> dataClass;
private FailureHandler<T> onFailure;
private DeadExecutionHandler<T> onDeadExecution;
private int defaultPriority = OneTimeTask.DEFAULT_PRIORITY;

public OneTimeTaskBuilder(String name, Class<T> dataClass) {
this.name = name;
Expand Down Expand Up @@ -250,8 +278,13 @@ public OneTimeTaskBuilder<T> onDeadExecution(DeadExecutionHandler<T> deadExecuti
return this;
}

public OneTimeTaskBuilder<T> setDefaultPriority(int priority) {
this.defaultPriority = priority;
return this;
}

public OneTimeTask<T> execute(VoidExecutionHandler<T> executionHandler) {
return new OneTimeTask<T>(name, dataClass, onFailure, onDeadExecution) {
return new OneTimeTask<>(name, dataClass, onFailure, onDeadExecution, defaultPriority) {
@Override
public void executeOnce(TaskInstance<T> taskInstance, ExecutionContext executionContext) {
executionHandler.execute(taskInstance, executionContext);
Expand All @@ -267,6 +300,7 @@ public static class TaskBuilder<T> {
private DeadExecutionHandler<T> onDeadExecution;
private ScheduleOnStartup<T> onStartup;
private Function<Instant, Instant> defaultExecutionTime = Function.identity();
private int defaultPriority = CustomTask.DEFAULT_PRIORITY;

public TaskBuilder(String name, Class<T> dataClass) {
this.name = name;
Expand Down Expand Up @@ -295,6 +329,11 @@ public TaskBuilder<T> onDeadExecution(DeadExecutionHandler<T> deadExecutionHandl
return this;
}

public TaskBuilder<T> setDefaultPriority(int priority) {
this.defaultPriority = priority;
return this;
}

public TaskBuilder<T> scheduleOnStartup(
String instance, T initialData, Function<Instant, Instant> firstExecutionTime) {
this.onStartup = new ScheduleOnceOnStartup<T>(instance, initialData, firstExecutionTime);
Expand All @@ -316,8 +355,14 @@ public TaskBuilder<T> defaultExecutionTime(Function<Instant, Instant> defaultExe
}

public CustomTask<T> execute(ExecutionHandler<T> executionHandler) {
return new CustomTask<T>(
name, dataClass, onStartup, defaultExecutionTime, onFailure, onDeadExecution) {
return new CustomTask<>(
name,
dataClass,
onStartup,
defaultExecutionTime,
onFailure,
onDeadExecution,
defaultPriority) {
@Override
public CompletionHandler<T> execute(
TaskInstance<T> taskInstance, ExecutionContext executionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class RecurringTaskTest {
public class RecurringTaskScheduleTest {

public static final ZoneId ZONE = ZoneId.systemDefault();
private static final LocalDate DATE = LocalDate.of(2018, 3, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.kagkarlsson.scheduler.task.helper;

import static org.junit.jupiter.api.Assertions.*;

import com.github.kagkarlsson.scheduler.task.Priority;
import org.junit.jupiter.api.Test;

class CustomTaskTest {

@Test
public void should_have_default_priority() {
CustomTask<Void> customTask =
Tasks.custom("name", Void.class).execute((taskInstance, executionContext) -> null);

assertEquals(CustomTask.DEFAULT_PRIORITY, customTask.getDefaultPriority());
}

@Test
public void should_override_default_priority() {
CustomTask<Void> customTask =
Tasks.custom("name", Void.class)
.setDefaultPriority(Priority.LOW)
.execute((taskInstance, executionContext) -> null);

assertEquals(Priority.LOW, customTask.getDefaultPriority());
}
}
Loading

0 comments on commit 3b64eb2

Please sign in to comment.