Skip to content

Commit

Permalink
spotless:apply
Browse files Browse the repository at this point in the history
  • Loading branch information
kagkarlsson committed Aug 25, 2023
1 parent f93afa0 commit 65e6926
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.github.kagkarlsson.scheduler.exceptions.MissingRawDataException;
import com.github.kagkarlsson.scheduler.task.Execution;
import com.github.kagkarlsson.scheduler.task.TaskInstanceId;

import java.time.Instant;
import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,12 @@ <T> void fetchScheduledExecutionsForTask(
/**
* @see #fetchScheduledExecutionsForTask(String, Class, Consumer)
*/
default <T> List<ScheduledExecution<Object>> getScheduledExecutionsForTask(
String taskName) {
default <T> List<ScheduledExecution<Object>> getScheduledExecutionsForTask(String taskName) {
List<ScheduledExecution<Object>> executions = new ArrayList<>();
fetchScheduledExecutionsForTask(taskName, Object.class, executions::add);
return executions;
}


/**
* @see #fetchScheduledExecutionsForTask(String, Class, Consumer)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public class DataClassMismatchException extends DbSchedulerException {
public DataClassMismatchException(Class expectedClass, Class actualClass) {
super(
String.format(
"Task data mismatch. If actual data-class is byte[], it might have been fetched without" +
" knowledge of task-data types, and is thus not deserialized." +
" Use getRawData() to get non-deserialized data in that case." +
" Expected class : %s, actual : %s", expectedClass, actualClass));
"Task data mismatch. If actual data-class is byte[], it might have been fetched without"
+ " knowledge of task-data types, and is thus not deserialized."
+ " Use getRawData() to get non-deserialized data in that case."
+ " Expected class : %s, actual : %s",
expectedClass, actualClass));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
package com.github.kagkarlsson.scheduler.jdbc;

import static com.github.kagkarlsson.scheduler.StringUtils.truncate;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

import com.github.kagkarlsson.jdbc.JdbcRunner;
import com.github.kagkarlsson.jdbc.ResultSetMapper;
import com.github.kagkarlsson.jdbc.SQLRuntimeException;
Expand All @@ -25,10 +30,6 @@
import com.github.kagkarlsson.scheduler.task.SchedulableInstance;
import com.github.kagkarlsson.scheduler.task.Task;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -39,11 +40,9 @@
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static com.github.kagkarlsson.scheduler.StringUtils.truncate;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("rawtypes")
public class JdbcTaskRepository implements TaskRepository {
Expand Down Expand Up @@ -579,11 +578,11 @@ private class ExecutionResultSetMapper implements ResultSetMapper<List<Execution
private final ExecutionResultSetConsumer delegate;

private ExecutionResultSetMapper(
boolean includeUnresolved, boolean addUnresolvedToExclusionFilter) {
boolean includeUnresolved, boolean addUnresolvedToExclusionFilter) {
this.executions = new ArrayList<>();
this.delegate =
new ExecutionResultSetConsumer(
executions::add, includeUnresolved, addUnresolvedToExclusionFilter);
new ExecutionResultSetConsumer(
executions::add, includeUnresolved, addUnresolvedToExclusionFilter);
}

@Override
Expand All @@ -605,9 +604,9 @@ private ExecutionResultSetConsumer(Consumer<Execution> consumer) {
}

private ExecutionResultSetConsumer(
Consumer<Execution> consumer,
boolean includeUnresolved,
boolean addUnresolvedToExclusionFilter) {
Consumer<Execution> consumer,
boolean includeUnresolved,
boolean addUnresolvedToExclusionFilter) {
this.consumer = consumer;
this.includeUnresolved = includeUnresolved;
this.addUnresolvedToExclusionFilter = addUnresolvedToExclusionFilter;
Expand All @@ -623,11 +622,11 @@ public Void map(ResultSet rs) throws SQLException {
if (!task.isPresent() && !includeUnresolved) {
if (addUnresolvedToExclusionFilter) {
LOG.warn(
"Failed to find implementation for task with name '{}'. Execution will be excluded from due. "
+ "Either delete the execution from the database, or add an implementation for it. "
+ "The scheduler may be configured to automatically delete unresolved tasks "
+ "after a certain period of time.",
taskName);
"Failed to find implementation for task with name '{}'. Execution will be excluded from due. "
+ "Either delete the execution from the database, or add an implementation for it. "
+ "The scheduler may be configured to automatically delete unresolved tasks "
+ "after a certain period of time.",
taskName);
}
continue;
}
Expand All @@ -642,32 +641,32 @@ public Void map(ResultSet rs) throws SQLException {
Instant lastSuccess = jdbcCustomization.getInstant(rs, "last_success");
Instant lastFailure = jdbcCustomization.getInstant(rs, "last_failure");
int consecutiveFailures =
rs.getInt("consecutive_failures"); // null-value is returned as 0 which is the preferred
rs.getInt("consecutive_failures"); // null-value is returned as 0 which is the preferred
// default
Instant lastHeartbeat = jdbcCustomization.getInstant(rs, "last_heartbeat");
long version = rs.getLong("version");

Supplier dataSupplier =
memoize(
() -> {
if (!task.isPresent()) {
// return the data raw if the type is not known
// a case for standalone clients, with no "known tasks"
return data;
}
return serializer.deserialize(task.get().getDataClass(), data);
});
memoize(
() -> {
if (!task.isPresent()) {
// return the data raw if the type is not known
// a case for standalone clients, with no "known tasks"
return data;
}
return serializer.deserialize(task.get().getDataClass(), data);
});
this.consumer.accept(
new Execution(
executionTime,
new TaskInstance(taskName, instanceId, dataSupplier),
picked,
pickedBy,
lastSuccess,
lastFailure,
consecutiveFailures,
lastHeartbeat,
version));
new Execution(
executionTime,
new TaskInstance(taskName, instanceId, dataSupplier),
picked,
pickedBy,
lastSuccess,
lastFailure,
consecutiveFailures,
lastHeartbeat,
version));
}

return null;
Expand All @@ -680,7 +679,9 @@ private static <T> Supplier<T> memoize(Supplier<T> original) {

public T get() {
return delegate.get();
} Supplier<T> delegate = this::firstTime;
}

Supplier<T> delegate = this::firstTime;

private synchronized T firstTime() {
if (!initialized) {
Expand All @@ -690,8 +691,6 @@ private synchronized T firstTime() {
}
return delegate.get();
}


};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Optional;
import java.util.Random;
import java.util.stream.IntStream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -413,7 +412,8 @@ public void get_scheduled_executions_should_work_with_unresolved() {
assertThat(taskResolver.getUnresolved(), hasSize(1));

assertThat(getScheduledExecutions(ScheduledExecutionsFilter.onlyResolved()), hasSize(0));
assertThat(getScheduledExecutions(ScheduledExecutionsFilter.onlyResolved(), taskName), hasSize(0));
assertThat(
getScheduledExecutions(ScheduledExecutionsFilter.onlyResolved(), taskName), hasSize(0));
assertThat(getScheduledExecutions(all()), hasSize(1));
assertThat(getScheduledExecutions(all(), taskName), hasSize(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.github.kagkarlsson.scheduler.task.Execution;
import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask;
import java.time.Instant;

import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -63,7 +62,6 @@ public void test_data_class_type_not_equals() {
});

assertThat(
dataClassMismatchException.getMessage(),
CoreMatchers.containsString("Task data mismatch"));
dataClassMismatchException.getMessage(), CoreMatchers.containsString("Task data mismatch"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.github.kagkarlsson.scheduler;

import static com.github.kagkarlsson.scheduler.SchedulerClient.Builder.create;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import co.unruly.matchers.OptionalMatchers;
import com.github.kagkarlsson.scheduler.TestTasks.SavingHandler;
import com.github.kagkarlsson.scheduler.serializer.JavaSerializer;
Expand All @@ -11,22 +18,14 @@
import com.github.kagkarlsson.scheduler.testhelper.ManualScheduler;
import com.github.kagkarlsson.scheduler.testhelper.SettableClock;
import com.github.kagkarlsson.scheduler.testhelper.TestHelper;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import static com.github.kagkarlsson.scheduler.SchedulerClient.Builder.create;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class SchedulerClientTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public void run(DataSource dataSource) {
System.out.println("Task a executed");
});

final SchedulerClient clientWithTypeInformation = SchedulerClient.Builder
.create(dataSource, task)
.serializer(new JacksonSerializer())
.build();
final SchedulerClient clientWithTypeInformation =
SchedulerClient.Builder.create(dataSource, task)
.serializer(new JacksonSerializer())
.build();

final Instant now = Instant.now();
for (int i = 0; i < 5; i++) {
Expand All @@ -58,23 +58,22 @@ public void run(DataSource dataSource) {
execution.getTaskInstance().getTaskName(),
execution.getTaskInstance().getId(),
execution.getExecutionTime(),
execution.getData()
);
execution.getData());
});

final SchedulerClient rawClient = SchedulerClient.Builder.create(dataSource).build();
System.out.println("Listing scheduled executions for client with no known tasks (data-classes and implementations)");
System.out.println(
"Listing scheduled executions for client with no known tasks (data-classes and implementations)");
rawClient
.getScheduledExecutions(ScheduledExecutionsFilter.all())
.forEach(
execution -> {
System.out.printf(
"Scheduled execution: taskName=%s, instance=%s, executionTime=%s, data=%s%n",
execution.getTaskInstance().getTaskName(),
execution.getTaskInstance().getId(),
execution.getExecutionTime(),
new String((byte[]) execution.getData())
);
});
.getScheduledExecutions(ScheduledExecutionsFilter.all())
.forEach(
execution -> {
System.out.printf(
"Scheduled execution: taskName=%s, instance=%s, executionTime=%s, data=%s%n",
execution.getTaskInstance().getTaskName(),
execution.getTaskInstance().getId(),
execution.getExecutionTime(),
new String((byte[]) execution.getData()));
});
}
}

0 comments on commit 65e6926

Please sign in to comment.