Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit d324ebe

Browse files
cppwfsilayaperumalg
authored andcommitted
Support Task Operations and shell commands for task logs
resolves #3390 This is to support Acceptance Tests for validating how CTR handles properties
1 parent 776dc96 commit d324ebe

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

spring-cloud-dataflow-rest-client/src/main/java/org/springframework/cloud/dataflow/rest/client/TaskOperations.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ public interface TaskOperations {
105105
*/
106106
TaskExecutionResource taskExecutionStatus(long id);
107107

108+
/**
109+
* Return the task execution log. The platform from which to retrieve the log will be set to {@code default}.
110+
*
111+
* @param externalExecutionId the external execution identifier of the task execution.
112+
* @return {@link String} containing the log.
113+
*/
114+
String taskExecutionLog(String externalExecutionId);
115+
116+
/**
117+
* Return the task execution log.
118+
*
119+
* @param externalExecutionId the external execution identifier of the task execution.
120+
* @param platform the platform from which to obtain the log.
121+
* @return {@link String} containing the log.
122+
*/
123+
String taskExecutionLog(String externalExecutionId, String platform);
124+
108125
/**
109126
* Return information including the count of currently executing tasks and task execution
110127
* limits.

spring-cloud-dataflow-rest-client/src/main/java/org/springframework/cloud/dataflow/rest/client/TaskTemplate.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collection;
2020
import java.util.Collections;
21+
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324

@@ -71,6 +72,8 @@ public class TaskTemplate implements TaskOperations {
7172

7273
private static final String PLATFORM_LIST_RELATION = "tasks/platforms";
7374

75+
private static final String RETRIEVE_LOG = "tasks/logs";
76+
7477
private final RestTemplate restTemplate;
7578

7679
private final Link definitionsLink;
@@ -91,6 +94,8 @@ public class TaskTemplate implements TaskOperations {
9194

9295
private final String dataFlowServerVersion;
9396

97+
private final Link retrieveLogLink;
98+
9499
TaskTemplate(RestTemplate restTemplate, ResourceSupport resources, String dataFlowServerVersion) {
95100
Assert.notNull(resources, "URI Resources must not be be null");
96101
Assert.notNull(resources.getLink(EXECUTIONS_RELATION), "Executions relation is required");
@@ -101,6 +106,7 @@ public class TaskTemplate implements TaskOperations {
101106
Assert.notNull(resources.getLink(EXECUTION_RELATION), "Execution relation is required");
102107
Assert.notNull(resources.getLink(EXECUTION_RELATION_BY_NAME), "Execution by name relation is required");
103108
Assert.notNull(dataFlowServerVersion, "dataFlowVersion must not be null");
109+
Assert.notNull(resources.getLink(RETRIEVE_LOG), "Log relation is required");
104110

105111
this.dataFlowServerVersion = dataFlowServerVersion;
106112

@@ -125,6 +131,7 @@ public class TaskTemplate implements TaskOperations {
125131
this.executionsCurrentLink = resources.getLink(EXECUTIONS_CURRENT_RELATION);
126132
this.validationLink = resources.getLink(VALIDATION_REL);
127133
this.platformListLink = resources.getLink(PLATFORM_LIST_RELATION);
134+
this.retrieveLogLink = resources.getLink(RETRIEVE_LOG);
128135
}
129136

130137
@Override
@@ -194,6 +201,19 @@ public TaskExecutionResource taskExecutionStatus(long id) {
194201
return restTemplate.getForObject(executionLink.expand(id).getHref(), TaskExecutionResource.class);
195202
}
196203

204+
@Override
205+
public String taskExecutionLog(String externalExecutionId) {
206+
return taskExecutionLog(externalExecutionId, "default");
207+
}
208+
209+
@Override
210+
public String taskExecutionLog(String externalExecutionId, String platform) {
211+
Map<String,String> map = new HashMap<>();
212+
map.put("taskExternalExecutionId",externalExecutionId);
213+
map.put("platformName", platform);
214+
return restTemplate.getForObject(retrieveLogLink.expand(map).getHref(), String.class);
215+
}
216+
197217
@Override
198218
public Collection<CurrentTaskExecutionsResource> currentTaskExecutions() {
199219
ParameterizedTypeReference<Collection<CurrentTaskExecutionsResource>> typeReference =

spring-cloud-dataflow-shell-core/src/main/java/org/springframework/cloud/dataflow/shell/command/TaskCommands.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class TaskCommands implements CommandMarker {
7575

7676
private static final String LAUNCH = "task launch";
7777
private static final String STOP = "task execution stop";
78+
private static final String LOG = "task execution log";
7879

7980
// Destroy Role
8081

@@ -229,6 +230,20 @@ public String stop(@CliOption(key = { "", "ids" }, help = "the task execution id
229230
return String.format("Request to stop the task execution with id(s): %s has been submitted", ids);
230231
}
231232

233+
@CliCommand(value = LOG, help = "Retrieve task execution log")
234+
public String retrieveTaskExecutionLog(@CliOption(key = { "", "id" }, help = "the task execution id", mandatory = true) long id,
235+
@CliOption(key = { "platform" }, help = "the platform of the task execution", mandatory = false) String platform) {
236+
TaskExecutionResource taskExecutionResource = taskOperations().taskExecutionStatus(id);
237+
String result;
238+
if(platform != null) {
239+
result = taskOperations().taskExecutionLog(taskExecutionResource.getExternalExecutionId(), platform);
240+
}
241+
else {
242+
result = taskOperations().taskExecutionLog(taskExecutionResource.getExternalExecutionId());
243+
}
244+
return result;
245+
}
246+
232247
@CliCommand(value = DESTROY, help = "Destroy an existing task")
233248
public String destroy(
234249
@CliOption(key = { "", "name" }, help = "the name of the task to destroy", mandatory = true,

spring-cloud-dataflow-shell-core/src/test/java/org/springframework/cloud/dataflow/shell/command/TaskCommandTemplate.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
*/
4040
public class TaskCommandTemplate {
4141

42+
private final static int WAIT_INTERVAL = 500;
43+
44+
private final static int MAX_WAIT_TIME = 3000;
45+
4246
private final JLineShellComponent shell;
4347

4448
private List<String> tasks = new ArrayList<String>();
@@ -103,6 +107,65 @@ public long launchWithAlternateCTR(String taskName, String ctrAppName) {
103107
return value;
104108
}
105109

110+
/**
111+
* Launch a task and validate the result from shell on default platform.
112+
*
113+
* @param taskName the name of the task
114+
*/
115+
public String getTaskExecutionLog(String taskName) throws Exception{
116+
long id = launchTaskExecutionForLog(taskName);
117+
CommandResult cr = shell.executeCommand("task execution log --id " + id);
118+
assertTrue(cr.toString().contains("Starting"));
119+
120+
return cr.toString();
121+
}
122+
123+
/**
124+
* Launch a task with invalid platform.
125+
*
126+
* @param taskName the name of the task
127+
*/
128+
public void getTaskExecutionLogInvalidPlatform(String taskName) throws Exception{
129+
long id = launchTaskExecutionForLog(taskName);
130+
shell.executeCommand(String.format("task execution log --id %s --platform %s", id, "foo"));
131+
}
132+
133+
/**
134+
* Launch a task with invalid task execution id
135+
136+
*/
137+
public void getTaskExecutionLogInvalidId() throws Exception{
138+
CommandResult cr = shell.executeCommand(String.format("task execution log --id %s", 88));
139+
}
140+
141+
private long launchTaskExecutionForLog(String taskName) throws Exception{
142+
// add the task name to the tasks list before assertion
143+
tasks.add(taskName);
144+
CommandResult cr = shell.executeCommand(String.format("task launch %s", taskName));
145+
CommandResult idResult = shell.executeCommand("task execution list --name " + taskName);
146+
Table taskExecutionResult = (Table) idResult.getResult();
147+
148+
long id = (long) taskExecutionResult.getModel().getValue(1, 1);
149+
assertTrue(cr.toString().contains("with execution id " + id));
150+
waitForDBToBePopulated(id);
151+
return id;
152+
}
153+
154+
private void waitForDBToBePopulated(long id) throws Exception {
155+
for (int waitTime = 0; waitTime <= MAX_WAIT_TIME; waitTime += WAIT_INTERVAL) {
156+
Thread.sleep(WAIT_INTERVAL);
157+
if (isEndTime(id)) {
158+
break;
159+
}
160+
}
161+
}
162+
163+
private boolean isEndTime(long id) {
164+
CommandResult cr = taskExecutionStatus(id);
165+
Table table = (Table) cr.getResult();
166+
return (table.getModel().getValue(6, 1) != null);
167+
168+
}
106169
/**
107170
* Stop a task execution.
108171
*

spring-cloud-dataflow-shell-core/src/test/java/org/springframework/cloud/dataflow/shell/command/TaskCommandTests.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@ public void testTaskLaunchCTRUsingAltCtrName() {
128128
task().launchWithAlternateCTR(taskName, "timestamp");
129129
}
130130

131+
@Test
132+
public void testGetLog() throws Exception{
133+
logger.info("Retrieving task execution log");
134+
String taskName = generateUniqueStreamOrTaskName();
135+
task().create(taskName, "timestamp");
136+
task().getTaskExecutionLog(taskName);
137+
}
138+
139+
@Test(expected = IllegalArgumentException.class)
140+
public void testGetLogInvalidPlatform() throws Exception{
141+
logger.info("Retrieving task execution log");
142+
String taskName = generateUniqueStreamOrTaskName();
143+
task().create(taskName, "timestamp");
144+
task().getTaskExecutionLogInvalidPlatform(taskName);
145+
}
146+
147+
@Test(expected = IllegalArgumentException.class)
148+
public void testGetLogInvalidId() throws Exception{
149+
task().getTaskExecutionLogInvalidId();
150+
}
151+
131152
@Test
132153
public void testTaskLaunchCTRUsingInvalidAltCtrAppName() {
133154
testInvalidCTRLaunch("1: timestamp && 2: timestamp", "timesdaftamp",
@@ -245,8 +266,7 @@ public void testViewExecution() {
245266

246267
CommandResult idResult = task().taskExecutionList();
247268
Table result = (Table) idResult.getResult();
248-
249-
long value = (long) result.getModel().getValue(1, 1);
269+
long value = (long) result.getModel().getValue(findRowForExecutionId(result, TASK_EXECUTION_ID), 1);
250270
logger.info("Looking up id " + value);
251271
CommandResult cr = task().taskExecutionStatus(value);
252272
assertTrue("task execution status command must be successful", cr.isSuccess());
@@ -339,4 +359,17 @@ private void verifyTableValue(Table table, int row, int col, Object expected) {
339359
assertEquals(String.format("Row %d, Column %d should be: %s", row, col, expected),expected,
340360
table.getModel().getValue(row, col));
341361
}
362+
363+
private int findRowForExecutionId(Table table, long id) {
364+
int result = -1;
365+
for(int rowNum = 0; rowNum < table.getModel().getRowCount(); rowNum++) {
366+
if(table.getModel().getValue(rowNum, 1).equals(id)) {
367+
result = rowNum;
368+
break;
369+
}
370+
}
371+
assertTrue("Task Execution Id specified was not found in execution list", id > -1);
372+
return result;
373+
}
374+
342375
}

0 commit comments

Comments
 (0)