Azure Durable Functions

 











Can develop Durable Functions via Azure portal or command line, but VS is the best option.  Add Azure Development tools.


Local storage is defined here.  On Azure it will be getting setting from function apps applications settings:
Template is just a nuget package reference with some functions added:







Creating a new Azure Function


Use HTTPRequst and IActionResult

Get video url


Start first function - null is the instance ID so Video needs to be passed as the third parameter.
Be useful to let the caller of our starter function know a bit about the orchestration that they just started so the below provides json of links that can be used to check status of the orchestration as well as cancelling the orchestration 





Creating the orchestrator function with orchestration attribute highlighted.  Orchestrator is used to call actives



So using the example above the orchestrator function would turn into this:






Orchestrator methods work by going to sleep after each activity method and then returning to the start again, that's why these rules are important as we'd get a different date time each time we run them.  To avoid continually logging the same logs, we can use the following context:


Activity functions


Example, note the Activity Trigger and here we don't have the same restrictions as the orchestrator functions as we can use a normal logging context etc.:









Only retry Invalid Operation Exceptions


Fan out - Fan in pattern with Durable functions.  Allows you to run parallel tasks and works out when they are all finished before moving to next function.  Actually, very tricky to do yourself.


In the orchestrator function, create a list of tasks and then use Task.WhenAll function.  The only difference is the orchestrator will check if all these tasks are complete before moving on.


As orchestrator functions must be deterministic, if you need to read in stuff from a database or config you should do it through another activity function.  
This works as it won't run an activity function a second time so the overall orchestrator remains deterministic.


This is the activity function, which doesn't take an input but needs an input parameter to hold the ActivityTrigger


Sub-orchestration is a technique to abstract parts of the orchestration function and tidy up the code.  This is a sub orchestration of the fan out - fan in function :


Then we can replace all the fan out fan in code with an abstraction using the 
context.CallSubOrchestratorAsync method calling the nameof(function), in this case nameof(TranscodeVideoOrchestrator)






Below we have an activity function which sends and email.  It is then waiting for an external event before it will continue with the process and depending on the external event it will either run a publish video activity function or a reject activity function.


To send the external event you need to use the special orchestrator url below:

Post the body of "Approved" to that url: 

The user does not click this link directly, this will be used via Azure Function



Send an email using SendGrid and table storage bindings

Accept approved link with table storage binding


Adding a timeout to an event is very easy and throws a timeout exception, this can be used to send a reminder to the user or fail gracefully some other way.  There the timeout is just 30 seconds but in a real world application this could be set to 30 days.




Often a timer trigger function is a simpler and easier way of running a regular job but they may be some instances where an eternal orchestration is preferred.



Use ContinueAsNew to create an orchestration that waits for an external event and then changes state depending on the response and continues again.  However, Durable Entities may be a better pattern for this.



Publish from VS:

Resource group on Azure

And all the Azure Functions

Remember to set up Application settings:




Starter Function security.  When set to Function then the secret code is required within the url for it be authorised. 

Get full link from Azure site as a demo with secret key, do not share with customer









































Comments

Popular posts from this blog

Microservices and Service-Oriented Architecture

Version control and Continuous Integration/Continuous Deployment (CI/CD)

Delegates