Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a foreach loop in a durable function?

I've written my first durable function and I want to know if it possible / good practice to use a foreach loop within an orchestrator function?

The first activity in the orchestration returns a list of project ids and I want to loop through the list and execute a series of activities for each project id, using a sub orchestration.

I've created a test function and it seems to work. The only behaviour I observed was that each time the orchestrator replays and when it reaches the foreach loop, it iterates through the whole list until it reaches the current item, then executes the activities.

Any advice / opinions would be appreciated.

Thanks

like image 908
Lymedo Avatar asked Apr 19 '26 16:04

Lymedo


1 Answers

Looking at your example this is very standard Fan-out/Fan-in case. You can run the loop on the activities in parallel but make sure you are doing it asynchronously. You can find use case and example here.

https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-concepts#fan-in-out

Based on comments

This is exactly how Orchestrater is supposed to work . Orchestration is using event sourcing pattern. When Orchestrator schedule the activity it goes to sleep and when activity is finished it wakes up. Every time orchestrator wakes up it will always replay from the start and will check the execution history to see if it has already completed the given activity and moves on. So in case of loop it will schedule all the activities and goes to sleep and when wakes up it replays back from start to see if it has completed its task. I highly recommend watching following clip from Jeff hollan from Microsoft and i am sure you will have very clear idea after that.

How Orchestration works

like image 115
Imran Arshad Avatar answered Apr 23 '26 16:04

Imran Arshad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!