* @param exception Exception that caused the TaskRun to fail, else null if the TaskRun succeeded.
* @return The saved TaskRun.
*/
private TaskRun saveFinishedTaskRun(JobDataMap jobDataMap, JobExecutionException exception) {
long taskRunId = jobDataMap.getLong(TASK_RUN_ID);
TaskRun taskRun = get(taskRunId);
taskRun.setEndDate(new Date());
taskRun.setSysOut(jobDataMap.getString(SYS_OUT));
taskRun.setSysErr(jobDataMap.getString(SYS_ERR));
if (exception == null) {
if (Status.CANCELLING.equals(taskRun.getStatus())) {
setStatus(taskRun, Status.CANCELLED);
} else {
setStatus(taskRun, Status.SUCCESS);
}
} else {
log.error(exception);
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
String stackTrace = writer.toString();
taskRun.setStackTrace(stackTrace);
if (Status.CANCELLING.equals(taskRun.getStatus())) {
setStatus(taskRun, Status.CANCELLED);
} else {
setStatus(taskRun, Status.FAILED);
}
}