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
This commit is contained in:
Michael Minella
2016-08-22 12:55:31 -05:00
parent bf0c27dd1a
commit 441bbfe492
21 changed files with 619 additions and 36 deletions

View File

@@ -136,7 +136,8 @@ public class SimpleTaskConfiguration {
this.platformTransactionManager = taskConfigurer.getTransactionManager();
this.taskExplorer = taskConfigurer.getTaskExplorer();
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, taskNameResolver(), this.applicationArguments);
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, taskNameResolver(),
this.applicationArguments, taskExplorer);
initialized = true;
}

View File

@@ -35,6 +35,7 @@ import org.springframework.boot.ExitCodeEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.context.ApplicationEvent;
@@ -75,6 +76,8 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private final TaskRepository taskRepository;
private final TaskExplorer taskExplorer;
private TaskExecution taskExecution;
private boolean started = false;
@@ -92,18 +95,23 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
@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) {
ApplicationArguments applicationArguments, TaskExplorer taskExplorer) {
Assert.notNull(taskRepository, "A taskRepository is required");
Assert.notNull(taskNameResolver, "A taskNameResolver is required");
Assert.notNull(taskExplorer, "A taskExplorer is required");
this.taskRepository = taskRepository;
this.taskNameResolver = taskNameResolver;
this.applicationArguments = applicationArguments;
this.taskExplorer = taskExplorer;
}
/**
@@ -187,9 +195,18 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
if(this.applicationArguments != null) {
args = Arrays.asList(this.applicationArguments.getSourceArgs());
}
this.taskExecution = this.taskRepository.createTaskExecution(
this.taskNameResolver.getTaskName(), new Date(), args);
if(this.taskExecutionId != null) {
TaskExecution taskExecution = taskExplorer.getTaskExecution(this.taskExecutionId);
Assert.notNull(taskExecution, String.format("Invalid TaskExecution, ID %s not found", this.taskExecutionId));
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);
}
else {
this.taskExecution = this.taskRepository.createTaskExecution(
this.taskNameResolver.getTaskName(), new Date(), args);
}
}
else {
logger.error("Multiple start events have been received. The first one was " +

View File

@@ -82,13 +82,12 @@ public class TaskExecution {
String errorMessage) {
Assert.notNull(arguments, "arguments must not be null");
Assert.notNull(startTime, "startTime must not be null");
this.executionId = executionId;
this.exitCode = exitCode;
this.taskName = taskName;
this.exitMessage = exitMessage;
this.arguments = new ArrayList<>(arguments);
this.startTime = (Date)startTime.clone();
this.startTime = (startTime != null) ? (Date)startTime.clone() : null;
this.endTime = (endTime != null) ? (Date)endTime.clone() : null;
this.errorMessage = errorMessage;
}

View File

@@ -68,4 +68,26 @@ public interface TaskRepository {
TaskExecution createTaskExecution(String taskName,
Date startTime,List<String> arguments);
/**
* Creates an empty TaskExecution with just an id provided. This is intended to be
* utilized in systems where the request of launching a task is separate from the
* actual start of a task (the underlying system may need to deploy the task prior to
* launching, etc).
*
* @return the initial {@link TaskExecution}
*/
@Transactional
TaskExecution createTaskExecution();
/**
* Notifies the repository that a taskExecution has has started.
* @param executionid to the task execution to be updated.
* @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.
* @return
*/
@Transactional
TaskExecution startTaskExecution(long executionid, String taskName,
Date startTime,List<String> arguments);
}

View File

