From 2eb1c49e126b31c7a61d708105281f1f4628c354 Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Tue, 3 Jan 2017 17:13:15 -0500 Subject: [PATCH] Added ability to update externalExecutionId resolves #259 --- .../cloud/task/repository/TaskRepository.java | 24 ++++++++---- .../repository/dao/JdbcTaskExecutionDao.java | 18 ++++++++- .../repository/dao/MapTaskExecutionDao.java | 11 +++++- .../task/repository/dao/TaskExecutionDao.java | 10 ++++- .../support/SimpleTaskRepository.java | 8 +++- .../SimpleTaskRepositoryJdbcTests.java | 38 ++++++++++++++++++- .../support/SimpleTaskRepositoryMapTests.java | 37 +++++++++++++++++- .../cloud/task/util/TestVerifierUtils.java | 6 ++- 8 files changed, 138 insertions(+), 14 deletions(-) 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 a475eb1d..00fb0088 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 @@ -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. @@ -83,15 +83,25 @@ public interface TaskRepository { /** * 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 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. - - * @return + * @return TaskExecution created based on the parameters. */ @Transactional TaskExecution startTaskExecution(long executionid, String taskName, Date startTime,List arguments, String externalExecutionId); + + /** + * Notifies the repository to update the taskExecution's externalExecutionId. + * + * @param executionid to the task execution to be updated. + * @param externalExecutionId id assigned to the task by the platform. + */ + @Transactional + void updateExternalExecutionId(long executionid, + String externalExecutionId); } 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 ae8bbad1..f2704589 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 @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 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. @@ -86,6 +86,9 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao { + "END_TIME = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, ERROR_MESSAGE = ?, " + "LAST_UPDATED = ? where TASK_EXECUTION_ID = ?"; + private static final String UPDATE_TASK_EXECUTION_EXTERNAL_EXECUTION_ID = "UPDATE %PREFIX%EXECUTION set " + + "EXTERNAL_EXECUTION_ID = ? where TASK_EXECUTION_ID = ?"; + 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 " @@ -313,6 +316,19 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao { } } + @Override + public void updateExternalExecutionId(long taskExecutionId, String externalExecutionId) { + Object[] parameters = new Object[]{externalExecutionId, + taskExecutionId}; + if (jdbcTemplate.update( + getQuery(UPDATE_TASK_EXECUTION_EXTERNAL_EXECUTION_ID), + parameters, + new int[]{Types.VARCHAR, Types.BIGINT}) != 1) { + throw new IllegalStateException("Invalid TaskExecution, ID " + + taskExecutionId + " not found."); + } + } + private Page queryForPageableResults(Pageable pageable, String selectClause, String fromClause, 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 29b40ba4..6ed47c2b 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 @@ -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. @@ -32,6 +32,7 @@ import org.springframework.cloud.task.repository.TaskExecution; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; +import org.springframework.util.Assert; /** * Stores Task Execution Information to a in-memory map. @@ -203,6 +204,14 @@ public class MapTaskExecutionDao implements TaskExecutionDao { } } + @Override + public void updateExternalExecutionId(long taskExecutionId, String externalExecutionId) { + TaskExecution taskExecution = taskExecutions.get(taskExecutionId); + Assert.notNull(taskExecution, "Invalid TaskExecution, ID " + + taskExecutionId + " not found."); + taskExecution.setExternalExecutionId(externalExecutionId); + } + public ConcurrentMap> getBatchJobAssociations() { return batchJobAssociations; } 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 f6a57e6d..86cb0367 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 @@ -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. @@ -163,4 +163,12 @@ public interface TaskExecutionDao { * @return a Set of the ids of the job executions executed within the task. */ Set getJobExecutionIdsByTaskExecutionId(long taskExecutionId); + + /** + * Updates the externalExecutionId for the execution id specified. + * @param taskExecutionId the execution id for the task to be updated. + * @param externalExecutionId the new externalExecutionId. + */ + void updateExternalExecutionId(long taskExecutionId, + String externalExecutionId); } 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 712932ae..360c02ae 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 @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 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. @@ -134,6 +134,12 @@ public class SimpleTaskRepository implements TaskRepository { return taskExecution; } + @Override + public void updateExternalExecutionId(long executionid, String externalExecutionId) { + initialize(); + taskExecutionDao.updateExternalExecutionId(executionid, externalExecutionId); + } + /** * Retrieves the taskExecutionDao associated with this repository. * @return the taskExecutionDao 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 bf04be8f..922157d8 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 @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 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. @@ -126,6 +126,42 @@ public class SimpleTaskRepositoryJdbcTests { TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution); } + @Test + public void testUpdateExternalExecutionId() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository); + expectedTaskExecution.setExternalExecutionId(UUID.randomUUID().toString()); + taskRepository.updateExternalExecutionId( + expectedTaskExecution.getExecutionId(), + expectedTaskExecution.getExternalExecutionId()); + TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, + TestDBUtils.getTaskExecutionFromDB(dataSource, + expectedTaskExecution.getExecutionId())); + } + + @Test + public void testUpdateNullExternalExecutionId() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository); + expectedTaskExecution.setExternalExecutionId(null); + taskRepository.updateExternalExecutionId( + expectedTaskExecution.getExecutionId(), + expectedTaskExecution.getExternalExecutionId()); + TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, + TestDBUtils.getTaskExecutionFromDB(dataSource, + expectedTaskExecution.getExecutionId())); + } + + @Test(expected = IllegalStateException.class) + public void testInvalidExecutionIdForExternalExecutionIdUpdate() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository); + expectedTaskExecution.setExternalExecutionId(null); + taskRepository.updateExternalExecutionId( + -1, + expectedTaskExecution.getExternalExecutionId()); + } + @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 6f14a554..edd22560 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 @@ -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. @@ -28,6 +28,7 @@ import org.springframework.cloud.task.repository.TaskExecution; import org.springframework.cloud.task.repository.TaskRepository; import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao; import org.springframework.cloud.task.util.TaskExecutionCreator; +import org.springframework.cloud.task.util.TestDBUtils; import org.springframework.cloud.task.util.TestVerifierUtils; import static org.springframework.test.util.AssertionErrors.assertTrue; @@ -61,6 +62,40 @@ public class SimpleTaskRepositoryMapTests { getSingleTaskExecutionFromMapRepository(expectedTaskExecution.getExecutionId())); } + @Test + public void testUpdateExternalExecutionId() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository); + expectedTaskExecution.setExternalExecutionId(UUID.randomUUID().toString()); + taskRepository.updateExternalExecutionId( + expectedTaskExecution.getExecutionId(), + expectedTaskExecution.getExternalExecutionId()); + TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, + getSingleTaskExecutionFromMapRepository(expectedTaskExecution.getExecutionId())); + } + + @Test + public void testUpdateNullExternalExecutionId() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository); + expectedTaskExecution.setExternalExecutionId(null); + taskRepository.updateExternalExecutionId( + expectedTaskExecution.getExecutionId(), + expectedTaskExecution.getExternalExecutionId()); + TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, + getSingleTaskExecutionFromMapRepository(expectedTaskExecution.getExecutionId())); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidExecutionIdForExternalExecutionIdUpdate() { + TaskExecution expectedTaskExecution = + TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository); + expectedTaskExecution.setExternalExecutionId(null); + taskRepository.updateExternalExecutionId( + -1, + expectedTaskExecution.getExternalExecutionId()); + } + @Test public void testCreateTaskExecutionWithParam() { TaskExecution 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 959934dc..463e4b09 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 @@ -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. @@ -156,6 +156,10 @@ public class TestVerifierUtils { assertEquals("errorMessage must be equal", expectedTaskExecution.getErrorMessage(), actualTaskExecution.getErrorMessage()); + assertEquals("externalExecutionId must be equal", + expectedTaskExecution.getExternalExecutionId(), + actualTaskExecution.getExternalExecutionId()); + if (expectedTaskExecution.getArguments() != null) { assertNotNull("arguments should not be null", actualTaskExecution.getArguments());