How To Manage Durable Functions Swap Deployment In Azure Release Pipeline

What are durable functions?

 
Durable Function is an extension of Azure Functions that lets you write stateful functions in a serverless compute environment.
 
Here we will see how can we deploy durable functions in a better way when swapping.
 
When swapping slots for Azure durable functions things we need to consider or the problems we need to think about are:
  1. How to manage the state between environments?
  2. How to swap if the durable function is running in the background?
  3. How to swap stage and production environments?
  4. What do we need to swap?
  5. How storage plays an important role?
Let us have a look at the configuration setting of a function to understand in detail
 
Point to be Noted
 
Both Slot (Stage environment) and prod environment have two different configuration settings.
 
Let us talk little about highlighted settings.
  • WEBSITE_CONTENTSHARE
    This setting is having a unique string value and the file path to the function app code.

  • AzureWebJobsStorage
    The Azure Functions runtime uses this storage account connection string and also saves the code under this connection string. To swap the code both stage slot and prod must use the same storage account. So for swapping the stage and prod environments, the AzureWebJobsStorage key value (Connection string ) must be the same.
For running the durable functions it uses (blob and tables) of a storage account for that reason we are separating the storage account for managing the state so add a new DurableManagementStorage key which stores a new storage account connection string. Add the key in both (Stage & Prod) configurations.
 
How to tell Azure Durable function to use the new storage account from code?
  1. Manipulate host.json
  2. Add new key (DurableManagementStorage ) in local setting.json for local or if you want to deploy add the key in function configuration of stage and prod in Azure portal.
Below is how host.json looks after adding the required
  1. {  
  2.     "version""2.0",  
  3.     "extensions": {  
  4.         "durableTask": {  
  5.             "hubName""mycustomhub",  
  6.             "storageProvider": {  
  7.                 "connectionStringName""DurableManagementStorage"  
  8.             }  
  9.         }  
  10.     }  
  11. }  
Below is how localsettings json looks:
  1. {  
  2.     "IsEncrypted"false,  
  3.     "Values": {  
  4.         "AzureWebJobsStorage""Same Connection string for all slots",  
  5.         "FUNCTIONS_WORKER_RUNTIME""dotnet",  
  6.         "DurableManagementStorage""Diffrent connection string for all slots"  
  7.     }  
  8. }   
So far the configuration for configuring Durable functions is done.
 
Now after this, while deploying or swapping, there might be a chance the function is running and it is in the middle of the process execution and might corrupt due to deployment or swapping. To overcome this issue we will be checking the status of process execution.
 
So, how to check the status of the durable function active status?
 
The below Function helps to check the status,
  1. [FunctionName("StatusCheck")]  
  2. public static async Task < IActionResult > StatusCheck(  
  3.     [HttpTrigger(AuthorizationLevel.Function, "get""post")] HttpRequestMessage req,  
  4.     [DurableClient] IDurableOrchestrationClient client, ILogger log) {  
  5.     var runtimeStatus = new List < OrchestrationRuntimeStatus > ();  
  6.     runtimeStatus.Add(OrchestrationRuntimeStatus.Pending);  
  7.     runtimeStatus.Add(OrchestrationRuntimeStatus.Running);  
  8.     var status = await client.GetStatusAsync(new DateTime(2015, 10, 10), null, runtimeStatus);  
  9.     return new OkObjectResult(new Status() {  
  10.         HasRunning = (status.Count != 0)  
  11.     });  
  12. }   
The above code checks the status and returns true if it is active, else it returns false.
 
Based upon the status we can configure the deployment gates in the release. We need to deploy the code only when the status is false.
 
Use Azure functions from the gate and validate the status by configuring the success criteria which are available in the advanced blade.
 
The below image shows where to configure the gate in the pipeline.
 
Scroll down to see the advanced so that you can configure the success criteria
 
To be noted: Make sure the "Deployment slot setting" is checked for the DurableManagementStorage key. It says the key value should not be swapped.
 
This way we can configure and deploy the durable function by managing the state.