How to terminate a Durable orchestration with custom status in Azure Durable Functions

2.3k views Asked by At

I have a Durable Function, with the rough outline as shown in the code block below. If I end up in the else section, it is because of a functional issue (in this case, because I don't have authentication details cached). And I would like to "self-terminate" the current instance of my orchestration. I can do this by throwing an exception, but I would like to find a cleaner way (if there is) to terminate the active instance with a friendly message, indicating the reason for the Termination.

Is this possible, and if yes, how?

[FunctionName(nameof(UserHistorySyncWorkflow))]
public async Task<List<Match>> RunOrchestrator(
  [OrchestrationTrigger] IDurableOrchestrationContext orchestrationContext, ILogger logger)
{
  if(CanConnect())
  {
    // Call Activity function and return results
  }
  else
  {
    // How to gracefully Terminate here?
  }
}
1

There are 1 answers

2
krishg On

You just simply return from the else block. It will terminate gracefully. But status will be 'completed', custom status is not possible. You can log a message for the reason.

[FunctionName(nameof(UserHistorySyncWorkflow))]
public async Task<List<Match>> RunOrchestrator(
  [OrchestrationTrigger] IDurableOrchestrationContext orchestrationContext, ILogger logger)
{
  if(CanConnect())
  {
    // Call Activity function and return results
  }
  else
  {
    // Gracefully terminating.
    logger.LogInformation("Exiting since nothing to do!");
    return null;
  }
}

enter image description here

If you want to mark as 'failed', just throw exception. I don't see any problem in that.

[FunctionName(nameof(UserHistorySyncWorkflow))]
public async Task<List<Match>> RunOrchestrator(
  [OrchestrationTrigger] IDurableOrchestrationContext orchestrationContext, ILogger logger)
{
  if(CanConnect())
  {
    // Call Activity function and return results
  }
  else
  {
    throw new FunctionException("Exiting since nothing to do!");
  }
}