Added code to record task completion when process is terminated gracefully
Updated per code review Updates for merge
This commit is contained in:
committed by
Glenn Renfro
parent
d6c36ee559
commit
172f4b957a
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,9 @@ map uncaught exceptions to exit codes. This allows you to be able to indicate a
|
||||
level what went wrong. Also, by mapping exit codes in this manner, Spring Cloud Task will
|
||||
record the exit code returned.
|
||||
|
||||
If the task is terminated with a SIG-INT or a SIG-TERM, the exit code will be zero unless
|
||||
otherwise specified within the code.
|
||||
|
||||
NOTE: While the task is running the exit code will be stored as a null in the repository.
|
||||
Once complete the appropriate exit code will be stored based on the guidelines enumerated
|
||||
above.
|
||||
|
||||
Reference in New Issue
Block a user