@@ -74,6 +74,9 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
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 = ?";
private static final String CHECK_TASK_EXECUTION_EXISTS = "SELECT COUNT(*) FROM "
+ "%PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = ?";
@@ -127,16 +130,31 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
@Override
public TaskExecution createTaskExecution(String taskName,
Date startTime, List<String> arguments) {
long taskExecutionId = getNextExecutionId();
TaskExecution taskExecution = new TaskExecution(taskExecutionId, null, taskName,
long nextExecutionId = getNextExecutionId();
TaskExecution taskExecution = new TaskExecution(nextExecutionId, null, taskName,
startTime, null, null, arguments, null);
Object[] queryParameters = new Object[]{ taskExecutionId, startTime, taskName, new Date()};
Object[] queryParameters = new Object[]{ nextExecutionId, startTime, taskName, new Date()};
jdbcTemplate.update(
getQuery(SAVE_TASK_EXECUTION),
queryParameters,
new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP });
insertTaskArguments(taskExecutionId, arguments);
insertTaskArguments(nextExecutionId, arguments);
return taskExecution;
}
@Override
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments) {
TaskExecution taskExecution = new TaskExecution(executionId, null, taskName,
startTime, null, null, arguments, null);
Object[] queryParameters = new Object[]{ startTime, taskName, new Date(), executionId};
jdbcTemplate.update(
getQuery(START_TASK_EXECUTION),
queryParameters,
new int[]{ Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, Types.BIGINT });
insertTaskArguments(executionId, arguments);
return taskExecution;
}
@@ -146,7 +164,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
// Check if given TaskExecution's Id already exists, if none is found
// it is invalid and an exception should be thrown.
if (jdbcTemplate.queryForObject(getQuery(CHECK_TASK_EXECUTION_EXISTS), Integer.class,
new Object[]{ taskExecutionId}) != 1) {
taskExecutionId) != 1) {
throw new IllegalStateException("Invalid TaskExecution, ID " + taskExecutionId + " not found.");
}

View File

@@ -61,8 +61,23 @@ public class MapTaskExecutionDao implements TaskExecutionDao {
return taskExecution;
}
@Override
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments) {
TaskExecution taskExecution= taskExecutions.get(executionId);
taskExecution.setTaskName(taskName);
taskExecution.setStartTime(startTime);
taskExecution.setArguments(arguments);
return taskExecution;
}
@Override
public void completeTaskExecution(long executionId, Integer exitCode, Date endTime, String exitMessage, String errorMessage) {
if(!this.taskExecutions.containsKey(executionId)) {
throw new IllegalStateException("Invalid TaskExecution, ID " + executionId + " not found.");
}
TaskExecution taskExecution= taskExecutions.get(executionId);
taskExecution.setEndTime(endTime);
taskExecution.setExitCode(exitCode);

View File

@@ -43,7 +43,19 @@ public interface TaskExecutionDao {
Date startTime, List<String> arguments);
/**
* Update and existing {@link TaskExecution}.
* Update and existing {@link TaskExecution} to mark it as started.
*
* @param executionId the id of the taskExecution to be updated.
* @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.
* @since 1.1.0
*/
TaskExecution startTaskExecution(long executionId, String taskName,
Date startTime, List<String> arguments);
/**
* Update and existing {@link TaskExecution} to mark it as completed.
*
* @param executionId the id of the taskExecution to be updated.
* @param exitCode the status of the task upon completion.

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.task.repository.support;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -109,6 +110,24 @@ public class SimpleTaskRepository implements TaskRepository {
return taskExecution;
}
@Override
public TaskExecution createTaskExecution() {
initialize();
TaskExecution taskExecution =
taskExecutionDao.createTaskExecution(null, null, new ArrayList<String>(0));
logger.debug("Creating: " + taskExecution.toString());
return taskExecution;
}
@Override
public TaskExecution startTaskExecution(long executionid, String taskName, Date startTime, List<String> arguments) {
initialize();
TaskExecution taskExecution =
taskExecutionDao.startTaskExecution(executionid, taskName, startTime, arguments);
logger.debug("Starting: " + taskExecution.toString());
return taskExecution;
}
/**
* Retrieves the taskExecutionDao associated with this repository.
* @return the taskExecutionDao