Skip to content

Commit 40566a5

Browse files
committed
Add support for querying externalExecutionId
resolves #863
1 parent 9b4d7f0 commit 40566a5

File tree

6 files changed

+153
-8
lines changed

6 files changed

+153
-8
lines changed

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/TaskExplorer.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,15 @@ public interface TaskExplorer {
4646
*/
4747
Page<TaskExecution> findRunningTaskExecutions(String taskName, Pageable pageable);
4848

49+
/**
50+
* Retrieve a collection of taskExecutions that contain the provided external
51+
* execution id.
52+
* @param externalExecutionId the external execution id of the tasks
53+
* @param pageable the constraints for the search
54+
* @return the set of task executions for tasks with the external execution id
55+
*/
56+
Page<TaskExecution> findTaskExecutionsByExecutionId(String externalExecutionId, Pageable pageable);
57+
4958
/**
5059
* Retrieve a list of available task names.
5160
* @return the set of task names that have been executed
@@ -71,6 +80,13 @@ public interface TaskExplorer {
7180
*/
7281
long getRunningTaskExecutionCount();
7382

83+
/**
84+
* Retrieves current number of task executions by external executionId.
85+
* @param externalExecutionId The externalExecutionId to be searched.
86+
* @return current number of task executions for a specific externalExecutionId.
87+
*/
88+
long getTaskExecutionCountByExternalExecutionId(String externalExecutionId);
89+
7490
/**
7591
* Get a collection/page of executions.
7692
* @param taskName the name of the task to be searched

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDao.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,6 +85,11 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
8585
*/
8686
public static final String TASK_NAME_WHERE_CLAUSE = "where TASK_NAME = :taskName ";
8787

88+
/**
89+
* WHERE clause for external execution id.
90+
*/
91+
public static final String EXTERNAL_EXECUTION_ID_WHERE_CLAUSE = "where EXTERNAL_EXECUTION_ID = :externalExecutionId ";
92+
8893
private static final String SAVE_TASK_EXECUTION = "INSERT into %PREFIX%EXECUTION"
8994
+ "(TASK_EXECUTION_ID, EXIT_CODE, START_TIME, TASK_NAME, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID)"
9095
+ "values (:taskExecutionId, :exitCode, :startTime, "
@@ -126,6 +131,9 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
126131
private static final String TASK_EXECUTION_COUNT_BY_NAME = "SELECT COUNT(*) FROM "
127132
+ "%PREFIX%EXECUTION where TASK_NAME = :taskName";
128133

134+
private static final String TASK_EXECUTION_COUNT_BY_EXTERNAL_EXECUTION_ID = "SELECT COUNT(*) FROM "
135+
+ "%PREFIX%EXECUTION where EXTERNAL_EXECUTION_ID = :externalExecutionId";
136+
129137
private static final String RUNNING_TASK_EXECUTION_COUNT_BY_NAME = "SELECT COUNT(*) FROM "
130138
+ "%PREFIX%EXECUTION where TASK_NAME = :taskName AND END_TIME IS NULL ";
131139

@@ -407,6 +415,27 @@ public Page<TaskExecution> findRunningTaskExecutions(String taskName, Pageable p
407415
new MapSqlParameterSource("taskName", taskName), getRunningTaskExecutionCountByTaskName(taskName));
408416
}
409417

