Added code to record task completion when process is terminated gracefully

Updated per code review

Updates for merge
This commit is contained in:
Michael Minella
2016-07-25 13:37:02 -05:00
committed by Glenn Renfro
parent d6c36ee559
commit 172f4b957a
2 changed files with 17 additions and 4 deletions

View File

@@ -27,6 +27,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
@@ -62,7 +63,7 @@ import org.springframework.util.Assert;
*
* @author Michael Minella
*/
public class TaskLifecycleListener implements ApplicationListener<ApplicationEvent>, SmartLifecycle {
public class TaskLifecycleListener implements ApplicationListener<ApplicationEvent>, SmartLifecycle, DisposableBean {
@Autowired
private ConfigurableApplicationContext context;
@@ -78,6 +79,8 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private boolean started = false;
private boolean finished = false;
private TaskNameResolver taskNameResolver;
private ApplicationArguments applicationArguments;
@@ -139,7 +142,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
}
private void doTaskEnd() {
if(started) {
if(this.started && !this.finished) {
this.taskExecution.setEndTime(new Date());
if(this.exitCodeEvent != null) {
@@ -164,11 +167,14 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
taskRepository.completeTaskExecution(taskExecution.getExecutionId(), taskExecution.getExitCode(),
taskExecution.getEndTime(), taskExecution.getExitMessage());
this.finished = true;
if(this.closeContext && this.context.isActive()) {
this.context.close();
}
}
else {
else if(!this.started){
logger.error("An event to end a task has been received for a task that has " +
"not yet started.");
}
@@ -254,7 +260,6 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
@Override
public void stop() {
}
@Override
@@ -266,4 +271,9 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
public int getPhase() {
return 0;
}
@Override
public void destroy() throws Exception {
this.doTaskEnd();
}
}