Skip to content

Commit

Permalink
[SELC-3948] refactor: Workflow Executor and Onboarding state updates (#…
Browse files Browse the repository at this point in the history
…110)

* remove savePendingState functions

* refactor workflow

* save onboarding next status on orchestration
  • Loading branch information
manuraf authored Jan 24, 2024
1 parent 9d4795f commit 41be6a4
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
import com.microsoft.durabletask.OrchestrationRuntimeStatus;
import com.microsoft.durabletask.PurgeInstanceCriteria;
import com.microsoft.durabletask.PurgeResult;
import com.microsoft.durabletask.azurefunctions.DurableActivityTrigger;
import com.microsoft.durabletask.azurefunctions.DurableClientContext;
import com.microsoft.durabletask.azurefunctions.DurableClientInput;
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.config.PurgeConfig;
import it.pagopa.selfcare.onboarding.functions.utils.SaveOnboardingStatusInput;
import it.pagopa.selfcare.onboarding.service.OnboardingService;

import java.time.Duration;
import java.time.Instant;
Expand All @@ -22,24 +18,13 @@
public class CommonFunctions {

public static final String FORMAT_LOGGER_ONBOARDING_STRING = "%s: %s";
public static final String SAVE_ONBOARDING_STATUS_ACTIVITY = "SaveOnboardingStatus";

private final OnboardingService service;

private final PurgeConfig purgeConfig;

public CommonFunctions(OnboardingService service, PurgeConfig purgeConfig) {
this.service = service;
public CommonFunctions(PurgeConfig purgeConfig) {
this.purgeConfig = purgeConfig;
}

@FunctionName(SAVE_ONBOARDING_STATUS_ACTIVITY)
public void savePendingState(@DurableActivityTrigger(name = "onboardingString") String saveOnboardingStatusInputString, final ExecutionContext context) {
SaveOnboardingStatusInput saveOnboardingStatusInput = SaveOnboardingStatusInput.readJsonString(saveOnboardingStatusInputString);
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SAVE_ONBOARDING_STATUS_ACTIVITY, saveOnboardingStatusInput.getOnboardingId()));
service.savePendingState(saveOnboardingStatusInput.getOnboardingId(), OnboardingStatus.valueOf(saveOnboardingStatusInput.getStatus()));
}

