Home > OS >  Executing a loop in a durable functions orchestrator
Executing a loop in a durable functions orchestrator

Time:03-13

I want to send several requests to an external endpoint using a loop in a durable functions orchestrator. (I don't want to do it in parallel because of limiting in the external service.)

foreach (input in inputs)
{
    await context.CallActivityAsyncWithRetry("PostRequest", retryOptions, input);
}

From my testing, if the first request fails (after retrying according to my retryOptions) the rest of the foreach fails. Even if I wrap the request in a try/catch which swallows the failure, if the first request fails then the foreach gives up.

foreach (input in inputs)
{
    try
    {
        await context.CallActivityAsyncWithRetry("PostRequest", retryOptions, input);
    }
    catch (Exception e){}
}

Can anyone recommend a way of dealing with this? Create a sub-orchestration for each input? If any of the preceeding requests fails (after retryOptions specified), I still want to try and execute the subsequent requests in the input list.

CodePudding user response:

Loop

foreach (input in inputs)
{
  await PostRequest(dataType input);
}

Then you must have a function to handle two operations, sending a request and handle failure

public async Task PostRequest(dataType input)
{

  try
  {
    await context.CallActivityAsyncWithRetry("PostRequest", retryOptions, input);
  }
   catch (Exception e)
  {
      //handle failure 
      //retry
  }

}

CodePudding user response:

In my view each activity that you call from main trigger function has to handle exception and throw that exception. It is good if you post what is inside postrequest.

  • From your code currently it will execute one by one ( Means it execute and wait for first one to finish).
 var tasks= new List<Task>();
foreach (input in inputs)
{
   var currentTask = context.CallActivityAsyncWithRetry("PostRequest", retryOptions, input);
  currentTask.ContinueWith(ss => Console.WriteLine(ss) , TaskContinuationOptions.OnlyOnFaulted);
  tasks.Add(currentTask);
}

Task.WaitAll(tasks);
  • Related