Skip to content

Commit

Permalink
IDP-2192 kieContainer ready logic simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
antonio.torre committed Dec 14, 2023
1 parent 85339b4 commit 6f94126
Showing 1 changed file with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,38 @@ public RuleEngineServiceImpl(OnboardingContextHolderService onboardingContextHol
public EvaluationDTO applyRules(OnboardingDTO onboardingRequest, InitiativeConfig initiative) {
log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] evaluating rules of user {} into initiative {}", onboardingRequest.getUserId(), onboardingRequest.getInitiativeId());

StatelessKieSession statelessKieSession = onboardingContextHolderService.getBeneficiaryRulesKieBase().newStatelessKieSession();

OnboardingDroolsDTO req = onboarding2OnboardingDroolsMapper.apply(onboardingRequest);

@SuppressWarnings("unchecked")
List<Command<?>> cmds = Arrays.asList(
CommandFactory.newInsert(req),
CommandFactory.newInsert(criteriaCodeService),
new AgendaGroupSetFocusCommand(req.getInitiativeId())
);

long before = System.currentTimeMillis();
statelessKieSession.execute(CommandFactory.newBatchExecution(cmds));
if(checkIfKieBaseContainerIsReady(initiative)) {
StatelessKieSession statelessKieSession = onboardingContextHolderService.getBeneficiaryRulesKieBase().newStatelessKieSession();

checkIfContainerWasReady(req, initiative);
@SuppressWarnings("unchecked")
List<Command<?>> cmds = Arrays.asList(
CommandFactory.newInsert(req),
CommandFactory.newInsert(criteriaCodeService),
new AgendaGroupSetFocusCommand(req.getInitiativeId())
);

PerformanceLogger.logTiming("ONBOARDING_RULE_ENGINE", before, "resulted into rejections %s".formatted(req.getOnboardingRejectionReasons()));

log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] Send message prepared: {}", req);

return onboarding2EvaluationMapper.apply(req, initiative, req.getOnboardingRejectionReasons());
}
long before = System.currentTimeMillis();
statelessKieSession.execute(CommandFactory.newBatchExecution(cmds));

private void checkIfContainerWasReady(OnboardingDroolsDTO req, InitiativeConfig initiative) {
if (req.getOnboardingRejectionReasons().isEmpty() && // there is not rejection reason
!CollectionUtils.isEmpty(initiative.getAutomatedCriteria()) && // the drools container is supposed to be involved
!onboardingContextHolderService.getBeneficiaryRulesKieInitiativeIds() // the initiative was not inside the container drools
.contains(initiative.getInitiativeId())
) {
PerformanceLogger.logTiming("ONBOARDING_RULE_ENGINE", before, "resulted into rejections %s".formatted(req.getOnboardingRejectionReasons()));
} else {
req.getOnboardingRejectionReasons().add(new OnboardingRejectionReason(
OnboardingRejectionReason.OnboardingRejectionReasonType.TECHNICAL_ERROR,
OnboardingConstants.REJECTION_REASON_RULE_ENGINE_NOT_READY,
null, null, null
));
}

log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] Send message prepared: {}", req);

return onboarding2EvaluationMapper.apply(req, initiative, req.getOnboardingRejectionReasons());
}

private boolean checkIfKieBaseContainerIsReady(InitiativeConfig initiative) {
return !CollectionUtils.isEmpty(initiative.getAutomatedCriteria()) && // the drools container is supposed to be involved
onboardingContextHolderService.getBeneficiaryRulesKieInitiativeIds() // the initiative is inside the container drools
.contains(initiative.getInitiativeId());
}
}

0 comments on commit 6f94126

Please sign in to comment.