FINERACT-2026: Fix job run history

This commit is contained in:
Adam Saghy
2023-12-13 10:23:43 +01:00
parent 7a417b058b
commit da2cdd8bfd
3 changed files with 168 additions and 9 deletions
@@ -25,6 +25,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.cob.conditions.LoanCOBEnabledCondition;
import org.apache.fineract.cob.data.LoanIdAndLastClosedBusinessDate;
import org.apache.fineract.cob.loan.LoanCOBConstant;
@@ -40,6 +41,7 @@ import org.apache.fineract.infrastructure.jobs.domain.ScheduledJobDetail;
import org.apache.fineract.infrastructure.jobs.domain.ScheduledJobDetailRepository;
import org.apache.fineract.infrastructure.jobs.exception.JobNotFoundException;
import org.apache.fineract.infrastructure.jobs.service.JobStarter;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.configuration.JobLocator;
@@ -51,6 +53,7 @@ import org.springframework.context.annotation.Conditional;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
@Conditional(LoanCOBEnabledCondition.class)
@@ -78,10 +81,12 @@ public class AsyncLoanCOBExecutorServiceImpl implements AsyncLoanCOBExecutorServ
executeLoanCOBDayByDayUntilCOBBusinessDate(oldestCOBProcessedDate, cobBusinessDate);
}
} catch (NoSuchJobException e) {
throw new JobNotFoundException(LoanCOBConstant.JOB_NAME, e);
// Throwing an error here is useless as it will be swallowed hence it is async method
log.error("", new JobNotFoundException(LoanCOBConstant.JOB_NAME, e));
} catch (JobInstanceAlreadyCompleteException | JobRestartException | JobParametersInvalidException
| JobExecutionAlreadyRunningException e) {
throw new RuntimeException(e);
| JobExecutionAlreadyRunningException | JobExecutionException e) {
// Throwing an error here is useless as it will be swallowed hence it is async method
log.error("", e);
} finally {
ThreadLocalContextUtil.reset();
}
@@ -89,7 +94,7 @@ public class AsyncLoanCOBExecutorServiceImpl implements AsyncLoanCOBExecutorServ
private void executeLoanCOBDayByDayUntilCOBBusinessDate(LocalDate oldestCOBProcessedDate, LocalDate cobBusinessDate)
throws NoSuchJobException, JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException,
JobParametersInvalidException, JobRestartException {
JobParametersInvalidException, JobRestartException, JobExecutionException {
Job job = jobLocator.getJob(LoanCOBConstant.JOB_NAME);
ScheduledJobDetail scheduledJobDetail = scheduledJobDetailRepository.findByJobName(LoanCOBConstant.JOB_HUMAN_READABLE_NAME);
LocalDate executingBusinessDate = oldestCOBProcessedDate.plusDays(1);
@@ -32,7 +32,10 @@ import org.apache.fineract.infrastructure.jobs.domain.JobParameterRepository;
import org.apache.fineract.infrastructure.jobs.domain.ScheduledJobDetail;
import org.apache.fineract.infrastructure.jobs.service.jobname.JobNameService;
import org.apache.fineract.infrastructure.jobs.service.jobparameterprovider.JobParameterProvider;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
@@ -55,19 +58,26 @@ public class JobStarter {
private final List<JobParameterProvider<?>> jobParameterProviders;
private final JobNameService jobNameService;
public void run(Job job, ScheduledJobDetail scheduledJobDetail, Set<JobParameterDTO> jobParameterDTOSet)
public static final List<BatchStatus> FAILED_STATUSES = List.of(BatchStatus.FAILED, BatchStatus.ABANDONED, BatchStatus.STOPPED,
BatchStatus.STOPPING, BatchStatus.UNKNOWN);
public JobExecution run(Job job, ScheduledJobDetail scheduledJobDetail, Set<JobParameterDTO> jobParameterDTOSet)
throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException,
JobRestartException {
JobRestartException, JobExecutionException {
Map<String, JobParameter<?>> jobParameterMap = getJobParameter(scheduledJobDetail);
JobParameters jobParameters = new JobParametersBuilder(jobExplorer).getNextJobParameters(job)
.addJobParameters(new JobParameters(jobParameterMap))
.addJobParameters(new JobParameters(provideCustomJobParameters(
jobNameService.getJobByHumanReadableName(scheduledJobDetail.getJobName()).getEnumStyleName(), jobParameterDTOSet)))
.toJobParameters();
jobLauncher.run(job, jobParameters);
JobExecution result = jobLauncher.run(job, jobParameters);
if (FAILED_STATUSES.contains(result.getStatus())) {
throw new JobExecutionException(result.getExitStatus().toString());
}
return result;
}
public Map<String, org.springframework.batch.core.JobParameter<?>> getJobParameter(ScheduledJobDetail scheduledJobDetail) {
protected Map<String, org.springframework.batch.core.JobParameter<?>> getJobParameter(ScheduledJobDetail scheduledJobDetail) {
List<org.apache.fineract.infrastructure.jobs.domain.JobParameter> jobParameterList = jobParameterRepository
.findJobParametersByJobId(scheduledJobDetail.getId());
Map<String, JobParameter<?>> jobParameterMap = new HashMap<>();
@@ -77,7 +87,7 @@ public class JobStarter {
return jobParameterMap;
}
private Map<String, JobParameter<?>> provideCustomJobParameters(String jobName, Set<JobParameterDTO> jobParameterDTOSet) {
protected Map<String, JobParameter<?>> provideCustomJobParameters(String jobName, Set<JobParameterDTO> jobParameterDTOSet) {
Optional<JobParameterProvider<?>> jobParameterProvider = jobParameterProviders.stream()
.filter(provider -> provider.canProvideParametersForJob(jobName)).findFirst();
Map<String, ? extends JobParameter<?>> map = jobParameterProvider