Skip to content

Commit

Permalink
Refactor TaskDescription a bit. SchedulableInstance.Builder since we …
Browse files Browse the repository at this point in the history
…now have a TaskInstance.Builder.
  • Loading branch information
kagkarlsson committed Sep 27, 2024
1 parent fce9c49 commit 26e0492
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 23 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,32 @@ An instance of a _one-time_ task has a single execution-time some time in the fu
Define a _one-time_ task and start the scheduler:

```java
OneTimeTask<MyTaskData> myAdhocTask = Tasks.oneTime("my-typed-adhoc-task", MyTaskData.class)
TaskDescriptor<MyTaskData> MY_TASK =
TaskDescriptor.of("my-onetime-task", MyTaskData.class);

OneTimeTask<MyTaskData> myTaskImplementation =
Tasks.oneTime(MY_TASK)
.execute((inst, ctx) -> {
System.out.println("Executed! Custom data, Id: " + inst.getData().id);
System.out.println("Executed! Custom data, Id: " + inst.getData().id);
});

final Scheduler scheduler = Scheduler
.create(dataSource, myAdhocTask)
.registerShutdownHook()
.build();
.create(dataSource, myTaskImplementation)
.registerShutdownHook()
.build();

scheduler.start();

```

... and then at some point (at runtime), an execution is scheduled using the `SchedulerClient`:

```java
// Schedule the task for execution a certain time in the future and optionally provide custom data for the execution
scheduler.schedule(myAdhocTask.instance("1045", new MyTaskData(1001L)), Instant.now().plusSeconds(5));
scheduler.schedule(
MY_TASK
.instanceWithId("1045")
.data(new MyTaskData(1001L))
.scheduledTo(Instant.now().plusSeconds(5)));
```

### More examples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@
package com.github.kagkarlsson.scheduler.task;

import java.time.Instant;
import java.util.function.Supplier;

