How to trigger a method after all the getMaximumAttempts has been reached in workflow

Hello,

If a workflow has many childworkflow and each childworkflow has multiple activities with different MaximumAttempts set then how can we know whether all the MaximumAttempts has been reached by the parent workflow.

My usecase is to send a notification on workflow failure but currently if MaximumAttempts is N then the method is getting called N times. I want to get a control where i can call my notification on failure only when getMaximumAttempts has been reached by Parent.

Sorry, I don’t understand the scenario. What do you mean by “getMaximumAttempts has been reached by Parent”?

I have defined multiple activities inside a workflow which is under different workflow.

while defining activities we are setting its setMaximumAttempts like

public static ActivityOptions getDefaultOptionsForNotificationManagementActivity() {

	return ActivityOptions.newBuilder()
			.setRetryOptions(RetryOptions.newBuilder()
			.setInitialInterval(Duration.ofMinutes(1))
			.setMaximumAttempts(5).build())
			.setScheduleToCloseTimeout(Duration.ofHours(2)).build();

}

We need to trigger some notification if my workflow fails but as each child has n number of setMaximumAttempts so we are not able to fetch information whether the max re attempts has been reached or not as we want to send notification only after all the re attempts has been exhausted. Some thing like this : Workflow.getInfo().getAttempt()) but is there any mthod which can tell whether all the reattempts has been reached.

System.out.println(“Workflow.getInfo().getParentWorkflowId()->”+Workflow.getInfo().getParentWorkflowId());
System.out.println(“Workflow.getInfo().getParentRunId()->”+Workflow.getInfo().getParentRunId());
System.out.println(“Workflow.getInfo().getWorkflowId()->”+Workflow.getInfo().getWorkflowId());
System.out.println(“Workflow.getInfo().getWorkflowId()->”+Workflow.getInfo().getWorkflowId());
System.out.println(“Workflow.getInfo().getAttempt()->”+Workflow.getInfo().getAttempt());

I think I’m still confused.

We need to trigger some notification if my workflow fails but as each child has n number of setMaximumAttempts so we are not able to fetch information whether the max re attempts has been reached

A workflow gets an ActivityFailure exception only when all the attempts are exhausted. So if it catches the exception and sends a notification before rethrowing it to fail the workflow it would solve your problem.

We have sets of workflow.

My use case is something like this :: ParentWorkFlow has multiple ChildWorkFlow with multiple activities which has some setMaximumAttempts .
Now if something fails in Child, we have to send notification only when all the reTry attempts has been exhausted . so i am looking for any API which can inform whether the maxAttempt has been reached so that we can trigger our action only once after all the retry.

SubscriptionActivity.getDefaultOptionsForSubscriptionActivity().getRetryOptions().getMaximumAttempts();

As I said the moment the max activity retries is exhausted the workflow is going to get an ActivityFailure exception which it can handle according to your business logic.

if i understand your intentions is you want some kind of action to be taken up when a workflow fails (notification), but dont want the notification for all intermintent failues but only when the workflow fails permanently (activity/workflow do not get resubmitted etc)

so, if workflow 1 spaws workflow 2 and workflow 3, and workflow 2 fails, (workflow 1 could actually spawn wf2 again as continue as new )… so its for workflow 1 to determine when to send notification or not.

if you are reusing the workflows (say workflow 2) is independent workflow but gets embedded in workflow 1 as well, then you can always use workflow.parent kind of api to figure out if workflow 2 needs to send notification or, should it let workflow 1 manage the notification…
so net net, in your scenario the parent process/workflow should manage the notification… sub workflows should mute them self up when managed through a parent…

so i am thinking of something like this

Workflow2 {

 execute(){


  //last step is to send notification
   if(workflow.parent ==null) //indedpent workflow
  {
      /notify
  }

}
}

workflow1 {

execute() {
childworflow2.execute();

  //notifiy 
  if(wf.parent ==null)
{
   //notifiy
 }

}

}

so , my proposal is check workflow.parent before notification, and if parent is null send notification, if its not null , let parent determine what to do…
just like you handle exception , if there is no catch block, it will get rolled up to parent…

Hello,

Thanks for this info.

Want to share my exploration which i am getting which is totally different from what you have mentioned i.e activityFailure Exception is occurring multiple times based on the retry option i have set.

I have a workflow which has an activity having maximum attemp as 2 like setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(2)

public static  ActivityOptions getDefaultOptionForAccountManagementActivity() {
		
		 return 
			      ActivityOptions.newBuilder()
			          .setScheduleToCloseTimeout(Duration.ofMinutes(3))
			          .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(2).build())
			          .build();
	}

and in my workflow i am catching ActivityFailure and have placed logger to check how many times this is getting called like following

catch (ActivityFailedException  | ActivityFailure ae) {
			errMsg=ae.getMessage();
			errCode="ActivityFailedException | ActivityFailure ";
			log.error("Activity Exception roll back all previous stuff due to error: {}", errMsg);
			log.info("EnvironmentCreationWorkflowImpl :: ActivityFailure ::");
			triggerFailEmail=true;
			saga.compensate();
			throw ae;
		}

Am i missing any stuff. My only requirement is how to identify whether all the retry has been completed so that i can trigger my notification.

Are you using a log created through Workflow.getLogger to log? Otherwise it can produce duplicated entries even if the code is executed only once.

Look at the workflow execution history, do you see multiple ActivityTaskFailed events in it? Could you provide the whole workflow history?

Yes Maxim, i am using a log created by Workflow.getLogger to log

private static final Logger log = Workflow.getLogger(MyWorkflowImpl.class);

What are the workflow execution history events related to this activity?