Skip to content

Commit

Permalink
Merge pull request #53 from kagkarlsson/check_for_existing_executions
Browse files Browse the repository at this point in the history
Expose method on SchedulerClient for checking if a single execution exists.
  • Loading branch information
kagkarlsson authored Mar 20, 2019
2 parents 56e63c6 + 9bcc2b7 commit 969c4b8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/github/kagkarlsson/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ public <T> void getScheduledExecutionsForTask(String taskName, Class<T> dataClas
this.delegate.getScheduledExecutionsForTask(taskName, dataClass, consumer);
}

@Override
public Optional<ScheduledExecution<Object>> getScheduledExecution(TaskInstanceId taskInstanceId) {
return this.delegate.getScheduledExecution(taskInstanceId);
}

public List<Execution> getFailingExecutions(Duration failingAtLeastFor) {
return taskRepository.getExecutionsFailingLongerThan(failingAtLeastFor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public interface SchedulerClient {

<T> void getScheduledExecutionsForTask(String taskName, Class<T> dataClass, Consumer<ScheduledExecution<T>> consumer);

Optional<ScheduledExecution<Object>> getScheduledExecution(TaskInstanceId taskInstanceId);

class Builder {

private final DataSource dataSource;
Expand Down Expand Up @@ -144,6 +146,12 @@ public <T> void getScheduledExecutionsForTask(String taskName, Class<T> dataClas
taskRepository.getScheduledExecutions(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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.kagkarlsson.scheduler;

import com.github.kagkarlsson.scheduler.task.Execution;
import com.github.kagkarlsson.scheduler.task.TaskInstanceId;

import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -43,4 +44,5 @@ public interface TaskRepository {
List<Execution> getExecutionsFailingLongerThan(Duration interval);

Optional<Execution> getExecution(String taskName, String taskInstanceId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.github.kagkarlsson.scheduler.task;

import java.util.Objects;

public interface TaskInstanceId {
String getTaskName();
String getId();
Expand All @@ -41,5 +43,18 @@ public String getId() {
return this.id;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StandardTaskInstanceId that = (StandardTaskInstanceId) o;
return Objects.equals(taskName, that.taskName) &&
Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return Objects.hash(taskName, id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.kagkarlsson.scheduler;

import co.unruly.matchers.OptionalMatchers;
import com.github.kagkarlsson.scheduler.task.TaskInstanceId;
import com.github.kagkarlsson.scheduler.testhelper.ManualScheduler;
import com.github.kagkarlsson.scheduler.testhelper.SettableClock;
import com.github.kagkarlsson.scheduler.testhelper.TestHelper;
Expand All @@ -9,6 +11,7 @@
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.google.common.collect.Lists;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -18,6 +21,7 @@

import static java.time.Duration.ofSeconds;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

public class SchedulerClientTest {
Expand Down Expand Up @@ -84,6 +88,16 @@ public void client_should_be_able_to_fetch_executions_for_task() {
assertThat(countExecutionsForTask(client, oneTimeTaskB.getName(), Void.class), is(3));
}

@Test
public void client_should_be_able_to_fetch_single_scheduled_execution() {
SchedulerClient client = SchedulerClient.Builder.create(DB.getDataSource(), oneTimeTaskA).build();
client.schedule(oneTimeTaskA.instance("1"), settableClock.now());

assertThat(client.getScheduledExecution(TaskInstanceId.of(oneTimeTaskA.getName(), "1")), not(OptionalMatchers.empty()));
assertThat(client.getScheduledExecution(TaskInstanceId.of(oneTimeTaskA.getName(), "2")), OptionalMatchers.empty());
assertThat(client.getScheduledExecution(TaskInstanceId.of(oneTimeTaskB.getName(), "1")), OptionalMatchers.empty());
}

private int countAllExecutions(SchedulerClient client) {
AtomicInteger counter = new AtomicInteger(0);
client.getScheduledExecutions((ScheduledExecution<Object> execution) -> {counter.incrementAndGet();});
Expand Down

0 comments on commit 969c4b8

Please sign in to comment.