418+
@Override
419+
public Page<TaskExecution> findTaskExecutionsByExternalExecutionId(String externalExecutionId, Pageable pageable) {
420+
return queryForPageableResults(pageable, SELECT_CLAUSE, FROM_CLAUSE, EXTERNAL_EXECUTION_ID_WHERE_CLAUSE,
421+
new MapSqlParameterSource("externalExecutionId", externalExecutionId),
422+
getTaskExecutionCountByExternalExecutionId(externalExecutionId));
423+
}
424+
425+
@Override
426+
public long getTaskExecutionCountByExternalExecutionId(String externalExecutionId) {
427+
final MapSqlParameterSource queryParameters = new MapSqlParameterSource().addValue("externalExecutionId",
428+
externalExecutionId, Types.VARCHAR);
429+
430+
try {
431+
return this.jdbcTemplate.queryForObject(getQuery(TASK_EXECUTION_COUNT_BY_EXTERNAL_EXECUTION_ID),
432+
queryParameters, Long.class);
433+
}
434+
catch (EmptyResultDataAccessException e) {
435+
return 0;
436+
}
437+
}
438+
410439
@Override
411440
public Page<TaskExecution> findTaskExecutionsByName(String taskName, Pageable pageable) {
412441
return queryForPageableResults(pageable, SELECT_CLAUSE, FROM_CLAUSE, TASK_NAME_WHERE_CLAUSE,

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/MapTaskExecutionDao.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -128,6 +128,17 @@ public long getTaskExecutionCountByTaskName(String taskName) {
128128
return count;
129129
}
130130

131+
@Override
132+
public long getTaskExecutionCountByExternalExecutionId(String externalExecutionId) {
133+
int count = 0;
134+
for (Map.Entry<Long, TaskExecution> entry : this.taskExecutions.entrySet()) {
135+
if (entry.getValue().getExternalExecutionId().equals(externalExecutionId)) {
136+
count++;
137+
}
138+
}
139+
return count;
140+
}
141+
131142
@Override
132143
public long getRunningTaskExecutionCountByTaskName(String taskName) {
133144
int count = 0;
@@ -166,6 +177,18 @@ public Page<TaskExecution> findRunningTaskExecutions(String taskName, Pageable p
166177
return getPageFromList(new ArrayList<>(result), pageable, getRunningTaskExecutionCountByTaskName(taskName));
167178
}
168179

180+
@Override
181+
public Page<TaskExecution> findTaskExecutionsByExternalExecutionId(String externalExecutionId, Pageable pageable) {
182+
Set<TaskExecution> result = getTaskExecutionTreeSet();
183+
for (Map.Entry<Long, TaskExecution> entry : this.taskExecutions.entrySet()) {
184+
if (entry.getValue().getExternalExecutionId().equals(externalExecutionId)) {
185+
result.add(entry.getValue());
186+
}
187+
}
188+
return getPageFromList(new ArrayList<>(result), pageable,
189+
getTaskExecutionCountByExternalExecutionId(externalExecutionId));
190+
}
191+
169192
@Override
170193
public Page<TaskExecution> findTaskExecutionsByName(String taskName, Pageable pageable) {
171194
Set<TaskExecution> filteredSet = getTaskExecutionTreeSet();

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/TaskExecutionDao.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -150,6 +150,24 @@ void completeTaskExecution(long executionId, Integer exitCode, LocalDateTime end
150150
*/
151151
Page<TaskExecution> findRunningTaskExecutions(String taskName, Pageable pageable);
152152

153+
/**
154+
* Retrieve a collection of taskExecutions that contain the provided external
155+
* execution id.
156+
* @param externalExecutionId the external execution id of the tasks
157+
* @param pageable the constraints for the search
158+
* @return the set of task executions for tasks with the externalExecutionId
159+
*/
160+
Page<TaskExecution> findTaskExecutionsByExternalExecutionId(String externalExecutionId, Pageable pageable);
161+
162+
/**
163+
* Retrieves current number of task executions for a externalTaskExecutionId.
164+
* @param externalExecutionId the external execution id of the task to search for in
165+
* the repository.
166+
* @return current number of task executions for the externalExecutionId.
167+
*/
168+
169+
long getTaskExecutionCountByExternalExecutionId(String externalExecutionId);
170+
153171
/**
154172
* Retrieves a subset of task executions by task name, start location and size.
155173
* @param taskName the name of the task to search for in the repository.

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskExplorer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,11 @@ public Page<TaskExecution> findRunningTaskExecutions(String taskName, Pageable p
5959
return this.taskExecutionDao.findRunningTaskExecutions(taskName, pageable);
6060
}
6161

62+
@Override
63+
public Page<TaskExecution> findTaskExecutionsByExecutionId(String externalExecutionId, Pageable pageable) {
64+
return this.taskExecutionDao.findTaskExecutionsByExternalExecutionId(externalExecutionId, pageable);
65+
}
66+
6267
@Override
6368
public List<String> getTaskNames() {
6469
return this.taskExecutionDao.getTaskNames();
@@ -79,6 +84,11 @@ public long getRunningTaskExecutionCount() {
7984
return this.taskExecutionDao.getRunningTaskExecutionCount();
8085
}
8186

87+
@Override
88+
public long getTaskExecutionCountByExternalExecutionId(String externalExecutionId) {
89+
return this.taskExecutionDao.getTaskExecutionCountByExternalExecutionId(externalExecutionId);
90+
}
91+
8292
@Override
8393
public Page<TaskExecution> findTaskExecutionsByName(String taskName, Pageable pageable) {
8494
return this.taskExecutionDao.findTaskExecutionsByName(taskName, pageable);

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskExplorerTests.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -151,7 +151,7 @@ public void findRunningTasks(DaoType testType) {
151151
final int COMPLETE_COUNT = 5;
152152

153153
Map<Long, TaskExecution> expectedResults = new HashMap<>();
154-
// Store completed jobs
154+
// Store completed task executions
155155
int i = 0;
156156
for (; i < COMPLETE_COUNT; i++) {
157157
createAndSaveTaskExecution(i);
@@ -178,6 +178,55 @@ public void findRunningTasks(DaoType testType) {
178178
}
179179
}
180180

181+
@ParameterizedTest
182+
@MethodSource("data")
183+
public void findTasksByExternalExecutionId(DaoType testType) {
184+
testDefaultContext(testType);
185+
Map<Long, TaskExecution> sampleDataSet = createSampleDataSet(33);
186+
sampleDataSet.values().forEach(taskExecution -> {
187+
Page<TaskExecution> taskExecutionsByExecutionId = this.taskExplorer
188+
.findTaskExecutionsByExecutionId(taskExecution.getExternalExecutionId(), PageRequest.of(0, 5));
189+
assertThat(taskExecutionsByExecutionId.getTotalElements()).isEqualTo(1);
190+
assertThat(this.taskExplorer
191+
.getTaskExecutionCountByExternalExecutionId(taskExecution.getExternalExecutionId())).isEqualTo(1);
192+
TaskExecution resultTaskExecution = taskExecutionsByExecutionId.getContent().get(0);
193+
assertThat(resultTaskExecution.getExecutionId()).isEqualTo(taskExecution.getExecutionId());
194+
});
195+
}
196+
197+
@ParameterizedTest
198+
@MethodSource("data")
199+
public void findTasksByExternalExecutionIdMultipleEntry(DaoType testType) {
200+
testDefaultContext(testType);
201+
202+
testDefaultContext(testType);
203+
final int SAME_EXTERNAL_ID_COUNT = 2;
204+
final int UNIQUE_COUNT = 3;
205+
206+
Map<Long, TaskExecution> expectedResults = new HashMap<>();
207+
// Store task executions each with a unique external execution id
208+
int i = 0;
209+
for (; i < UNIQUE_COUNT; i++) {
210+
createAndSaveTaskExecution(i);
211+
}
212+
// Create task execution with same external execution id
213+
for (; i < (UNIQUE_COUNT + SAME_EXTERNAL_ID_COUNT); i++) {
214+
TaskExecution expectedTaskExecution = this.taskRepository.createTaskExecution(getSimpleTaskExecution());
215+
expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution);
216+
}
217+
Pageable pageable = PageRequest.of(0, 10);
218+
Page<TaskExecution> resultSet = this.taskExplorer.findTaskExecutionsByExecutionId(EXTERNAL_EXECUTION_ID,
219+
pageable);
220+
assertThat(resultSet.getTotalElements()).isEqualTo(SAME_EXTERNAL_ID_COUNT);
221+
List<TaskExecution> taskExecutions = resultSet.getContent();
222+
taskExecutions.forEach(taskExecution -> {
223+
assertThat(expectedResults.keySet()).contains(taskExecution.getExecutionId());
224+
});
225+
assertThat(this.taskExplorer.getTaskExecutionCountByExternalExecutionId(EXTERNAL_EXECUTION_ID))
226+
.isEqualTo(SAME_EXTERNAL_ID_COUNT);
227+
228+
}
229+
181230
@ParameterizedTest
182231
@MethodSource("data")
183232
public void findTasksByName(DaoType testType) {
@@ -186,7 +235,7 @@ public void findTasksByName(DaoType testType) {
186235
final int COMPLETE_COUNT = 7;
187236

188237
Map<Long, TaskExecution> expectedResults = new HashMap<>();
189-
// Store completed jobs
238+
// Store completed task executions
190239
for (int i = 0; i < COMPLETE_COUNT; i++) {
191240
createAndSaveTaskExecution(i);
192241
}

0 commit comments

Comments
 (0)