public interface SchedulableInstance<T> extends TaskInstanceId {

static <T> SchedulableInstance<T> of(TaskInstance<T> taskInstance, Instant executionTime) {
return new SchedulableTaskInstance<T>(taskInstance, executionTime);
}

static <T> SchedulableInstance<T> of(
TaskInstance<T> taskInstance, NextExecutionTime executionTime) {
return new SchedulableTaskInstance<T>(taskInstance, executionTime);
}

TaskInstance<T> getTaskInstance();

Instant getNextExecutionTime(Instant currentTime);
Expand All @@ -29,12 +39,36 @@ default String getId() {
return getTaskInstance().getId();
}

static <T> SchedulableInstance<T> of(TaskInstance<T> taskInstance, Instant executionTime) {
return new SchedulableTaskInstance<T>(taskInstance, executionTime);
}
class Builder<T> {

static <T> SchedulableInstance<T> of(
TaskInstance<T> taskInstance, NextExecutionTime executionTime) {
return new SchedulableTaskInstance<T>(taskInstance, executionTime);
private final String taskName;
private final String id;
private Supplier<T> dataSupplier = () -> (T) null;
private int priority = Priority.MEDIUM;

public Builder(String taskName, String id) {
this.id = id;
this.taskName = taskName;
}

public SchedulableInstance.Builder<T> data(Supplier<T> dataSupplier) {
this.dataSupplier = dataSupplier;
return this;
}

public SchedulableInstance.Builder<T> data(T data) {
this.dataSupplier = () -> (T) data;
return this;
}

public SchedulableInstance.Builder<T> priority(int priority) {
this.priority = priority;
return this;
}

public SchedulableInstance<T> scheduledTo(Instant executionTime) {
TaskInstance<T> taskInstance = new TaskInstance<>(taskName, id, dataSupplier, priority);
return new SchedulableTaskInstance<>(taskInstance, executionTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ public interface TaskDescriptor<T> extends HasTaskName {

Class<T> getDataClass();

static TaskDescriptor<Void> of(String name) {
return TaskDescriptor.of(name, Void.class);
}

static <T> TaskDescriptor<T> of(String name, Class<T> dataClass) {
return new TaskDescriptor.SimpleTaskDescriptor<T>(name, dataClass);
}

default SchedulableInstance.Builder<T> instanceWithId(String id) {
return new SchedulableInstance.Builder<>(getTaskName(), id);
}

class SimpleTaskDescriptor<T> implements TaskDescriptor<T> {

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.github.kagkarlsson.examples.helpers.Example;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.task.TaskWithoutDataDescriptor;
import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import java.time.Duration;
Expand All @@ -30,8 +31,11 @@ public static void main(String[] args) {
@Override
public void run(DataSource dataSource) {

TaskWithoutDataDescriptor DESCRIPTOR =
new TaskWithoutDataDescriptor("my_task");

OneTimeTask<Void> onetimeTask =
Tasks.oneTime("my_task")
Tasks.oneTime(DESCRIPTOR)
.execute(
(taskInstance, executionContext) -> {
System.out.println("Executed!");
Expand All @@ -48,8 +52,13 @@ public void run(DataSource dataSource) {

sleep(2000);
System.out.println("Scheduling task to executed immediately.");
scheduler.schedule(onetimeTask.instance("1"), Instant.now());
// scheduler.triggerCheckForDueExecutions(); // another option for triggering execution
// directly
scheduler.schedule(
DESCRIPTOR
.instanceWithId("1")
.scheduledTo(Instant.now()));

// scheduler.triggerCheckForDueExecutions();
// another option for triggering execution directly

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.kagkarlsson.examples.helpers.Example;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.task.FailureHandler;
import com.github.kagkarlsson.scheduler.task.TaskDescriptor;
import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import java.time.Instant;
Expand All @@ -31,8 +32,10 @@ public static void main(String[] args) {

@Override
public void run(DataSource dataSource) {
TaskDescriptor<Void> TASK = TaskDescriptor.of("exponential_backoff_task", Void.class);

OneTimeTask<Void> failingTask =
Tasks.oneTime("exponential_backoff_task")
Tasks.oneTime(TASK)
.onFailure(new FailureHandler.ExponentialBackoffFailureHandler<>(ofSeconds(1)))
.execute(
(taskInstance, executionContext) -> {
Expand All @@ -45,7 +48,7 @@ public void run(DataSource dataSource) {
.registerShutdownHook()
.build();

scheduler.schedule(failingTask.instance("1"), Instant.now());
scheduler.schedule(TASK.instanceWithId("1").scheduledTo(Instant.now()));

scheduler.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.github.kagkarlsson.examples.helpers.Example;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.task.TaskDescriptor;
import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import java.io.Serializable;
Expand All @@ -30,21 +31,27 @@ public static void main(String[] args) {
@Override
public void run(DataSource dataSource) {

OneTimeTask<MyTaskData> myAdhocTask =
Tasks.oneTime("my-typed-adhoc-task", MyTaskData.class)
TaskDescriptor<MyTaskData> MY_TASK = TaskDescriptor.of("my-onetime-task", MyTaskData.class);

OneTimeTask<MyTaskData> taskImplementation =
Tasks.oneTime(MY_TASK)
.execute(
(inst, ctx) -> {
System.out.println("Executed! Custom data, Id: " + inst.getData().id);
});

final Scheduler scheduler = Scheduler.create(dataSource, myAdhocTask).threads(5).build();
final Scheduler scheduler =
Scheduler.create(dataSource, taskImplementation).registerShutdownHook().build();

scheduler.start();

// Schedule the task for execution a certain time in the future and optionally provide custom
// data for the execution
scheduler.schedule(
myAdhocTask.instance("1045", new MyTaskData(1001L)), Instant.now().plusSeconds(5));
MY_TASK
.instanceWithId("1045")
.data(new MyTaskData(1001L))
.scheduledTo(Instant.now().plusSeconds(5)));
}

public static class MyTaskData implements Serializable {
Expand Down

0 comments on commit 26e0492

Please sign in to comment.