Checks for invalid ExecutionId in the TaskLifeCycleListener

resolves #115
* Fixes bug where if the user set the environment variable and commandline args a unique constraint would fire.
* Updated docs
* Removed deprecation
* Fixed version number for integration test.

Added integration tests for externally generated task executions

Add externalExecutionId

resolves #80
This commit is contained in:
Michael Minella
2016-08-22 12:55:31 -05:00
parent 2bfc4780c7
commit 17e4250586
40 changed files with 307 additions and 114 deletions

View File

@@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorFactoryBean;
import org.springframework.cloud.task.repository.TaskExplorer;
@@ -49,6 +50,7 @@ import org.springframework.util.CollectionUtils;
*/
@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties(TaskProperties.class)
public class SimpleTaskConfiguration {
protected static final Log logger = LogFactory.getLog(SimpleTaskConfiguration.class);
@@ -62,6 +64,9 @@ public class SimpleTaskConfiguration {
@Autowired(required = false)
private ApplicationArguments applicationArguments;
@Autowired
private TaskProperties taskProperties;
private boolean initialized = false;
private TaskRepository taskRepository;
@@ -137,7 +142,7 @@ public class SimpleTaskConfiguration {
this.taskExplorer = taskConfigurer.getTaskExplorer();
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, taskNameResolver(),
this.applicationArguments, taskExplorer);
this.applicationArguments, taskExplorer, taskProperties);
initialized = true;
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties available to configure the task.
*
* @author Glenn Renfro
*/
@ConfigurationProperties(prefix = "spring.cloud.task")
public class TaskProperties {
/**
* An id that can be associated with a task.
*/
private String externalExecutionId;
/**
* An id that will be used by the task when updating the task execution.
*/
private Integer executionid;
/**
* When set to true the context is closed at the end of the task. Else
* the context remains open.
*/
private Boolean closecontextEnable = true;
public String getExternalExecutionId() {
return externalExecutionId;
}
public void setExternalExecutionId(String externalExecutionId) {
this.externalExecutionId = externalExecutionId;
}
public Integer getExecutionid() {
return executionid;
}
public void setExecutionid(Integer executionid) {
this.executionid = executionid;
}
public Boolean getClosecontextEnable() {
return closecontextEnable;
}
public void setClosecontextEnable(Boolean closecontextEnable) {
this.closecontextEnable = closecontextEnable;
}
}

View File

@@ -34,6 +34,7 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ExitCodeEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskNameResolver;
@@ -80,6 +81,8 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private TaskExecution taskExecution;
private TaskProperties taskProperties;
private boolean started = false;
private boolean finished = false;
@@ -92,26 +95,23 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private ExitCodeEvent exitCodeEvent;
@Value("${spring.cloud.task.closecontext.enable:true}")
private Boolean closeContext;
@Value("${spring.cloud.task.executionid:}")
private Integer taskExecutionId;
/**
* @param taskRepository The repository to record executions in.
*/
public TaskLifecycleListener(TaskRepository taskRepository,
TaskNameResolver taskNameResolver,
ApplicationArguments applicationArguments, TaskExplorer taskExplorer) {
ApplicationArguments applicationArguments, TaskExplorer taskExplorer,
TaskProperties taskProperties) {
Assert.notNull(taskRepository, "A taskRepository is required");
Assert.notNull(taskNameResolver, "A taskNameResolver is required");
Assert.notNull(taskExplorer, "A taskExplorer is required");
Assert.notNull(taskProperties, "TaskProperties is required");
this.taskRepository = taskRepository;
this.taskNameResolver = taskNameResolver;
this.applicationArguments = applicationArguments;
this.taskExplorer = taskExplorer;
this.taskProperties = taskProperties;
}
/**
@@ -176,7 +176,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
this.finished = true;
if(this.closeContext && this.context.isActive()) {
if(taskProperties.getClosecontextEnable() && this.context.isActive()) {
this.context.close();
}
@@ -195,17 +195,17 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
if(this.applicationArguments != null) {
args = Arrays.asList(this.applicationArguments.getSourceArgs());
}
if(this.taskExecutionId != null) {
TaskExecution taskExecution = taskExplorer.getTaskExecution(this.taskExecutionId);
Assert.notNull(taskExecution, String.format("Invalid TaskExecution, ID %s not found", this.taskExecutionId));
if(taskProperties.getExecutionid() != null) {
TaskExecution taskExecution = taskExplorer.getTaskExecution(taskProperties.getExecutionid());
Assert.notNull(taskExecution, String.format("Invalid TaskExecution, ID %s not found", taskProperties.getExecutionid()));
Assert.isNull(taskExecution.getEndTime(), String.format(
"Invalid TaskExecution, ID %s task is already complete", this.taskExecutionId));
this.taskExecution = this.taskRepository.startTaskExecution(this.taskExecutionId,
this.taskNameResolver.getTaskName(), new Date(), args);
"Invalid TaskExecution, ID %s task is already complete", taskProperties.getExecutionid()));
this.taskExecution = this.taskRepository.startTaskExecution(taskProperties.getExecutionid(),
this.taskNameResolver.getTaskName(), new Date(), args, taskProperties.getExternalExecutionId());
}
else {
this.taskExecution = this.taskRepository.createTaskExecution(
this.taskNameResolver.getTaskName(), new Date(), args);
this.taskNameResolver.getTaskName(), new Date(), args, taskProperties.getExternalExecutionId());
}
}
else {
@@ -254,7 +254,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
taskExecution.getExitCode(), taskExecution.getTaskName(), startTime,
endTime,taskExecution.getExitMessage(),
Collections.unmodifiableList(taskExecution.getArguments()),
taskExecution.getErrorMessage());
taskExecution.getErrorMessage(), taskExecution.getExternalExecutionId());
}
@Override

View File

@@ -21,6 +21,7 @@ import java.util.Date;
import java.util.List;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Represents the state of the Task for each execution.
@@ -60,6 +61,13 @@ public class TaskExecution {
*/
private String exitMessage;
/**
* Id assigned to the task by the platform.
*
* @since 1.1.0
*/
private String externalExecutionId;
/**
* Error information available upon the failure of a task
*
@@ -79,7 +87,7 @@ public class TaskExecution {
public TaskExecution(long executionId, Integer exitCode, String taskName,
Date startTime, Date endTime,
String exitMessage, List<String> arguments,
String errorMessage) {
String errorMessage, String externalExecutionId) {
Assert.notNull(arguments, "arguments must not be null");
this.executionId = executionId;
@@ -90,6 +98,7 @@ public class TaskExecution {
this.startTime = (startTime != null) ? (Date)startTime.clone() : null;
this.endTime = (endTime != null) ? (Date)endTime.clone() : null;
this.errorMessage = errorMessage;
this.externalExecutionId = externalExecutionId;
}
public long getExecutionId() {
@@ -152,6 +161,14 @@ public class TaskExecution {
this.errorMessage = errorMessage;
}
public String getExternalExecutionId() {
return externalExecutionId;
}
public void setExternalExecutionId(String externalExecutionId) {
this.externalExecutionId = externalExecutionId;
}
@Override
public String toString() {
return "TaskExecution{" +

View File

@@ -62,11 +62,12 @@ public interface TaskRepository {
* @param taskName the name that associated with the task execution.
* @param startTime the time task began.
* @param arguments list of key/value pairs that configure the task.
* @param externalExecutionId id assigned to the task by the platform.
* @return the initial {@link TaskExecution}
*/
@Transactional
TaskExecution createTaskExecution(String taskName,
Date startTime,List<String> arguments);
Date startTime,List<String> arguments, String externalExecutionId);
/**
* Creates an empty TaskExecution with just an id provided. This is intended to be
@@ -85,9 +86,11 @@ public interface TaskRepository {
* @param taskName the name that associated with the task execution.
* @param startTime the time task began.
* @param arguments list of key/value pairs that configure the task.
* @param externalExecutionId id assigned to the task by the platform.
* @return
*/
@Transactional
TaskExecution startTaskExecution(long executionid, String taskName,
Date startTime,List<String> arguments);
Date startTime,List<String> arguments, String externalExecutionId);
}