@FunctionName("PurgeInstancesCompleted")
public void purgeInstances(
@TimerTrigger(name = "purgeTimer", schedule = "0 0 11 * * *") String timerInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.microsoft.durabletask.azurefunctions.DurableClientContext;
import com.microsoft.durabletask.azurefunctions.DurableClientInput;
import com.microsoft.durabletask.azurefunctions.DurableOrchestrationTrigger;
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.config.RetryPolicyConfig;
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.exception.ResourceNotFoundException;
Expand Down Expand Up @@ -95,7 +96,8 @@ public void onboardingsOrchestrator(
default -> throw new IllegalArgumentException("Workflow options not found!");
}

workflowExecutor.execute(ctx, onboarding);
Optional<OnboardingStatus> optNextStatus = workflowExecutor.execute(ctx, onboarding);
optNextStatus.ifPresent(onboardingStatus -> service.updateOnboardingStatus(onboardingId, onboardingStatus));
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private SendMailInput builderWithProductAndUserRequest(Onboarding onboarding) {
return sendMailInput;
}

public void savePendingState(String onboardingId, OnboardingStatus status) {
public void updateOnboardingStatus(String onboardingId, OnboardingStatus status) {
repository
.update("status", status.name())
.where("_id", onboardingId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@
import com.microsoft.durabletask.TaskOrchestrationContext;
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.functions.utils.SaveOnboardingStatusInput;

import static it.pagopa.selfcare.onboarding.functions.CommonFunctions.SAVE_ONBOARDING_STATUS_ACTIVITY;
import java.util.Optional;

import static it.pagopa.selfcare.onboarding.functions.utils.ActivityName.*;
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingString;

public interface WorkflowExecutor {

void executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding);
void executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding);
Optional<OnboardingStatus> executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding);
Optional<OnboardingStatus> executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding);

ObjectMapper objectMapper();

TaskOptions optionsRetry();

default void execute(TaskOrchestrationContext ctx, Onboarding onboarding){
switch (onboarding.getStatus()){
default Optional<OnboardingStatus> execute(TaskOrchestrationContext ctx, Onboarding onboarding){
return switch (onboarding.getStatus()) {
case REQUEST -> executeRequestState(ctx, onboarding);
case TOBEVALIDATED -> executeToBeValidatedState(ctx, onboarding);
case PENDING -> executePendingState(ctx, onboarding);
}
default -> Optional.empty();
};

}

default void executePendingState(TaskOrchestrationContext ctx, Onboarding onboarding) {
default Optional<OnboardingStatus> executePendingState(TaskOrchestrationContext ctx, Onboarding onboarding) {
String onboardingString = getOnboardingString(objectMapper(), onboarding);

//CreateInstitution activity return an institutionId that is used by CreateOnboarding activity
Expand All @@ -39,8 +41,6 @@ default void executePendingState(TaskOrchestrationContext ctx, Onboarding onboar
ctx.callActivity(CREATE_ONBOARDING_ACTIVITY, onboardingString, optionsRetry(), String.class).await();
ctx.callActivity(SEND_MAIL_COMPLETION_ACTIVITY, onboardingString, optionsRetry(), String.class).await();

//Last activity consist of saving pending status
String saveOnboardingStatusInput = SaveOnboardingStatusInput.buildAsJsonString(onboarding.getOnboardingId(), OnboardingStatus.COMPLETED.name());
ctx.callActivity(SAVE_ONBOARDING_STATUS_ACTIVITY, saveOnboardingStatusInput, optionsRetry(), String.class).await();
return Optional.of(OnboardingStatus.COMPLETED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.entity.Onboarding;

import static it.pagopa.selfcare.onboarding.functions.CommonFunctions.SAVE_ONBOARDING_STATUS_ACTIVITY;
import java.util.Optional;

import static it.pagopa.selfcare.onboarding.functions.utils.ActivityName.SEND_MAIL_CONFIRMATION_ACTIVITY;
import static it.pagopa.selfcare.onboarding.functions.utils.SaveOnboardingStatusInput.buildAsJsonString;
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingString;

public class WorkflowExecutorConfirmation implements WorkflowExecutor {
Expand All @@ -21,14 +21,15 @@ public WorkflowExecutorConfirmation(ObjectMapper objectMapper, TaskOptions optio
this.optionsRetry = optionsRetry;
}
@Override
public void executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
String onboardingString = getOnboardingString(objectMapper, onboarding);
ctx.callActivity(SEND_MAIL_CONFIRMATION_ACTIVITY, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SAVE_ONBOARDING_STATUS_ACTIVITY, buildAsJsonString(onboarding.getOnboardingId(), OnboardingStatus.PENDING.name()), optionsRetry, String.class).await();
return Optional.of(OnboardingStatus.PENDING);
}

@Override
public void executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
return Optional.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.entity.Onboarding;

import static it.pagopa.selfcare.onboarding.functions.CommonFunctions.SAVE_ONBOARDING_STATUS_ACTIVITY;
import java.util.Optional;

import static it.pagopa.selfcare.onboarding.functions.utils.ActivityName.*;
import static it.pagopa.selfcare.onboarding.functions.utils.SaveOnboardingStatusInput.buildAsJsonString;
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingString;

public class WorkflowExecutorContractRegistration implements WorkflowExecutor {
Expand All @@ -21,16 +21,17 @@ public WorkflowExecutorContractRegistration(ObjectMapper objectMapper, TaskOptio
this.optionsRetry = optionsRetry;
}
@Override
public void executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
String onboardingString = getOnboardingString(objectMapper, onboarding);
ctx.callActivity(BUILD_CONTRACT_ACTIVITY_NAME, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SAVE_TOKEN_WITH_CONTRACT_ACTIVITY_NAME, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SEND_MAIL_REGISTRATION_WITH_CONTRACT_ACTIVITY, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SAVE_ONBOARDING_STATUS_ACTIVITY, buildAsJsonString(onboarding.getOnboardingId(), OnboardingStatus.PENDING.name()), optionsRetry, String.class).await();
return Optional.of(OnboardingStatus.PENDING);
}

@Override
public void executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
return Optional.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.entity.Onboarding;

import static it.pagopa.selfcare.onboarding.functions.CommonFunctions.SAVE_ONBOARDING_STATUS_ACTIVITY;
import java.util.Optional;

import static it.pagopa.selfcare.onboarding.functions.utils.ActivityName.*;
import static it.pagopa.selfcare.onboarding.functions.utils.SaveOnboardingStatusInput.buildAsJsonString;
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingString;

public class WorkflowExecutorForApprove implements WorkflowExecutor {
Expand All @@ -21,19 +21,19 @@ public WorkflowExecutorForApprove(ObjectMapper objectMapper, TaskOptions options
this.optionsRetry = optionsRetry;
}
@Override
public void executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
String onboardingString = getOnboardingString(objectMapper, onboarding);
ctx.callActivity(SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SAVE_ONBOARDING_STATUS_ACTIVITY, buildAsJsonString(onboarding.getOnboardingId(), OnboardingStatus.TOBEVALIDATED.name()), optionsRetry, String.class).await();
return Optional.of(OnboardingStatus.TOBEVALIDATED);
}

@Override
public void executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
String onboardingString = getOnboardingString(objectMapper, onboarding);
ctx.callActivity(BUILD_CONTRACT_ACTIVITY_NAME, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SAVE_TOKEN_WITH_CONTRACT_ACTIVITY_NAME, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SEND_MAIL_REGISTRATION_WITH_CONTRACT_WHEN_APPROVE_ACTIVITY, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SAVE_ONBOARDING_STATUS_ACTIVITY, buildAsJsonString(onboarding.getOnboardingId(), OnboardingStatus.PENDING.name()), optionsRetry, String.class).await();
return Optional.of(OnboardingStatus.PENDING);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.entity.Onboarding;

import static it.pagopa.selfcare.onboarding.functions.CommonFunctions.SAVE_ONBOARDING_STATUS_ACTIVITY;
import java.util.Optional;

import static it.pagopa.selfcare.onboarding.functions.utils.ActivityName.SEND_MAIL_REGISTRATION_APPROVE_ACTIVITY;
import static it.pagopa.selfcare.onboarding.functions.utils.ActivityName.SEND_MAIL_REGISTRATION_REQUEST_ACTIVITY;
import static it.pagopa.selfcare.onboarding.functions.utils.SaveOnboardingStatusInput.buildAsJsonString;
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingString;

public class WorkflowExecutorForApprovePt implements WorkflowExecutor {
Expand All @@ -22,20 +22,21 @@ public WorkflowExecutorForApprovePt(ObjectMapper objectMapper, TaskOptions optio
this.optionsRetry = optionsRetry;
}
@Override
public void executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executeRequestState(TaskOrchestrationContext ctx, Onboarding onboarding) {
String onboardingString = getOnboardingString(objectMapper, onboarding);
ctx.callActivity(SEND_MAIL_REGISTRATION_REQUEST_ACTIVITY, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SEND_MAIL_REGISTRATION_APPROVE_ACTIVITY, onboardingString, optionsRetry, String.class).await();
ctx.callActivity(SAVE_ONBOARDING_STATUS_ACTIVITY, buildAsJsonString(onboarding.getOnboardingId(), OnboardingStatus.TOBEVALIDATED.name()), optionsRetry, String.class).await();
return Optional.of(OnboardingStatus.TOBEVALIDATED);
}

@Override
public void executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
executePendingState(ctx, onboarding);
public Optional<OnboardingStatus> executeToBeValidatedState(TaskOrchestrationContext ctx, Onboarding onboarding) {
return executePendingState(ctx, onboarding);
}

@Override
public void executePendingState(TaskOrchestrationContext ctx, Onboarding onboarding) {
public Optional<OnboardingStatus> executePendingState(TaskOrchestrationContext ctx, Onboarding onboarding) {
return Optional.empty();
}

@Override
Expand Down
Loading

0 comments on commit 41be6a4

Please sign in to comment.