Skip to content

Commit

Permalink
feat: Add rescheduleOrCreate methode to the SchedulerClient
Browse files Browse the repository at this point in the history
  • Loading branch information
beilCrxmarkets committed Nov 12, 2024
1 parent 28ce451 commit 6e31bab
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ public <T> void reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTi
this.delegate.reschedule(taskInstanceId, newExecutionTime, newData);
}

@Override
public <T> void rescheduleOrCreate(TaskInstance<T> taskInstance, Instant executionTime) {
this.delegate.rescheduleOrCreate(taskInstance, executionTime);
}

@Override
public <T> void rescheduleOrCreate(SchedulableInstance<T> schedulableInstance) {
this.delegate.rescheduleOrCreate(schedulableInstance);
}

@Override
public void cancel(TaskInstanceId taskInstanceId) {
this.delegate.cancel(taskInstanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ public interface SchedulerClient {
*/
<T> void reschedule(SchedulableInstance<T> schedulableInstance);

/**
* Updates an existing execution to a new execution time.
*
* <p>Should the execution not exist, it will be scheduled. An exception is thrown if the
* execution is currently running.
*
* @param taskInstance Task-instance, optionally with data
* @param executionTime Instant it should run
* @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> void rescheduleOrCreate(TaskInstance<T> taskInstance, Instant executionTime);

/**
* Updates an existing execution with a new execution-time and new task-data.
*
* <p>Should the execution not exist, it will be scheduled. An exception is thrown if the
* execution is currently running.
*
* @param schedulableInstance Task-instance and time it should run
* @throws TaskInstanceCurrentlyExecutingException if the execution is currently running
* @see com.github.kagkarlsson.scheduler.task.SchedulableInstance
* @see com.github.kagkarlsson.scheduler.exceptions.TaskInstanceCurrentlyExecutingException
*/
<T> void rescheduleOrCreate(SchedulableInstance<T> schedulableInstance);

/**
* Removes/Cancels an execution.
*
Expand Down Expand Up @@ -352,6 +380,26 @@ public <T> void reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTi
}
}

@Override
public <T> void rescheduleOrCreate(TaskInstance<T> taskInstance, Instant executionTime) {
Optional<ScheduledExecution<Object>> executionObject =
this.getScheduledExecutions().stream()
.filter(e -> e.getTaskInstance().getId().equals(taskInstance.getId()))
.findFirst();
if (executionObject.isPresent()) {
this.reschedule(taskInstance, executionTime, taskInstance.getData());
} else {
this.scheduleIfNotExists(taskInstance, executionTime);
}
}

@Override
public <T> void rescheduleOrCreate(SchedulableInstance<T> schedulableInstance) {
rescheduleOrCreate(
schedulableInstance.getTaskInstance(),
schedulableInstance.getNextExecutionTime(clock.now()));
}

@Override
public void cancel(TaskInstanceId taskInstanceId) {
String taskName = taskInstanceId.getTaskName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ public void client_should_be_able_to_reschedule_executions() {
assertThat(savingHandler.savedData, CoreMatchers.is(data2));
}

@Test
public void client_should_be_able_to_rescheduleOrCreate_executions() {
String data1 = "data1";
String data2 = "data2";

scheduler.rescheduleOrCreate(savingTask.instance("1", data1), settableClock.now());
scheduler.runAnyDueExecutions();
assertThat(savingHandler.savedData, CoreMatchers.is(data1));

scheduler.rescheduleOrCreate(
savingTask.instance("2", "none"), settableClock.now().plusSeconds(1));
scheduler.rescheduleOrCreate(savingTask.instance("2", data2), settableClock.now());
scheduler.runAnyDueExecutions();
assertThat(savingHandler.savedData, CoreMatchers.is(data2));

scheduler.tick(ofSeconds(1));
scheduler.runAnyDueExecutions();
assertThat(savingHandler.savedData, CoreMatchers.is(data2));
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void raw_client_should_be_able_to_fetch_executions() {
Expand Down

0 comments on commit 6e31bab

Please sign in to comment.