View File

@@ -58,7 +58,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
public static final String SELECT_CLAUSE = "TASK_EXECUTION_ID, "
+ "START_TIME, END_TIME, TASK_NAME, EXIT_CODE, "
+ "EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED ";
+ "EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID ";
public static final String FROM_CLAUSE = "%PREFIX%EXECUTION";
@@ -68,14 +68,14 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
public static final String TASK_NAME_WHERE_CLAUSE = "where TASK_NAME = ? ";
private static final String SAVE_TASK_EXECUTION = "INSERT into %PREFIX%EXECUTION"
+ "(TASK_EXECUTION_ID, START_TIME, TASK_NAME, LAST_UPDATED)"
+ "values (?, ?, ?, ?)";
+ "(TASK_EXECUTION_ID, START_TIME, TASK_NAME, LAST_UPDATED, EXTERNAL_EXECUTION_ID)"
+ "values (?, ?, ?, ?, ?)";
private static final String CREATE_TASK_ARGUMENT = "INSERT into "
+ "%PREFIX%EXECUTION_PARAMS(TASK_EXECUTION_ID, TASK_PARAM ) values (?, ?)";
private static final String START_TASK_EXECUTION = "UPDATE %PREFIX%EXECUTION set "
+ "START_TIME = ?, TASK_NAME = ?, LAST_UPDATED = ? where TASK_EXECUTION_ID = ?";
+ "START_TIME = ?, TASK_NAME = ?, LAST_UPDATED = ?, EXTERNAL_EXECUTION_ID = ? where TASK_EXECUTION_ID = ?";
private static final String CHECK_TASK_EXECUTION_EXISTS = "SELECT COUNT(*) FROM "
+ "%PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = ?";
@@ -86,7 +86,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
private static final String GET_EXECUTION_BY_ID = "SELECT TASK_EXECUTION_ID, " +
"START_TIME, END_TIME, TASK_NAME, EXIT_CODE, "
+ "EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED "
+ "EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID "
+ "from %PREFIX%EXECUTION where TASK_EXECUTION_ID = ?";
private static final String FIND_ARGUMENT_FROM_ID = "SELECT TASK_EXECUTION_ID, "
@@ -129,31 +129,32 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
@Override
public TaskExecution createTaskExecution(String taskName,
Date startTime, List<String> arguments) {
Date startTime, List<String> arguments, String externalExecutionId) {
long nextExecutionId = getNextExecutionId();
TaskExecution taskExecution = new TaskExecution(nextExecutionId, null, taskName,
startTime, null, null, arguments, null);
startTime, null, null, arguments, null, externalExecutionId);
Object[] queryParameters = new Object[]{ nextExecutionId, startTime, taskName, new Date()};
Object[] queryParameters = new Object[]{ nextExecutionId, startTime, taskName, new Date(), externalExecutionId};
jdbcTemplate.update(
getQuery(SAVE_TASK_EXECUTION),
queryParameters,
new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP });
new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR });
insertTaskArguments(nextExecutionId, arguments);
return taskExecution;
}
@Override
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments) {
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments,
String externalExecutionId) {
TaskExecution taskExecution = new TaskExecution(executionId, null, taskName,
startTime, null, null, arguments, null);
startTime, null, null, arguments, null, externalExecutionId);
Object[] queryParameters = new Object[]{ startTime, taskName, new Date(), executionId};
Object[] queryParameters = new Object[]{ startTime, taskName, new Date(), externalExecutionId, executionId};
jdbcTemplate.update(
getQuery(START_TASK_EXECUTION),
queryParameters,
new int[]{ Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, Types.BIGINT });
new int[]{ Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.BIGINT });
insertTaskArguments(executionId, arguments);
return taskExecution;
}
@@ -399,7 +400,8 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
rs.getTimestamp("END_TIME"),
rs.getString("EXIT_MESSAGE"),
getTaskArguments(id),
rs.getString("ERROR_MESSAGE"));
rs.getString("ERROR_MESSAGE"),
rs.getString("EXTERNAL_EXECUTION_ID"));
}
private Integer getNullableExitCode(ResultSet rs) throws SQLException {

View File

@@ -53,21 +53,23 @@ public class MapTaskExecutionDao implements TaskExecutionDao {
@Override
public TaskExecution createTaskExecution(String taskName,
Date startTime, List<String> arguments) {
Date startTime, List<String> arguments, String externalExecutionId) {
long taskExecutionId = getNextExecutionId();
TaskExecution taskExecution = new TaskExecution(taskExecutionId, null, taskName,
startTime, null, null, arguments, null);
startTime, null, null, arguments, null, externalExecutionId);
taskExecutions.put(taskExecutionId, taskExecution);
return taskExecution;
}
@Override
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments) {
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments,
String externalExecutionid) {
TaskExecution taskExecution= taskExecutions.get(executionId);
taskExecution.setTaskName(taskName);
taskExecution.setStartTime(startTime);
taskExecution.setArguments(arguments);
taskExecution.setExternalExecutionId(externalExecutionid);
return taskExecution;
}

