Skip to content

Commit

Permalink
Improve action execution flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Aug 31, 2024
1 parent 9e32b08 commit 91c934d
Showing 1 changed file with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,74 @@ public boolean isExecutionEnabled(ActionType actionType) {
return ActionExecutorConfig.getInstance().isExecutionForActionTypeEnabled(actionType);
}

/**
* Resolve the actions that need to be executed for thr given action types and execute them.
*
* @param actionType Action Type.
* @param eventContext The event context of the corresponding flow.
* @param tenantDomain Tenant domain.
* @return Action execution status.
*/
public ActionExecutionStatus execute(ActionType actionType, Map<String, Object> eventContext, String tenantDomain)
throws ActionExecutionException {

try {
List<Action> actions = getActionsByActionType(actionType, tenantDomain);
validateActions(actions, actionType);
ActionExecutionRequest actionRequest = buildActionExecutionRequest(actionType, eventContext);
ActionExecutionResponseProcessor actionExecutionResponseProcessor = getResponseProcessor(actionType);

Action action = actions.get(0); // As of now only one action is allowed.
return Optional.ofNullable(action)
.filter(activeAction -> activeAction.getStatus() == Action.Status.ACTIVE)
.map(activeAction -> executeAction(activeAction, actionRequest, eventContext,
actionExecutionResponseProcessor))
.orElse(new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext));
// As of now only one action is allowed.
Action action = actions.get(0);
return execute(action, eventContext);
} catch (ActionExecutionRuntimeException e) {
// todo: add to diagnostics
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
}
}

/**
* Resolve the actions by given the action id list and execute them.
*
* @param actionType Action Type.
* @param actionIds Lis of action Ids of the actions that need to be executed.
* @param eventContext The event context of the corresponding flow.
* @param tenantDomain Tenant domain.
* @return Action execution status.
*/
public ActionExecutionStatus execute(ActionType actionType, String[] actionIds, Map<String, Object> eventContext,
String tenantDomain) throws ActionExecutionException {

try {
// As of now only one action is allowed.
return execute(getActionByActionId(actionType, actionIds[0], tenantDomain), eventContext);
} catch (ActionExecutionRuntimeException e) {
// todo: add to diagnostics
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
}
}

private ActionExecutionStatus execute(Action action, Map<String, Object> eventContext)
throws ActionExecutionException {

ActionType actionType = ActionType.valueOf(action.getType().getActionType());
ActionExecutionRequest actionRequest = buildActionExecutionRequest(actionType, eventContext);
ActionExecutionResponseProcessor actionExecutionResponseProcessor = getResponseProcessor(actionType);

return Optional.ofNullable(action)
.filter(activeAction -> activeAction.getStatus() == Action.Status.ACTIVE)
.map(activeAction -> executeAction(activeAction, actionRequest, eventContext,
actionExecutionResponseProcessor))
.orElse(new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext));
}

private Action getActionByActionId(ActionType actionType, String actionId, String tenantDomain)
throws ActionExecutionRuntimeException {

try {
return ActionExecutionServiceComponentHolder.getInstance().getActionManagementService().getActionByActionId(
Action.ActionTypes.valueOf(actionType.name()).getActionType(), actionId, tenantDomain);
} catch (ActionMgtException e) {
throw new ActionExecutionRuntimeException("Error occurred while retrieving action by action Id .", e);
}
}

Expand Down

0 comments on commit 91c934d

Please sign in to comment.