Use spring Boot events to get exit code

Spring Boot now provides the result of an ExitCodeExceptionMapper via an
event called ExitCodeEvent.  This commit takes advantage of this new
event.

Resolves spring-cloud/spring-cloud-task#48
This commit is contained in:
Michael Minella
2016-01-22 13:03:21 -06:00
parent eb995ee037
commit 2ceb353838
4 changed files with 28 additions and 31 deletions

View File

@@ -26,6 +26,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ExitCodeEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskNameResolver;
@@ -68,6 +69,10 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private ApplicationArguments applicationArguments;
private ApplicationFailedEvent applicationFailedEvent;
private ExitCodeEvent exitCodeEvent;
/**
* @param taskRepository The repository to record executions in.
*/
@@ -99,28 +104,15 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
doTaskStart();
started = true;
}
else if(applicationEvent instanceof ApplicationFailedEvent) {
this.applicationFailedEvent = (ApplicationFailedEvent) applicationEvent;
}
else if(applicationEvent instanceof ExitCodeEvent){
this.exitCodeEvent = (ExitCodeEvent) applicationEvent;
}
else if(applicationEvent instanceof ContextClosedEvent) {
doTaskEnd();
}
else if(applicationEvent instanceof ApplicationFailedEvent) {
doTaskFailed(((ApplicationFailedEvent) applicationEvent).getException());
}
}
private void doTaskFailed(Throwable exception) {
if(started) {
this.taskExecution.setEndTime(new Date());
//TODO: get exit code from new event thrown by Boot.
this.taskExecution.setExitCode(1);
this.taskExecution.setExitMessage(stackTraceToString(exception));
taskRepository.update(taskExecution);
}
else {
logger.error("An event to fail a task has been received for a task that has " +
"not yet started.");
}
}
private String stackTraceToString(Throwable exception) {
@@ -135,8 +127,20 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private void doTaskEnd() {
if(started) {
this.taskExecution.setEndTime(new Date());
//TODO: get exit code from new event thrown by Boot.
this.taskExecution.setExitCode(0);
if(this.exitCodeEvent != null) {
this.taskExecution.setExitCode(exitCodeEvent.getExitCode());
}
else if(this.applicationFailedEvent != null){
this.taskExecution.setExitCode(1);
}
else{
this.taskExecution.setExitCode(0);
}
if(this.applicationFailedEvent != null) {
this.taskExecution.setExitMessage(stackTraceToString(this.applicationFailedEvent.getException()));
}
taskRepository.update(taskExecution);
}
@@ -166,12 +170,4 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
"recorded.");
}
}
/**
* Used for testing purposes
* @return the current {@link TaskExecution}
*/
TaskExecution getTaskExecution() {
return this.taskExecution;
}
}

View File

@@ -99,6 +99,7 @@ public class TaskLifecycleListenerTests {
context.refresh();
RuntimeException exception = new RuntimeException("This was expected");
context.publishEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], context, exception));
context.publishEvent(new ContextClosedEvent(context));
verifyTaskExecution(0, true, 1, exception);
}