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

Expose method on SchedulerClient for checking if a single execution exists. #53

Merged
merged 2 commits into from
Mar 20, 2019
Merged
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
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