Skip to content

Latest commit

 

History

History
119 lines (77 loc) · 5.67 KB

05_create_orchestration_client.md

File metadata and controls

119 lines (77 loc) · 5.67 KB

Lab 5 - Creating the orchestration client function

Goal

The goal of this lab is to create an orchestration client function which accepts the detected NEO messages from the Servicebus and starts a new instance of an orchestration. The orchestrator function will be created in the next lab and should not be created as part of this lab.

Steps

1. Reference the Durable Functions extension

In order to use the Durable Functions API we need a reference to this NuGet package:

Microsoft.Azure.WebJobs.Extensions.DurableTask

2. Specify a task hub name

Durable Functions uses Table Storage to checkpoint the state of the orchestration and Storage Queues to schedule the orchestrator and its activities. You have control over the prefix used in naming these tables and queues by specifying the hubName setting in the host.json file:

{
  "version": "2.0",
  "extensions": {
    "durableTask": {
      "hubName": "NEOEventsV1"
    }
  }
}

For more info about storage and task hubs please read the Task hubs in Durable Functions documentation.

Since we'll be making a lot of (breaking) changes to the orchestration in the next labs, I suggest we add a version suffix to the hubName each time we make a (breaking) change to the orchestrator code. By doing this, Durable Functions will create new tables & queues, and we won't run into issues running new orchestrator code with old data.

3. Update the Servicebus triggered function

Now update the signature of the Servicebus triggered function method and add the DurableClient attribute to give the function the durable client function role:

[FunctionName(nameof(NeoEventProcessingClientServicebus))]
public async Task Run(
    [ServiceBusTrigger("neo-events", "<YOUR_PERSONAL_TOPIC_SUBSCRIPTIONNAME>", Connection = "NEOEventsTopic")]string message, 
    [DurableClient]IDurableClient orchestrationClient,
    ILogger log)

By adding this line to the function method the Durable Functions framework will inject an instance of the DurableClient which is used to manage orchestration instances.

New orchestration function instances are started by passing in the name of the orchestration function to the StartNewAsync method like this:

var instanceId = await orchestrationClient.StartNewAsync(
                    "NeoEventProcessingOrchestrator",
                    detectedNeoEvent)

The method returns the ID of the orchestration instance that is scheduled to start.

By using the syntax above update the function to start a new instance of the NeoEventProcessingOrchestrator function. Do not create the actual NeoEventProcessingOrchestrator function just yet!

Note that the method is asynchronous and the result is awaited. You probably need to update your function signature to make it async and return a Task.

4. Build & run locally

Do a build of the project.

Do we get any warnings about a missing orchestration function?

Now run/debug your local Function App.

What is the output from the Azure Functions Runtime in the console once the function is triggered? What does the failure say?

5. More control when debugging locally

Messages are continuously being pushed to the Servicebus topic. This makes it quite difficult for us when we're debugging because new orchestrators are being instantiated every couple of seconds.

Lets disable the ServicebusTrigger for now and create a complementary HttpTrigger which we can start ourselves.

Disabling the ServicebusTrigger function

Functions can be disabled by adding this line to the application settings in local.settings.json:

"AzureWebJobs.<FUNCTION_NAME>.Disabled": true

Adding an HttpTrigger function

Now add an HttpTrigger function, which responds to a POST to the api/start route. The function should extract the DetectedNeoEvent from the message body and start a new orchestator function in the same way as the ServicebusTrigger function.

Since the trigger is now Http based we can return an HttpResponseMessage with the ID of the started orchestration.

  • Change the return type to Task<HttpResonseMessage>
  • Use the CreateCheckStatusResponse method to return the status response object.

The implementation should look something like this:

[FunctionName(nameof(NeoEventProcessingClientHttp))]
public async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "POST", Route = "start")]HttpRequestMessage message,
    [DurableClient]IDurableClient orchestrationClient,
    ILogger log)
{
    var detectedNeoEvent = await message.Content.ReadAsAsync<DetectedNeoEvent>();
    var instanceId = await orchestrationClient.StartNewAsync("NeoEventProcessingOrchestrator",
        detectedNeoEvent);

    log.LogInformation($"HTTP started orchestration with ID {instanceId}.");

    return orchestrationClient.CreateCheckStatusResponse(message, instanceId);
}

6. Test the HttpTrigger client function

Now trigger the HttpTrigger client function by using the request in the start_orchestration.http file. You can direcly execute the requests in this file by using VSCode and the REST Client extension.

Durable Functions has an HTTP API which allows management of the orchestrator instances. This can be very useful for debugging and maintenance. The start_orchestration.http file also contains two requests to get the result of orchestrations.

Continue to the next lab to create an orchestrator function.