Skip to content

Commit 25cf280

Browse files
committed
StartTime is now honored for TaskExecution
regardless if they were created before or during task execution. resolves #861
1 parent db7d7b9 commit 25cf280

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2022 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.
@@ -277,8 +277,9 @@ private void doTaskStart() {
277277
Assert.isNull(taskExecution.getEndTime(),
278278
String.format("Invalid TaskExecution, ID %s task is already complete",
279279
this.taskProperties.getExecutionid()));
280+
Date startDate = (taskExecution.getStartTime() == null) ? new Date() : taskExecution.getStartTime();
280281
this.taskExecution = this.taskRepository.startTaskExecution(this.taskProperties.getExecutionid(),
281-
this.taskNameResolver.getTaskName(), new Date(), args,
282+
this.taskNameResolver.getTaskName(), startDate, args,
282283
this.taskProperties.getExternalExecutionId(), this.taskProperties.getParentExecutionId());
283284
}
284285
else {

spring-cloud-task-integration-tests/src/test/java/org/springframework/cloud/task/executionid/TaskStartTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
2222
import java.util.ArrayList;
23+
import java.util.Collections;
2324
import java.util.Date;
2425
import java.util.HashMap;
2526
import java.util.Map;
@@ -185,6 +186,27 @@ public void testWithGeneratedTaskExecutionWithName() throws Exception {
185186
assertThat(this.taskExplorer.getTaskExecution(1).getTaskName()).isEqualTo("batchEvents");
186187
}
187188

189+
@Test
190+
public void testWithGeneratedTaskExecutionWithExistingDate() throws Exception {
191+
final String TASK_EXECUTION_NAME = "PRE-EXECUTION-TEST-NAME";
192+
Date startDate = new Date();
193+
Thread.sleep(500);
194+
TaskExecution taskExecution = new TaskExecution(1, 0, TASK_EXECUTION_NAME, startDate, new Date(), "foo",
195+
Collections.emptyList(), "foo", "bar", null);
196+
this.taskRepository.createTaskExecution(taskExecution);
197+
assertThat(this.taskExplorer.getTaskExecutionCount()).as("Only one row is expected").isEqualTo(1);
198+
199+
this.applicationContext = getTaskApplication(1).run(new String[0]);
200+
assertThat(waitForDBToBePopulated()).isTrue();
201+
202+
Page<TaskExecution> taskExecutions = this.taskExplorer.findAll(PageRequest.of(0, 10));
203+
assertThat(taskExecutions.getTotalElements()).as("Only one row is expected").isEqualTo(1);
204+
assertThat(taskExecutions.iterator().next().getExitCode().intValue()).as("return code should be 0")
205+
.isEqualTo(0);
206+
assertThat(this.taskExplorer.getTaskExecution(1).getStartTime().getTime()).isEqualTo(startDate.getTime());
207+
208+
}
209+
188210
@Test
189211
public void testWithNoTaskExecution() throws Exception {
190212
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> {

0 commit comments

Comments
 (0)