|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | +using System.Threading; |
| 7 | +using Azure.Core.Serialization; |
| 8 | +using Microsoft.Azure.Functions.Worker.Http; |
| 9 | +using Microsoft.DurableTask.Client; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | + |
| 13 | +namespace Microsoft.Azure.Functions.Worker; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Extensions for <see cref="DurableTaskClient"/> |
| 17 | +/// </summary> |
| 18 | +public static class DurableTaskClientExtensions |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Creates an HTTP response that is useful for checking the status of the specified instance. |
| 22 | + /// </summary> |
| 23 | + /// <param name="client">The <see cref="DurableTaskClient"/>.</param> |
| 24 | + /// <param name="request">The HTTP request that this response is for.</param> |
| 25 | + /// <param name="instanceId">The ID of the orchestration instance to check.</param> |
| 26 | + /// <param name="cancellation">The cancellation token.</param> |
| 27 | + /// <returns>An HTTP 202 response with a Location header and a payload containing instance control URLs.</returns> |
| 28 | + public static HttpResponseData CreateCheckStatusResponse( |
| 29 | + this DurableTaskClient client, |
| 30 | + HttpRequestData request, |
| 31 | + string instanceId, |
| 32 | + CancellationToken cancellation = default) |
| 33 | + { |
| 34 | + return client.CreateCheckStatusResponse(request, instanceId, HttpStatusCode.Accepted, cancellation); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Creates an HTTP response that is useful for checking the status of the specified instance. |
| 39 | + /// </summary> |
| 40 | + /// <param name="client">The <see cref="DurableTaskClient"/>.</param> |
| 41 | + /// <param name="request">The HTTP request that this response is for.</param> |
| 42 | + /// <param name="instanceId">The ID of the orchestration instance to check.</param> |
| 43 | + /// <param name="statusCode">The status code.</param> |
| 44 | + /// <param name="cancellation">The cancellation token.</param> |
| 45 | + /// <returns>An HTTP response with a Location header and a payload containing instance control URLs.</returns> |
| 46 | + public static HttpResponseData CreateCheckStatusResponse( |
| 47 | + this DurableTaskClient client, |
| 48 | + HttpRequestData request, |
| 49 | + string instanceId, |
| 50 | + HttpStatusCode statusCode, |
| 51 | + CancellationToken cancellation = default) |
| 52 | + { |
| 53 | + if (client is null) |
| 54 | + { |
| 55 | + throw new ArgumentNullException(nameof(client)); |
| 56 | + } |
| 57 | + |
| 58 | + static string BuildUrl(string url, params string?[] queryValues) |
| 59 | + { |
| 60 | + bool appended = false; |
| 61 | + foreach (string? query in queryValues) |
| 62 | + { |
| 63 | + if (!string.IsNullOrEmpty(query)) |
| 64 | + { |
| 65 | + url = url + (appended ? "&" : "?") + query; |
| 66 | + appended = true; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return url; |
| 71 | + } |
| 72 | + |
| 73 | + // TODO: To better support scenarios involving proxies or application gateways, this |
| 74 | + // code should take the X-Forwarded-Host, X-Forwarded-Proto, and Forwarded HTTP |
| 75 | + // request headers into consideration and generate the base URL accordingly. |
| 76 | + // More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded. |
| 77 | + // One potential workaround is to set ASPNETCORE_FORWARDEDHEADERS_ENABLED to true. |
| 78 | + string baseUrl = request.Url.GetLeftPart(UriPartial.Authority); |
| 79 | + string formattedInstanceId = Uri.EscapeDataString(instanceId); |
| 80 | + string instanceUrl = $"{baseUrl}/runtime/webhooks/durabletask/instances/{formattedInstanceId}"; |
| 81 | + string? commonQueryParameters = GetQueryParams(client); |
| 82 | + |
| 83 | + HttpResponseData response = request.CreateResponse(statusCode); |
| 84 | + response.Headers.Add("Location", BuildUrl(instanceUrl, commonQueryParameters)); |
| 85 | + response.Headers.Add("Content-Type", "application/json"); |
| 86 | + |
| 87 | + ObjectSerializer serializer = GetObjectSerializer(response); |
| 88 | + var payload = new |
| 89 | + { |
| 90 | + id = instanceId, |
| 91 | + purgeHistoryDeleteUri = BuildUrl(instanceUrl, commonQueryParameters), |
| 92 | + sendEventPostUri = BuildUrl($"{instanceUrl}/raiseEvent/{{eventName}}", commonQueryParameters), |
| 93 | + statusQueryGetUri = BuildUrl(instanceUrl, commonQueryParameters), |
| 94 | + terminatePostUri = BuildUrl($"{instanceUrl}/terminate", "reason={{text}}}", commonQueryParameters), |
| 95 | + }; |
| 96 | + |
| 97 | + serializer.Serialize(response.Body, payload, payload.GetType(), cancellation); |
| 98 | + return response; |
| 99 | + } |
| 100 | + |
| 101 | + private static ObjectSerializer GetObjectSerializer(HttpResponseData response) |
| 102 | + { |
| 103 | + return response.FunctionContext.InstanceServices.GetService<IOptions<WorkerOptions>>()?.Value?.Serializer |
| 104 | + ?? throw new InvalidOperationException("A serializer is not configured for the worker."); |
| 105 | + } |
| 106 | + |
| 107 | + private static string? GetQueryParams(DurableTaskClient client) |
| 108 | + { |
| 109 | + return client is FunctionsDurableTaskClient functions ? functions.QueryString : null; |
| 110 | + } |
| 111 | +} |
0 commit comments