View File

@@ -37,10 +37,11 @@ public interface TaskExecutionDao {
* @param taskName the name that associated with the task execution.
* @param startTime the time task began.
* @param arguments list of key/value pairs that configure the task.
* @param externalExecutionId id assigned to the task by the platform
* @return A fully qualified {@link TaskExecution} instance.
*/
TaskExecution createTaskExecution( String taskName,
Date startTime, List<String> arguments);
Date startTime, List<String> arguments, String externalExecutionId);
/**
* Update and existing {@link TaskExecution} to mark it as started.
@@ -49,10 +50,11 @@ public interface TaskExecutionDao {
* @param taskName the name that associated with the task execution.
* @param startTime the time task began.
* @param arguments list of key/value pairs that configure the task.
* @param externalExecutionId id assigned to the task by the platform
* @since 1.1.0
*/
TaskExecution startTaskExecution(long executionId, String taskName,
Date startTime, List<String> arguments);
Date startTime, List<String> arguments, String externalExecutionId);
/**
* Update and existing {@link TaskExecution} to mark it as completed.

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.task.repository.support;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -101,11 +102,11 @@ public class SimpleTaskRepository implements TaskRepository {
@Override
public TaskExecution createTaskExecution(String taskName,
Date startTime,List<String> arguments) {
Date startTime,List<String> arguments, String externalExecutionId) {
initialize();
validateCreateInformation(startTime, taskName);
TaskExecution taskExecution =
taskExecutionDao.createTaskExecution(taskName, startTime, arguments);
taskExecutionDao.createTaskExecution(taskName, startTime, arguments, externalExecutionId);
logger.debug("Creating: " + taskExecution.toString());
return taskExecution;
}
@@ -114,16 +115,18 @@ public class SimpleTaskRepository implements TaskRepository {
public TaskExecution createTaskExecution() {
initialize();
TaskExecution taskExecution =
taskExecutionDao.createTaskExecution(null, null, new ArrayList<String>(0));
taskExecutionDao.createTaskExecution(null, null,
Collections.<String>emptyList(), null);
logger.debug("Creating: " + taskExecution.toString());
return taskExecution;
}
@Override
public TaskExecution startTaskExecution(long executionid, String taskName, Date startTime, List<String> arguments) {
public TaskExecution startTaskExecution(long executionid, String taskName, Date startTime, List<String> arguments,
String externalExecutionId) {
initialize();
TaskExecution taskExecution =
taskExecutionDao.startTaskExecution(executionid, taskName, startTime, arguments);
taskExecutionDao.startTaskExecution(executionid, taskName, startTime, arguments, externalExecutionId);
logger.debug("Starting: " + taskExecution.toString());
return taskExecution;
}

View File

@@ -1 +1,2 @@
alter table task_execution add ERROR_MESSAGE VARCHAR(2500);
alter table task_execution add EXTERNAL_EXECUTION_ID VARCHAR(255);

View File

@@ -1 +1,2 @@
alter table task_execution add column ERROR_MESSAGE VARCHAR(2500);
alter table task_execution add EXTERNAL_EXECUTION_ID VARCHAR(255);

View File

@@ -1 +1,3 @@
alter table task_execution add column ERROR_MESSAGE VARCHAR(2500);
alter table task_execution add EXTERNAL_EXECUTION_ID VARCHAR(255);

View File

@@ -1 +1,3 @@
alter table task_execution add ERROR_MESSAGE varchar2(2500);
alter table task_execution add EXTERNAL_EXECUTION_ID varchar2(255);

View File

@@ -1 +1,2 @@
alter table task_execution add column ERROR_MESSAGE VARCHAR(2500);
alter table task_execution add EXTERNAL_EXECUTION_ID VARCHAR(255);

View File

@@ -1 +1,2 @@
alter table task_execution add ERROR_MESSAGE VARCHAR(2500);
alter table task_execution add EXTERNAL_EXECUTION_ID VARCHAR(255);

View File

@@ -7,7 +7,8 @@ CREATE TABLE TASK_EXECUTION (
EXIT_CODE INTEGER ,
EXIT_MESSAGE VARCHAR(2500) ,
ERROR_MESSAGE VARCHAR(2500) ,
LAST_UPDATED TIMESTAMP
LAST_UPDATED TIMESTAMP,
EXTERNAL_EXECUTION_ID VARCHAR(255)
);
CREATE TABLE TASK_EXECUTION_PARAMS (

View File

@@ -7,7 +7,8 @@ CREATE TABLE TASK_EXECUTION (
EXIT_CODE INTEGER ,
EXIT_MESSAGE VARCHAR(2500) ,
ERROR_MESSAGE VARCHAR(2500) ,
LAST_UPDATED TIMESTAMP
LAST_UPDATED TIMESTAMP,
EXTERNAL_EXECUTION_ID VARCHAR(255)
);
CREATE TABLE TASK_EXECUTION_PARAMS (

View File

@@ -7,7 +7,8 @@ CREATE TABLE TASK_EXECUTION (
EXIT_CODE INTEGER ,
EXIT_MESSAGE VARCHAR(2500) ,
ERROR_MESSAGE VARCHAR(2500) ,
LAST_UPDATED TIMESTAMP
LAST_UPDATED TIMESTAMP,
EXTERNAL_EXECUTION_ID VARCHAR(255)
) ENGINE=InnoDB;
CREATE TABLE TASK_EXECUTION_PARAMS (
@@ -18,8 +19,8 @@ CREATE TABLE TASK_EXECUTION_PARAMS (
) ENGINE=InnoDB;
CREATE TABLE TASK_TASK_BATCH (
TASK_EXECUTION_ID BIGINT NOT NULL ,
JOB_EXECUTION_ID BIGINT NOT NULL ,
TASK_EXECUTION_ID BIGINT NOT NULL ,
JOB_EXECUTION_ID BIGINT NOT NULL ,
constraint TASK_EXEC_BATCH_FK foreign key (TASK_EXECUTION_ID)
references TASK_EXECUTION(TASK_EXECUTION_ID)
) ENGINE=InnoDB;

View File

@@ -7,7 +7,8 @@ CREATE TABLE TASK_EXECUTION (
EXIT_CODE INTEGER ,
EXIT_MESSAGE VARCHAR2(2500) ,
ERROR_MESSAGE VARCHAR2(2500) ,
LAST_UPDATED TIMESTAMP
LAST_UPDATED TIMESTAMP,
EXTERNAL_EXECUTION_ID VARCHAR2(255)
);
CREATE TABLE TASK_EXECUTION_PARAMS (

View File

@@ -7,7 +7,8 @@ CREATE TABLE TASK_EXECUTION (
EXIT_CODE INTEGER ,
EXIT_MESSAGE VARCHAR(2500) ,
ERROR_MESSAGE VARCHAR(2500) ,
LAST_UPDATED TIMESTAMP
LAST_UPDATED TIMESTAMP ,
EXTERNAL_EXECUTION_ID VARCHAR(255)
);
CREATE TABLE TASK_EXECUTION_PARAMS (

View File

@@ -6,7 +6,8 @@ CREATE TABLE TASK_EXECUTION (
EXIT_CODE INTEGER ,
EXIT_MESSAGE VARCHAR(2500) ,
ERROR_MESSAGE VARCHAR(2500) ,
LAST_UPDATED DATETIME
LAST_UPDATED DATETIME ,
EXTERNAL_EXECUTION_ID VARCHAR(255)
);
CREATE TABLE TASK_EXECUTION_PARAMS (