Refactored dependency management

Created a Spring Boot starter that can be used to configure Spring Cloud
Task and it's related functionality.

Updates per code review

Removed autowiring of app context

Refactored DeployerPartitionHandler to correctly use environment variables

Exposed deployment properties of TaskLauncher

Exposed deployment properties via the TaskLaunchRequest

Updated based on code review
This commit is contained in:
Michael Minella
2016-06-30 23:10:08 -05:00
committed by Glenn Renfro
parent a1c1dd161f
commit 57e9d4dcae
39 changed files with 369 additions and 142 deletions

View File

@@ -105,6 +105,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
private Environment environment;
private Map<String, String> deploymentProperties;
public DeployerPartitionHandler(TaskLauncher taskLauncher,
JobExplorer jobExplorer,
Resource resource,
@@ -168,6 +170,15 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
this.timeout = timeout;
}
/**
* Map of deployment properties to be used by the {@link TaskLauncher}
*
* @param deploymentProperties
*/
public void setDeploymentProperties(Map<String, String> deploymentProperties) {
this.deploymentProperties = deploymentProperties;
}
@BeforeTask
public void beforeTask(TaskExecution taskExecution) {
this.taskExecution = taskExecution;
@@ -213,32 +224,38 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
}
private void launchWorker(StepExecution workerStepExecution) {
//TODO: Refactor these to be passed as command line args once SCD-20 is complete
// https://github.com/spring-cloud/spring-cloud-deployer/issues/20
Map<String, String> arguments = getArguments(this.taskExecution.getArguments());
arguments.put(SPRING_CLOUD_TASK_JOB_EXECUTION_ID,
String.valueOf(workerStepExecution.getJobExecution().getId()));
arguments.put(SPRING_CLOUD_TASK_STEP_EXECUTION_ID,
String.valueOf(workerStepExecution.getId()));
arguments.put(SPRING_CLOUD_TASK_STEP_NAME, this.stepName);
List<String> arguments = new ArrayList<>();
arguments.addAll(this.taskExecution.getArguments());
arguments.add(formatArgument(SPRING_CLOUD_TASK_JOB_EXECUTION_ID,
String.valueOf(workerStepExecution.getJobExecution().getId())));
arguments.add(formatArgument(SPRING_CLOUD_TASK_STEP_EXECUTION_ID,
String.valueOf(workerStepExecution.getId())));
arguments.add(formatArgument(SPRING_CLOUD_TASK_STEP_NAME, this.stepName));
Map<String, String> environmentProperties = new HashMap<>(this.environmentProperties.size());
environmentProperties.putAll(getCurrentEnvironmentProperties());
environmentProperties.putAll(this.environmentProperties);
AppDefinition definition =
new AppDefinition(String.format("%s:%s:%s",
taskExecution.getTaskName(),
workerStepExecution.getJobExecution().getJobInstance().getJobName(),
workerStepExecution.getStepName()),
arguments);
Map<String, String> environmentProperties = new HashMap<>(this.environmentProperties.size());
environmentProperties.putAll(getCurrentEnvironmentProperties());
environmentProperties.putAll(this.environmentProperties);
environmentProperties);
AppDeploymentRequest request =
new AppDeploymentRequest(definition, this.resource, environmentProperties);
new AppDeploymentRequest(definition,
this.resource,
this.deploymentProperties,
arguments);
taskLauncher.launch(request);
}
private String formatArgument(String key, String value) {
return String.format("--%s=%s", key, value);
}
private Collection<StepExecution> pollReplies(final StepExecution masterStepExecution,
final Set<StepExecution> executed,
final Set<StepExecution> candidates,

View File

@@ -147,9 +147,9 @@ public class DeployerPartitionHandlerTests {
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask:partitionedJob:step1:partition1", appDefinition.getName());
assertEquals("1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
assertEquals("4", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
assertEquals("step1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertEquals(1, results.size());
StepExecution resultStepExecution = results.iterator().next();
@@ -344,16 +344,16 @@ public class DeployerPartitionHandlerTests {
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(2, request.getDeploymentProperties().size());
assertEquals("bar", request.getDeploymentProperties().get("foo"));
assertEquals("qux", request.getDeploymentProperties().get("baz"));
assertEquals(2, request.getDefinition().getProperties().size());
assertEquals("bar", request.getDefinition().getProperties().get("foo"));
assertEquals("qux", request.getDefinition().getProperties().get("baz"));
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask:partitionedJob:step1:partition1", appDefinition.getName());
assertEquals("1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
assertEquals("4", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
assertEquals("step1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertEquals(1, results.size());
StepExecution resultStepExecution = results.iterator().next();
@@ -361,6 +361,10 @@ public class DeployerPartitionHandlerTests {
assertEquals("step1:partition1", resultStepExecution.getStepName());
}
private String formatArgs(String key, String value) {
return String.format("--%s=%s", key, value);
}
@Test
public void testOverridingEnvironmentProperties() throws Exception {
@@ -399,17 +403,17 @@ public class DeployerPartitionHandlerTests {
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(3, request.getDeploymentProperties().size());
assertEquals("bar", request.getDeploymentProperties().get("foo"));
assertEquals("qux", request.getDeploymentProperties().get("baz"));
assertEquals("batch", request.getDeploymentProperties().get("task"));
assertEquals(3, request.getDefinition().getProperties().size());
assertEquals("bar", request.getDefinition().getProperties().get("foo"));
assertEquals("qux", request.getDefinition().getProperties().get("baz"));
assertEquals("batch", request.getDefinition().getProperties().get("task"));
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask:partitionedJob:step1:partition1", appDefinition.getName());
assertEquals("1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
assertEquals("4", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
assertEquals("step1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertEquals(1, results.size());
StepExecution resultStepExecution = results.iterator().next();
@@ -539,6 +543,58 @@ public class DeployerPartitionHandlerTests {
validateStepExecutionResults(results);
}
@Test
public void testDeployerProperties() 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);
Map<String, String> deploymentProperties = new HashMap<>(2);
deploymentProperties.put("foo", "bar");
deploymentProperties.put("baz", "qux");
handler.setDeploymentProperties(deploymentProperties);
TaskExecution taskExecution = new TaskExecution();
taskExecution.setTaskName("partitionedJobTask");
Set<StepExecution> stepExecutions = new HashSet<>();
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(2, request.getDeploymentProperties().size());
assertEquals("bar", request.getDeploymentProperties().get("foo"));
assertEquals("qux", request.getDeploymentProperties().get("baz"));
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask:partitionedJob:step1:partition1", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertEquals(1, results.size());
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
}
private StepExecution getStepExecutionFinish(StepExecution stepExecutionStart, BatchStatus status) {
StepExecution workerStepExecutionFinish = new StepExecution(stepExecutionStart.getStepName(), stepExecutionStart.getJobExecution());
workerStepExecutionFinish.setId(stepExecutionStart.getId());
@@ -592,9 +648,9 @@ public class DeployerPartitionHandlerTests {
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask:partitionedJob:step1:partition" + (i - 3), appDefinition.getName());
assertEquals("1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
assertEquals(String.valueOf(i), appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
assertEquals("step1", appDefinition.getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, String.valueOf(i))));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
}
}