Implement getRunningTaskCount

Expose functionality via TaskExplorer

Polishing on merge
This commit is contained in:
David Turanski
2018-07-10 16:42:10 -04:00
committed by Glenn Renfro
parent cf19942206
commit ba05720499
7 changed files with 69 additions and 0 deletions

View File

@@ -71,6 +71,13 @@ public interface TaskExplorer {
*/
long getTaskExecutionCount();
/**
* Retrieves current number of running task executions.
*
* @return current number of running task executions.
*/
long getRunningTaskExecutionCount();
/**
* Get a collection/page of executions
*

View File

@@ -46,6 +46,7 @@ import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@@ -56,6 +57,7 @@ import org.springframework.util.StringUtils;
*
* @author Glenn Renfro
* @author Gunnar Hillert
* @author David Turanski
*/
public class JdbcTaskExecutionDao implements TaskExecutionDao {
@@ -115,6 +117,9 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
private static final String RUNNING_TASK_EXECUTION_COUNT_BY_NAME = "SELECT COUNT(*) FROM " +
"%PREFIX%EXECUTION where TASK_NAME = :taskName AND END_TIME IS NULL ";
private static final String RUNNING_TASK_EXECUTION_COUNT = "SELECT COUNT(*) FROM " +
"%PREFIX%EXECUTION where END_TIME IS NULL ";
private static final String LAST_TASK_EXECUTIONS_BY_TASK_NAMES =
"select TE2.* from (" +
"select MAX(TE.TASK_EXECUTION_ID) as TASK_EXECUTION_ID, TE.TASK_NAME, TE.START_TIME from (" +
@@ -325,6 +330,18 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
}
}
@Override
public long getRunningTaskExecutionCount() {
try {
return jdbcTemplate.queryForObject(
getQuery(RUNNING_TASK_EXECUTION_COUNT), (SqlParameterSource)null, Long.class);
}
catch (EmptyResultDataAccessException e) {
return 0;
}
}
@Override
public List<TaskExecution> getLatestTaskExecutionsByTaskNames(String... taskNames) {
Assert.notEmpty(taskNames, "At least 1 task name must be provided.");

View File

@@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
*
* @author Glenn Renfro
* @author Gunnar Hillert
* @author David Turanski
*/
public class MapTaskExecutionDao implements TaskExecutionDao {
@@ -139,6 +140,17 @@ public class MapTaskExecutionDao implements TaskExecutionDao {
return count;
}
@Override
public long getRunningTaskExecutionCount() {
long count = 0;
for (Map.Entry<Long, TaskExecution> entry : taskExecutions.entrySet()) {
if ( entry.getValue().getEndTime() == null) {
count++;
}
}
return count;
}
@Override
public long getTaskExecutionCount() {
return taskExecutions.size();

View File

@@ -29,6 +29,7 @@ import org.springframework.data.domain.Pageable;
*
* @author Glenn Renfro
* @author Gunnar Hillert
* @author David Turanski
*
*/
public interface TaskExecutionDao {
@@ -135,6 +136,14 @@ public interface TaskExecutionDao {
*/
long getRunningTaskExecutionCountByTaskName(String taskName);
/**
* Retrieves current number of task executions with an endTime of null.
*
* @return current number of task executions.
*/
long getRunningTaskExecutionCount();
/**
* Retrieves current number of task executions.
*

View File

@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
* @author Glenn Renfro
* @author Michael Minella
* @author Gunnar Hillert
* @author David Turanski
*/
public class SimpleTaskExplorer implements TaskExplorer {
@@ -73,6 +74,11 @@ public class SimpleTaskExplorer implements TaskExplorer {
return taskExecutionDao.getTaskExecutionCount();
}
@Override
public long getRunningTaskExecutionCount() {
return taskExecutionDao.getRunningTaskExecutionCount();
}
@Override
public Page<TaskExecution> findTaskExecutionsByName(String taskName, Pageable pageable) {
return taskExecutionDao.findTaskExecutionsByName(taskName, pageable);

View File

@@ -239,6 +239,15 @@ public class BaseTaskExecutionDaoTestCases {
assertEquals(9 + executionIdOffset, latestTaskExecution.getExecutionId());
}
@Test
@DirtiesContext
public void getRunningTaskExecutions() {
initializeRepositoryNotInOrderWithMultipleTaskExecutions();
assertEquals(dao.getTaskExecutionCount(), dao.getRunningTaskExecutionCount());
dao.completeTaskExecution(1, 0, new Date(), "c'est fini!" );
assertEquals(dao.getTaskExecutionCount() - 1, dao.getRunningTaskExecutionCount());
}
protected long initializeRepositoryNotInOrderWithMultipleTaskExecutions() {
final TaskExecution foo1_0 = getTaskExecution("FOO1", "externalC");

View File

@@ -154,6 +154,15 @@ public class SimpleTaskExplorerTests {
33, taskExplorer.getTaskExecutionCount());
}
@Test
public void getRunningTaskCount() {
createSampleDataSet(33);
assertEquals(String.format(
"task count did not match expected result for test Type %s",
testType),
33, taskExplorer.getRunningTaskExecutionCount());
}
@Test
public void findRunningTasks() {
final int TEST_COUNT = 2;