From acaaad89f3767cf2c93415a65c2127d7615ac986 Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Fri, 6 Jan 2017 09:42:10 -0500 Subject: [PATCH] Adds support for Task Execution Parent Ids resolves #261 --- .../partition/DeployerPartitionHandler.java | 7 ++- .../DeployerPartitionHandlerTests.java | 38 ++++++++++++- .../task/configuration/TaskProperties.java | 22 ++++++-- .../task/listener/TaskLifecycleListener.java | 7 ++- .../cloud/task/repository/TaskExecution.java | 34 ++++++++++-- .../cloud/task/repository/TaskRepository.java | 17 ++++++ .../repository/dao/JdbcTaskExecutionDao.java | 55 +++++++++++++++---- .../repository/dao/MapTaskExecutionDao.java | 18 +++++- .../task/repository/dao/TaskExecutionDao.java | 30 ++++++++++ .../support/SimpleTaskRepository.java | 24 +++++--- .../migration/{ => 1.1.x}/migration-h2.sql | 0 .../{ => 1.1.x}/migration-hsqldb.sql | 0 .../migration/{ => 1.1.x}/migration-mysql.sql | 0 .../{ => 1.1.x}/migration-oracle.sql | 0 .../{ => 1.1.x}/migration-postgresql.sql | 0 .../{ => 1.1.x}/migration-sqlserver.sql | 0 .../task/migration/1.2.x/migration-h2.sql | 1 + .../task/migration/1.2.x/migration-hsqldb.sql | 1 + .../task/migration/1.2.x/migration-mysql.sql | 1 + .../task/migration/1.2.x/migration-oracle.sql | 1 + .../migration/1.2.x/migration-postgresql.sql | 1 + .../migration/1.2.x/migration-sqlserver.sql | 1 + .../springframework/cloud/task/schema-db2.sql | 3 +- .../springframework/cloud/task/schema-h2.sql | 3 +- .../cloud/task/schema-hsqldb.sql | 3 +- .../cloud/task/schema-mysql.sql | 3 +- .../cloud/task/schema-oracle10g.sql | 3 +- .../cloud/task/schema-postgresql.sql | 3 +- .../cloud/task/schema-sqlserver.sql | 3 +- .../listener/TaskLifecycleListenerTests.java | 24 +++++++- .../FindAllPagingQueryProviderTests.java | 18 +++--- .../WhereClausePagingQueryProviderTests.java | 18 +++--- .../SimpleTaskRepositoryJdbcTests.java | 19 +++++++ .../support/SimpleTaskRepositoryMapTests.java | 21 ++++++- .../cloud/task/util/TaskExecutionCreator.java | 5 +- .../cloud/task/util/TestVerifierUtils.java | 3 + .../src/main/asciidoc/features.adoc | 13 +++++ 37 files changed, 336 insertions(+), 64 deletions(-) rename spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/{ => 1.1.x}/migration-h2.sql (100%) rename spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/{ => 1.1.x}/migration-hsqldb.sql (100%) rename spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/{ => 1.1.x}/migration-mysql.sql (100%) rename spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/{ => 1.1.x}/migration-oracle.sql (100%) rename spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/{ => 1.1.x}/migration-postgresql.sql (100%) rename spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/{ => 1.1.x}/migration-sqlserver.sql (100%) create mode 100644 spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-h2.sql create mode 100644 spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-hsqldb.sql create mode 100644 spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-mysql.sql create mode 100644 spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-oracle.sql create mode 100644 spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-postgresql.sql create mode 100644 spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-sqlserver.sql diff --git a/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandler.java b/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandler.java index f4d09c8d..e7eaf649 100644 --- a/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandler.java +++ b/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 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. @@ -76,6 +76,9 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw public static final String SPRING_CLOUD_TASK_STEP_NAME = "spring.cloud.task.step-name"; + public static final String SPRING_CLOUD_TASK_PARENT_EXECUTION_ID = + "spring.cloud.task.parentExecutionId"; + public static final String SPRING_CLOUD_TASK_NAME = "spring.cloud.task.name"; private int maxWorkers = -1; @@ -268,6 +271,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw taskExecution.getTaskName(), workerStepExecution.getJobExecution().getJobInstance().getJobName(), workerStepExecution.getStepName()))); + arguments.add(formatArgument(SPRING_CLOUD_TASK_PARENT_EXECUTION_ID, + String.valueOf(taskExecution.getExecutionId()))); copyContext = new ExecutionContext(workerStepExecution.getExecutionContext()); diff --git a/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandlerTests.java b/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandlerTests.java index acfc1c21..d9432998 100644 --- a/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandlerTests.java +++ b/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/partition/DeployerPartitionHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 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. @@ -15,6 +15,7 @@ */ package org.springframework.cloud.task.batch.partition; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -160,6 +161,41 @@ public class DeployerPartitionHandlerTests { assertEquals("step1:partition1", resultStepExecution.getStepName()); } + @Test + public void testParentExecutionId() throws Exception { + + StepExecution masterStepExecution = createMasterStepExecution(); + JobExecution jobExecution = masterStepExecution.getJobExecution(); + + StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L); + StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED); + + DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1"); + handler.setEnvironment(this.environment); + + TaskExecution taskExecution = new TaskExecution(55, null, null, null, + null, null, new ArrayList(), null, null); + + taskExecution.setTaskName("partitionedJobTask"); + + Set stepExecutions = new HashSet<>(); + stepExecutions.add(workerStepExecutionStart); + when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions); + + when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish); + + handler.afterPropertiesSet(); + + handler.beforeTask(taskExecution); + + handler.handle(this.splitter, masterStepExecution); + + verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture()); + + AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue(); + assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID, "55"))); + } + @Test public void testThreePartitions() throws Exception { diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskProperties.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskProperties.java index 06933c8a..41616ec5 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskProperties.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 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. @@ -37,7 +37,13 @@ public class TaskProperties { /** * An id that will be used by the task when updating the task execution. */ - private Integer executionid; + private Long executionid; + + /** + * The id of the parent task execution id that launched this task execution. + * Defaults to null if task execution had no parent. + */ + private Long parentExecutionId; /** * The prefix to append to the table names created by Spring Cloud Task. @@ -58,11 +64,11 @@ public class TaskProperties { this.externalExecutionId = externalExecutionId; } - public Integer getExecutionid() { + public Long getExecutionid() { return executionid; } - public void setExecutionid(Integer executionid) { + public void setExecutionid(Long executionid) { this.executionid = executionid; } @@ -81,4 +87,12 @@ public class TaskProperties { public void setTablePrefix(String tablePrefix) { this.tablePrefix = tablePrefix; } + + public Long getParentExecutionId() { + return parentExecutionId; + } + + public void setParentExecutionId(Long parentExecutionId) { + this.parentExecutionId = parentExecutionId; + } } 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 446be4b4..26551926 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 the original author or authors. + * Copyright 2016-2017 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. @@ -201,7 +201,9 @@ public class TaskLifecycleListener implements ApplicationListener arguments, - String errorMessage, String externalExecutionId) { + String errorMessage, String externalExecutionId, + Long parentExecutionId) { Assert.notNull(arguments, "arguments must not be null"); this.executionId = executionId; @@ -99,6 +105,16 @@ public class TaskExecution { this.endTime = (endTime != null) ? (Date)endTime.clone() : null; this.errorMessage = errorMessage; this.externalExecutionId = externalExecutionId; + this.parentExecutionId = parentExecutionId; + } + + public TaskExecution(long executionId, Integer exitCode, String taskName, + Date startTime, Date endTime, + String exitMessage, List arguments, + String errorMessage, String externalExecutionId) { + + this(executionId, exitCode, taskName, startTime, endTime, exitMessage, + arguments, errorMessage,externalExecutionId, null); } public long getExecutionId() { @@ -169,16 +185,26 @@ public class TaskExecution { this.externalExecutionId = externalExecutionId; } + public Long getParentExecutionId() { + return parentExecutionId; + } + + public void setParentExecutionId(Long parentExecutionId) { + this.parentExecutionId = parentExecutionId; + } + @Override public String toString() { return "TaskExecution{" + "executionId=" + executionId + + ", parentExecutionId=" + parentExecutionId + ", exitCode=" + exitCode + ", taskName='" + taskName + '\'' + ", startTime=" + startTime + ", endTime=" + endTime + ", exitMessage='" + exitMessage + '\'' + - ", errorMessage='" + errorMessage + "\'" + + ", externalExecutionId='" + externalExecutionId + '\'' + + ", errorMessage='" + errorMessage + '\'' + ", arguments=" + arguments + '}'; } diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/TaskRepository.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/TaskRepository.java index 00fb0088..d15cb36e 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/TaskRepository.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/TaskRepository.java @@ -104,4 +104,21 @@ public interface TaskRepository { @Transactional void updateExternalExecutionId(long executionid, String externalExecutionId); + + /** + * 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. + * @param externalExecutionId id assigned to the task by the platform. + * @param parentExecutionId the parent task execution id. + + * @return + */ + @Transactional + TaskExecution startTaskExecution(long executionid, String taskName, + Date startTime,List arguments, String externalExecutionId, + Long parentExecutionId); + } diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDao.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDao.java index 44f45f29..ef5c0ae0 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDao.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDao.java @@ -61,7 +61,8 @@ 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, EXTERNAL_EXECUTION_ID "; + + "EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, " + + "EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID "; public static final String FROM_CLAUSE = "%PREFIX%EXECUTION"; @@ -71,14 +72,16 @@ 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, EXTERNAL_EXECUTION_ID)" - + "values (?, ?, ?, ?, ?)"; + + "(TASK_EXECUTION_ID, START_TIME, TASK_NAME, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_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 = ?, EXTERNAL_EXECUTION_ID = ? where TASK_EXECUTION_ID = ?"; + + "START_TIME = ?, TASK_NAME = ?, LAST_UPDATED = ?, " + + "EXTERNAL_EXECUTION_ID = ?, PARENT_EXECUTION_ID = ? " + + "where TASK_EXECUTION_ID = ?"; private static final String CHECK_TASK_EXECUTION_EXISTS = "SELECT COUNT(*) FROM " + "%PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = ?"; @@ -92,7 +95,8 @@ 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, EXTERNAL_EXECUTION_ID " + + "EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, " + + "PARENT_EXECUTION_ID " + "from %PREFIX%EXECUTION where TASK_EXECUTION_ID = ?"; private static final String FIND_ARGUMENT_FROM_ID = "SELECT TASK_EXECUTION_ID, " @@ -150,16 +154,27 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao { @Override public TaskExecution createTaskExecution(String taskName, Date startTime, List arguments, String externalExecutionId) { + return createTaskExecution(taskName, startTime, arguments, + externalExecutionId, null); + } + + @Override + public TaskExecution createTaskExecution(String taskName, Date startTime, + List arguments, String externalExecutionId, + Long parentExecutionId) { long nextExecutionId = getNextExecutionId(); TaskExecution taskExecution = new TaskExecution(nextExecutionId, null, taskName, startTime, null, null, arguments, null, externalExecutionId); - Object[] queryParameters = new Object[]{ nextExecutionId, startTime, taskName, new Date(), externalExecutionId}; + Object[] queryParameters = new Object[]{ nextExecutionId, startTime, + taskName, new Date(), externalExecutionId, + parentExecutionId}; jdbcTemplate.update( getQuery(SAVE_TASK_EXECUTION), queryParameters, - new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR }); + new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, + Types.TIMESTAMP, Types.VARCHAR, Types.BIGINT}); insertTaskArguments(nextExecutionId, arguments); return taskExecution; } @@ -167,14 +182,25 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao { @Override public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List arguments, String externalExecutionId) { - TaskExecution taskExecution = new TaskExecution(executionId, null, taskName, - startTime, null, null, arguments, null, externalExecutionId); + return startTaskExecution(executionId, taskName, startTime, arguments, + externalExecutionId, null); + } - Object[] queryParameters = new Object[]{ startTime, taskName, new Date(), externalExecutionId, executionId}; + @Override + public TaskExecution startTaskExecution(long executionId, String taskName, + Date startTime, List arguments, + String externalExecutionId, Long parentExecutionId) { + TaskExecution taskExecution = new TaskExecution(executionId, null, taskName, + startTime, null, null, arguments,null, externalExecutionId, parentExecutionId); + + Object[] queryParameters = new Object[]{ startTime, taskName, + new Date(), externalExecutionId, + parentExecutionId, executionId}; jdbcTemplate.update( getQuery(START_TASK_EXECUTION), queryParameters, - new int[]{ Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.BIGINT }); + new int[]{ Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, + Types.VARCHAR, Types.BIGINT, Types.BIGINT }); insertTaskArguments(executionId, arguments); return taskExecution; } @@ -442,6 +468,10 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao { @Override public TaskExecution mapRow(ResultSet rs, int rowNum) throws SQLException { long id = rs.getLong("TASK_EXECUTION_ID"); + Long parentExecutionId = rs.getLong("PARENT_EXECUTION_ID"); + if(rs.wasNull()) { + parentExecutionId = null; + } return new TaskExecution(id, getNullableExitCode(rs), rs.getString("TASK_NAME"), @@ -450,7 +480,8 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao { rs.getString("EXIT_MESSAGE"), getTaskArguments(id), rs.getString("ERROR_MESSAGE"), - rs.getString("EXTERNAL_EXECUTION_ID")); + rs.getString("EXTERNAL_EXECUTION_ID"), + parentExecutionId); } private Integer getNullableExitCode(ResultSet rs) throws SQLException { diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/MapTaskExecutionDao.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/MapTaskExecutionDao.java index 6ed47c2b..eeefd792 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/MapTaskExecutionDao.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/MapTaskExecutionDao.java @@ -55,9 +55,15 @@ public class MapTaskExecutionDao implements TaskExecutionDao { @Override public TaskExecution createTaskExecution(String taskName, Date startTime, List arguments, String externalExecutionId) { + return createTaskExecution(taskName, startTime, arguments, + externalExecutionId, null); + } + + @Override + public TaskExecution createTaskExecution(String taskName, Date startTime, List arguments, String externalExecutionId, Long parentExecutionId) { long taskExecutionId = getNextExecutionId(); TaskExecution taskExecution = new TaskExecution(taskExecutionId, null, taskName, - startTime, null, null, arguments, null, externalExecutionId); + startTime, null, null, arguments, null, externalExecutionId, parentExecutionId); taskExecutions.put(taskExecutionId, taskExecution); return taskExecution; } @@ -65,12 +71,20 @@ public class MapTaskExecutionDao implements TaskExecutionDao { @Override public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List arguments, String externalExecutionid) { - TaskExecution taskExecution= taskExecutions.get(executionId); + return startTaskExecution(executionId, taskName, startTime, arguments, + externalExecutionid, null); + } + @Override + public TaskExecution startTaskExecution(long executionId, String taskName, + Date startTime, List arguments, String externalExecutionid, + Long parentExecutionId) { + TaskExecution taskExecution= taskExecutions.get(executionId); taskExecution.setTaskName(taskName); taskExecution.setStartTime(startTime); taskExecution.setArguments(arguments); taskExecution.setExternalExecutionId(externalExecutionid); + taskExecution.setParentExecutionId(parentExecutionId); return taskExecution; } diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/TaskExecutionDao.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/TaskExecutionDao.java index 86cb0367..69f9626c 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/TaskExecutionDao.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/TaskExecutionDao.java @@ -43,6 +43,21 @@ public interface TaskExecutionDao { TaskExecution createTaskExecution( String taskName, Date startTime, List arguments, String externalExecutionId); + /** + * Save a new {@link TaskExecution}. + * + * @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 + * @param parentExecutionId the parent task execution id. + * @return A fully qualified {@link TaskExecution} instance. + * @since 1.2.0 + */ + TaskExecution createTaskExecution( String taskName, + Date startTime, List arguments, String externalExecutionId, + Long parentExecutionId); + /** * Update and existing {@link TaskExecution} to mark it as started. * @@ -56,6 +71,21 @@ public interface TaskExecutionDao { TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List arguments, String externalExecutionId); + /** + * 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. + * @param externalExecutionId id assigned to the task by the platform + * @param parentExecutionId the parent task execution id. + * @since 1.2.0 + */ + TaskExecution startTaskExecution(long executionId, String taskName, + Date startTime, List arguments, String externalExecutionId, + Long parentExecutionId); + /** * Update and existing {@link TaskExecution} to mark it as completed. * diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java index 360c02ae..d388fd3b 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java @@ -16,7 +16,6 @@ package org.springframework.cloud.task.repository.support; -import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; @@ -109,7 +108,8 @@ public class SimpleTaskRepository implements TaskRepository { taskExecution.getTaskName(), taskExecution.getStartTime(), taskExecution.getArguments(), - taskExecution.getExternalExecutionId()); + taskExecution.getExternalExecutionId(), + taskExecution.getParentExecutionId()); logger.debug("Creating: " + taskExecution.toString()); return daoTaskExecution; } @@ -127,11 +127,8 @@ public class SimpleTaskRepository implements TaskRepository { @Override public TaskExecution startTaskExecution(long executionid, String taskName, Date startTime, List arguments, String externalExecutionId) { - initialize(); - TaskExecution taskExecution = - taskExecutionDao.startTaskExecution(executionid, taskName, startTime, arguments, externalExecutionId); - logger.debug("Starting: " + taskExecution.toString()); - return taskExecution; + return startTaskExecution(executionid, taskName, startTime, arguments, + externalExecutionId, null); } @Override @@ -140,6 +137,19 @@ public class SimpleTaskRepository implements TaskRepository { taskExecutionDao.updateExternalExecutionId(executionid, externalExecutionId); } + @Override + public TaskExecution startTaskExecution(long executionid, String taskName, + Date startTime, List arguments, String externalExecutionId, + Long parentExecutionId) { + initialize(); + TaskExecution taskExecution = + taskExecutionDao.startTaskExecution(executionid, taskName, + startTime, arguments, externalExecutionId, + parentExecutionId); + logger.debug("Starting: " + taskExecution.toString()); + return taskExecution; + } + /** * Retrieves the taskExecutionDao associated with this repository. * @return the taskExecutionDao diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-h2.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-h2.sql similarity index 100% rename from spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-h2.sql rename to spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-h2.sql diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-hsqldb.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-hsqldb.sql similarity index 100% rename from spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-hsqldb.sql rename to spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-hsqldb.sql diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-mysql.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-mysql.sql similarity index 100% rename from spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-mysql.sql rename to spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-mysql.sql diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-oracle.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-oracle.sql similarity index 100% rename from spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-oracle.sql rename to spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-oracle.sql diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-postgresql.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-postgresql.sql similarity index 100% rename from spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-postgresql.sql rename to spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-postgresql.sql diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-sqlserver.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-sqlserver.sql similarity index 100% rename from spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/migration-sqlserver.sql rename to spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.1.x/migration-sqlserver.sql diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-h2.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-h2.sql new file mode 100644 index 00000000..914d277d --- /dev/null +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-h2.sql @@ -0,0 +1 @@ +alter table task_execution add PARENT_EXECUTION_ID BIGINT; diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-hsqldb.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-hsqldb.sql new file mode 100644 index 00000000..914d277d --- /dev/null +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-hsqldb.sql @@ -0,0 +1 @@ +alter table task_execution add PARENT_EXECUTION_ID BIGINT; diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-mysql.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-mysql.sql new file mode 100644 index 00000000..914d277d --- /dev/null +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-mysql.sql @@ -0,0 +1 @@ +alter table task_execution add PARENT_EXECUTION_ID BIGINT; diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-oracle.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-oracle.sql new file mode 100644 index 00000000..914d277d --- /dev/null +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-oracle.sql @@ -0,0 +1 @@ +alter table task_execution add PARENT_EXECUTION_ID BIGINT; diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-postgresql.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-postgresql.sql new file mode 100644 index 00000000..914d277d --- /dev/null +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-postgresql.sql @@ -0,0 +1 @@ +alter table task_execution add PARENT_EXECUTION_ID BIGINT; diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-sqlserver.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-sqlserver.sql new file mode 100644 index 00000000..914d277d --- /dev/null +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/migration/1.2.x/migration-sqlserver.sql @@ -0,0 +1 @@ +alter table task_execution add PARENT_EXECUTION_ID BIGINT; diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-db2.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-db2.sql index 7dd498e2..7e7834e5 100644 --- a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-db2.sql +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-db2.sql @@ -8,7 +8,8 @@ CREATE TABLE TASK_EXECUTION ( EXIT_MESSAGE VARCHAR(2500) , ERROR_MESSAGE VARCHAR(2500) , LAST_UPDATED TIMESTAMP, - EXTERNAL_EXECUTION_ID VARCHAR(255) + EXTERNAL_EXECUTION_ID VARCHAR(255), + PARENT_EXECUTION_ID BIGINT ); CREATE TABLE TASK_EXECUTION_PARAMS ( diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-h2.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-h2.sql index 75178998..eb633d9d 100644 --- a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-h2.sql +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-h2.sql @@ -8,7 +8,8 @@ CREATE TABLE TASK_EXECUTION ( EXIT_MESSAGE VARCHAR(2500) , ERROR_MESSAGE VARCHAR(2500) , LAST_UPDATED TIMESTAMP, - EXTERNAL_EXECUTION_ID VARCHAR(255) + EXTERNAL_EXECUTION_ID VARCHAR(255), + PARENT_EXECUTION_ID BIGINT ); CREATE TABLE TASK_EXECUTION_PARAMS ( diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-hsqldb.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-hsqldb.sql index e345cbb3..4ffa6734 100644 --- a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-hsqldb.sql +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-hsqldb.sql @@ -8,7 +8,8 @@ CREATE TABLE TASK_EXECUTION ( EXIT_MESSAGE VARCHAR(2500) , ERROR_MESSAGE VARCHAR(2500) , LAST_UPDATED TIMESTAMP, - EXTERNAL_EXECUTION_ID VARCHAR(255) + EXTERNAL_EXECUTION_ID VARCHAR(255), + PARENT_EXECUTION_ID BIGINT ); CREATE TABLE TASK_EXECUTION_PARAMS ( diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-mysql.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-mysql.sql index a41b8fce..00599fa7 100644 --- a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-mysql.sql +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-mysql.sql @@ -8,7 +8,8 @@ CREATE TABLE TASK_EXECUTION ( EXIT_MESSAGE VARCHAR(2500) , ERROR_MESSAGE VARCHAR(2500) , LAST_UPDATED TIMESTAMP, - EXTERNAL_EXECUTION_ID VARCHAR(255) + EXTERNAL_EXECUTION_ID VARCHAR(255), + PARENT_EXECUTION_ID BIGINT ) ENGINE=InnoDB; CREATE TABLE TASK_EXECUTION_PARAMS ( diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-oracle10g.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-oracle10g.sql index b871ed1c..e8caad41 100644 --- a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-oracle10g.sql +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-oracle10g.sql @@ -8,7 +8,8 @@ CREATE TABLE TASK_EXECUTION ( EXIT_MESSAGE VARCHAR2(2500) , ERROR_MESSAGE VARCHAR2(2500) , LAST_UPDATED TIMESTAMP, - EXTERNAL_EXECUTION_ID VARCHAR2(255) + EXTERNAL_EXECUTION_ID VARCHAR2(255), + PARENT_EXECUTION_ID BIGINT ); CREATE TABLE TASK_EXECUTION_PARAMS ( diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-postgresql.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-postgresql.sql index 0810a7e2..6d558b4f 100644 --- a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-postgresql.sql +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-postgresql.sql @@ -8,7 +8,8 @@ CREATE TABLE TASK_EXECUTION ( EXIT_MESSAGE VARCHAR(2500) , ERROR_MESSAGE VARCHAR(2500) , LAST_UPDATED TIMESTAMP , - EXTERNAL_EXECUTION_ID VARCHAR(255) + EXTERNAL_EXECUTION_ID VARCHAR(255), + PARENT_EXECUTION_ID BIGINT ); CREATE TABLE TASK_EXECUTION_PARAMS ( diff --git a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-sqlserver.sql b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-sqlserver.sql index 1d475e68..3985cd96 100644 --- a/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-sqlserver.sql +++ b/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task/schema-sqlserver.sql @@ -7,7 +7,8 @@ CREATE TABLE TASK_EXECUTION ( EXIT_MESSAGE VARCHAR(2500) , ERROR_MESSAGE VARCHAR(2500) , LAST_UPDATED DATETIME , - EXTERNAL_EXECUTION_ID VARCHAR(255) + EXTERNAL_EXECUTION_ID VARCHAR(255), + PARENT_EXECUTION_ID BIGINT ); CREATE TABLE TASK_EXECUTION_PARAMS ( diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/listener/TaskLifecycleListenerTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/listener/TaskLifecycleListenerTests.java index 5be44a5b..8d08205b 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/listener/TaskLifecycleListenerTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/listener/TaskLifecycleListenerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2017 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. @@ -184,15 +184,34 @@ public class TaskLifecycleListenerTests { this.taskExplorer = context.getBean(TaskExplorer.class); verifyTaskExecution(0, false, 0, null, "myid"); + } + @Test + public void testParentExecutionId() { + ConfigurableEnvironment environment = new StandardEnvironment(); + MutablePropertySources propertySources = environment.getPropertySources(); + Map myMap = new HashMap(); + myMap.put("spring.cloud.task.parentExecutionId", 789); + propertySources.addFirst(new MapPropertySource("EnvrionmentTestPropsource", myMap)); + context.setEnvironment(environment); + context.refresh(); + this.taskExplorer = context.getBean(TaskExplorer.class); + + verifyTaskExecution(0, false, 0, null, null, 789L); } private void verifyTaskExecution(int numberOfParams, boolean update) { verifyTaskExecution(numberOfParams, update, 0, null, null); } - private void verifyTaskExecution(int numberOfParams, boolean update, Integer exitCode, Throwable exception, String externalExecutionId) { + verifyTaskExecution(numberOfParams, update, exitCode, exception, + externalExecutionId, null); + } + + private void verifyTaskExecution(int numberOfParams, boolean update, + Integer exitCode, Throwable exception, String externalExecutionId, + Long parentExecutionId) { Sort sort = new Sort("id"); @@ -206,6 +225,7 @@ public class TaskLifecycleListenerTests { assertEquals(numberOfParams, taskExecution.getArguments().size()); assertEquals(exitCode, taskExecution.getExitCode()); assertEquals(externalExecutionId, taskExecution.getExternalExecutionId()); + assertEquals(parentExecutionId, taskExecution.getParentExecutionId()); if(exception != null) { assertTrue(taskExecution.getErrorMessage().length() > exception.getStackTrace().length); diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/FindAllPagingQueryProviderTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/FindAllPagingQueryProviderTests.java index 87a28050..82f59295 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/FindAllPagingQueryProviderTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/FindAllPagingQueryProviderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2017 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. @@ -43,30 +43,30 @@ public class FindAllPagingQueryProviderTests { public static Collection data() { return Arrays.asList(new Object[][]{ {"Oracle", "SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM " + "(SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, ROWNUM as " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID, ROWNUM as " + "TMP_ROW_NUM FROM (SELECT TASK_EXECUTION_ID, START_TIME, " - + "END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID " + + "END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID " + "FROM %PREFIX%EXECUTION ORDER BY START_TIME DESC, " + "TASK_EXECUTION_ID DESC)) WHERE TMP_ROW_NUM >= 1 AND " + "TMP_ROW_NUM < 11"}, {"HSQL Database Engine","SELECT LIMIT 0 10 TASK_EXECUTION_ID, " + "START_TIME, END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, " - + "ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM %PREFIX%EXECUTION ORDER BY " + + "ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM %PREFIX%EXECUTION ORDER BY " + "START_TIME DESC, TASK_EXECUTION_ID DESC"}, {"PostgreSQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, " - + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID " + + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID " + "FROM %PREFIX%EXECUTION ORDER BY START_TIME DESC, " + "TASK_EXECUTION_ID DESC LIMIT 10 OFFSET 0"}, {"MySQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM " + "%PREFIX%EXECUTION ORDER BY START_TIME DESC, " + "TASK_EXECUTION_ID DESC LIMIT 0, 10"}, {"Microsoft SQL Server","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, " - + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM " + + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM " + "(SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, ROW_NUMBER() " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID, ROW_NUMBER() " + "OVER (ORDER BY START_TIME DESC, TASK_EXECUTION_ID DESC) AS " + "TMP_ROW_NUM FROM %PREFIX%EXECUTION) TASK_EXECUTION_PAGE " + "WHERE TMP_ROW_NUM >= 1 AND TMP_ROW_NUM < 11 ORDER BY START_TIME DESC, " diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/WhereClausePagingQueryProviderTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/WhereClausePagingQueryProviderTests.java index 9f23182f..d7f81470 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/WhereClausePagingQueryProviderTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/WhereClausePagingQueryProviderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2017 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. @@ -44,34 +44,34 @@ public class WhereClausePagingQueryProviderTests { public static Collection data() { return Arrays.asList(new Object[][]{ {"Oracle", "SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM " + "(SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, ROWNUM as " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID, ROWNUM as " + "TMP_ROW_NUM FROM (SELECT TASK_EXECUTION_ID, START_TIME, " + "END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, " - + "LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM %PREFIX%EXECUTION " + + "LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM %PREFIX%EXECUTION " + "WHERE TASK_EXECUTION_ID = '0000' ORDER BY START_TIME DESC, " + "TASK_EXECUTION_ID DESC)) WHERE TMP_ROW_NUM >= 1 AND " + "TMP_ROW_NUM < 11"}, {"HSQL Database Engine","SELECT LIMIT 0 10 TASK_EXECUTION_ID, " + "START_TIME, END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, " - + "ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM %PREFIX%EXECUTION " + + "ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM %PREFIX%EXECUTION " + "WHERE TASK_EXECUTION_ID = '0000' ORDER BY " + "START_TIME DESC, TASK_EXECUTION_ID DESC"}, {"PostgreSQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, " - + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID " + + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID " + "FROM %PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = '0000' " + "ORDER BY START_TIME DESC, " + "TASK_EXECUTION_ID DESC LIMIT 10 OFFSET 0"}, {"MySQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM " + "%PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = '0000' " + "ORDER BY START_TIME DESC, " + "TASK_EXECUTION_ID DESC LIMIT 0, 10"}, {"Microsoft SQL Server","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, " - + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID FROM " + + "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID FROM " + "(SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, " - + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, ROW_NUMBER() " + + "EXIT_CODE, EXIT_MESSAGE, ERROR_MESSAGE, LAST_UPDATED, EXTERNAL_EXECUTION_ID, PARENT_EXECUTION_ID, ROW_NUMBER() " + "OVER (ORDER BY START_TIME DESC, TASK_EXECUTION_ID DESC) AS " + "TMP_ROW_NUM FROM %PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = " + "'0000') TASK_EXECUTION_PAGE WHERE TMP_ROW_NUM >= 1 " diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java index 922157d8..74b0c15d 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java @@ -162,6 +162,25 @@ public class SimpleTaskRepositoryJdbcTests { expectedTaskExecution.getExternalExecutionId()); } + @Test + @DirtiesContext + public void startTaskExecutionWithParent() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository); + + expectedTaskExecution.setStartTime(new Date()); + expectedTaskExecution.setTaskName(UUID.randomUUID().toString()); + expectedTaskExecution.setParentExecutionId(12345L); + + TaskExecution actualTaskExecution = this.taskRepository.startTaskExecution(expectedTaskExecution.getExecutionId(), + expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(), + expectedTaskExecution.getArguments(), + expectedTaskExecution.getExternalExecutionId(), + expectedTaskExecution.getParentExecutionId()); + + TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution); + } + @Test @DirtiesContext public void testCompleteTaskExecution() { diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryMapTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryMapTests.java index edd22560..79a9dde3 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryMapTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryMapTests.java @@ -115,7 +115,9 @@ public class SimpleTaskRepositoryMapTests { TaskExecution actualTaskExecution = this.taskRepository.startTaskExecution(expectedTaskExecution.getExecutionId(), expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(), - expectedTaskExecution.getArguments(), expectedTaskExecution.getExternalExecutionId()); + expectedTaskExecution.getArguments(), + expectedTaskExecution.getExternalExecutionId(), + expectedTaskExecution.getParentExecutionId()); TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution); } @@ -135,6 +137,23 @@ public class SimpleTaskRepositoryMapTests { TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution); } + + @Test + public void startTaskExecutionWithParent() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository); + + expectedTaskExecution.setStartTime(new Date()); + expectedTaskExecution.setTaskName(UUID.randomUUID().toString()); + expectedTaskExecution.setParentExecutionId(12345L); + + TaskExecution actualTaskExecution = this.taskRepository.startTaskExecution(expectedTaskExecution.getExecutionId(), + expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(), + expectedTaskExecution.getArguments(), expectedTaskExecution.getExternalExecutionId()); + + TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution); + } + @Test public void testCompleteTaskExecution() { TaskExecution expectedTaskExecution = diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java index 218fc339..507cb487 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2017 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. @@ -47,8 +47,7 @@ public class TaskExecutionCreator { * @return the taskExecution created. */ public static TaskExecution createAndStoreTaskExecutionNoParams(TaskRepository taskRepository) { - TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg(); - expectedTaskExecution = taskRepository.createTaskExecution(); + TaskExecution expectedTaskExecution = taskRepository.createTaskExecution(); return expectedTaskExecution; } diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TestVerifierUtils.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TestVerifierUtils.java index 463e4b09..08a4b260 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TestVerifierUtils.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TestVerifierUtils.java @@ -159,6 +159,9 @@ public class TestVerifierUtils { assertEquals("externalExecutionId must be equal", expectedTaskExecution.getExternalExecutionId(), actualTaskExecution.getExternalExecutionId()); + assertEquals("parentExecutionId must be equal", + expectedTaskExecution.getParentExecutionId(), + actualTaskExecution.getParentExecutionId()); if (expectedTaskExecution.getArguments() != null) { assertNotNull("arguments should not be null", diff --git a/spring-cloud-task-docs/src/main/asciidoc/features.adoc b/spring-cloud-task-docs/src/main/asciidoc/features.adoc index 22abcd47..744c4746 100644 --- a/spring-cloud-task-docs/src/main/asciidoc/features.adoc +++ b/spring-cloud-task-docs/src/main/asciidoc/features.adoc @@ -194,6 +194,19 @@ following property: spring.cloud.task.external-execution-id= ``` +[[features-parent_task_id]] +=== Parent Task Id + +Spring Cloud Task allows a user to store an parent task Id for each +TaskExecution. An example of this would be a task that executes another task +or tasks and the user would like to store what task launched the child tasks. +In order to configure your Task to set a parent TaskExecutionId add the +following property on the child task: + +``` +spring.cloud.task.parent-execution-id= +``` + [[features-task-configurer]] === TaskConfigurer