FINERACT-1724: Set system user when a job got restarted by stuck job listener

This commit is contained in:
Adam Saghy
2023-12-14 21:23:17 +01:00
parent 4e877451c3
commit cba759533d
3 changed files with 11 additions and 10 deletions
@@ -36,7 +36,6 @@ import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.mapping.NullAuthoritiesMapper;
import org.springframework.security.core.context.SecurityContextHolder;
@Slf4j
@@ -49,8 +48,7 @@ public class InitialisationTasklet implements Tasklet {
public RepeatStatus execute(@NotNull StepContribution contribution, @NotNull ChunkContext chunkContext) throws Exception {
HashMap<BusinessDateType, LocalDate> businessDates = ThreadLocalContextUtil.getBusinessDates();
AppUser user = userRepository.fetchSystemUser();
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(),
new NullAuthoritiesMapper().mapAuthorities(user.getAuthorities()));
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
ThreadLocalContextUtil.setActionContext(ActionContext.COB);
String businessDateString = Objects.requireNonNull((String) chunkContext.getStepContext().getStepExecution().getJobExecution()
@@ -36,8 +36,6 @@ import org.quartz.JobKey;
import org.quartz.JobListener;
import org.quartz.Trigger;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.core.authority.mapping.NullAuthoritiesMapper;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
@@ -52,7 +50,6 @@ public class SchedulerJobListener implements JobListener {
private final String name = SchedulerServiceConstants.DEFAULT_LISTENER_NAME;
private final SchedularWritePlatformService schedularService;
private final AppUserRepositoryWrapper userRepository;
private final GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();
private final BusinessDateReadPlatformService businessDateReadPlatformService;
private int stackTraceLevel = 0;
@@ -64,8 +61,7 @@ public class SchedulerJobListener implements JobListener {
@Override
public void jobToBeExecuted(@SuppressWarnings("unused") final JobExecutionContext context) {
AppUser user = this.userRepository.fetchSystemUser();
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(),
authoritiesMapper.mapAuthorities(user.getAuthorities()));
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
HashMap<BusinessDateType, LocalDate> businessDates = businessDateReadPlatformService.getBusinessDates();
ThreadLocalContextUtil.setActionContext(ActionContext.DEFAULT);
@@ -30,12 +30,15 @@ import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.infrastructure.core.service.tenant.TenantDetailsService;
import org.apache.fineract.infrastructure.event.external.service.JdbcTemplateFactory;
import org.apache.fineract.infrastructure.jobs.domain.JobExecutionRepository;
import org.apache.fineract.infrastructure.jobs.service.jobname.JobNameProvider;
import org.apache.fineract.useradministration.domain.AppUser;
import org.apache.fineract.useradministration.domain.AppUserRepositoryWrapper;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
@Service
@@ -46,10 +49,10 @@ public class StuckJobListener implements ApplicationListener<ContextRefreshedEve
private final JobExecutionRepository jobExecutionRepository;
private final JdbcTemplateFactory jdbcTemplateFactory;
private final TenantDetailsService tenantDetailsService;
private final JobNameProvider jobNameProvider;
private final JobRegistry jobRegistry;
private final BusinessDateReadPlatformService businessDateReadPlatformService;
private final StuckJobExecutorService stuckJobExecutorService;
private final AppUserRepositoryWrapper userRepository;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
@@ -64,6 +67,10 @@ public class StuckJobListener implements ApplicationListener<ContextRefreshedEve
HashMap<BusinessDateType, LocalDate> businessDates = businessDateReadPlatformService.getBusinessDates();
ThreadLocalContextUtil.setActionContext(ActionContext.DEFAULT);
ThreadLocalContextUtil.setBusinessDates(businessDates);
AppUser user = userRepository.fetchSystemUser();
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(),
user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
stuckJobNames.forEach(stuckJobExecutorService::resumeStuckJob);
} catch (Exception e) {
throw new RuntimeException("Error while trying to restart stuck jobs", e);