From 25cf280f78068804e78df87dd4a820ed55f1db25 Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Wed, 14 Sep 2022 11:00:12 -0400 Subject: [PATCH] StartTime is now honored for TaskExecution regardless if they were created before or during task execution. resolves #861 --- .../task/listener/TaskLifecycleListener.java | 5 +++-- .../task/executionid/TaskStartTests.java | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java index fea00dd3..dbb8d41f 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 the original author or authors. + * Copyright 2016-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -277,8 +277,9 @@ public class TaskLifecycleListener Assert.isNull(taskExecution.getEndTime(), String.format("Invalid TaskExecution, ID %s task is already complete", this.taskProperties.getExecutionid())); + Date startDate = (taskExecution.getStartTime() == null) ? new Date() : taskExecution.getStartTime(); this.taskExecution = this.taskRepository.startTaskExecution(this.taskProperties.getExecutionid(), - this.taskNameResolver.getTaskName(), new Date(), args, + this.taskNameResolver.getTaskName(), startDate, args, this.taskProperties.getExternalExecutionId(), this.taskProperties.getParentExecutionId()); } else { diff --git a/spring-cloud-task-integration-tests/src/test/java/org/springframework/cloud/task/executionid/TaskStartTests.java b/spring-cloud-task-integration-tests/src/test/java/org/springframework/cloud/task/executionid/TaskStartTests.java index 27796a7e..5cc83015 100644 --- a/spring-cloud-task-integration-tests/src/test/java/org/springframework/cloud/task/executionid/TaskStartTests.java +++ b/spring-cloud-task-integration-tests/src/test/java/org/springframework/cloud/task/executionid/TaskStartTests.java @@ -20,6 +20,7 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -185,6 +186,27 @@ public class TaskStartTests { assertThat(this.taskExplorer.getTaskExecution(1).getTaskName()).isEqualTo("batchEvents"); } + @Test + public void testWithGeneratedTaskExecutionWithExistingDate() throws Exception { + final String TASK_EXECUTION_NAME = "PRE-EXECUTION-TEST-NAME"; + Date startDate = new Date(); + Thread.sleep(500); + TaskExecution taskExecution = new TaskExecution(1, 0, TASK_EXECUTION_NAME, startDate, new Date(), "foo", + Collections.emptyList(), "foo", "bar", null); + this.taskRepository.createTaskExecution(taskExecution); + assertThat(this.taskExplorer.getTaskExecutionCount()).as("Only one row is expected").isEqualTo(1); + + this.applicationContext = getTaskApplication(1).run(new String[0]); + assertThat(waitForDBToBePopulated()).isTrue(); + + Page taskExecutions = this.taskExplorer.findAll(PageRequest.of(0, 10)); + assertThat(taskExecutions.getTotalElements()).as("Only one row is expected").isEqualTo(1); + assertThat(taskExecutions.iterator().next().getExitCode().intValue()).as("return code should be 0") + .isEqualTo(0); + assertThat(this.taskExplorer.getTaskExecution(1).getStartTime().getTime()).isEqualTo(startDate.getTime()); + + } + @Test public void testWithNoTaskExecution() throws Exception { assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> {