-
Notifications
You must be signed in to change notification settings - Fork 13
/
HttpStartAndWait.cs
88 lines (77 loc) · 3.99 KB
/
HttpStartAndWait.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
// ReSharper disable once CheckNamespace
namespace DurableFunctions.Demo.DotNetCore.Starters
{
public class HttpStartAndWait
{
/// <summary>
/// This function starts a new Orchestrator in the same Function App
/// which matches the functionName parameter. If the Orchestrator completes
/// within the specified timeout the actual Orchestrator output is returned in the HttpResponseMessage.
/// If the Orchestrator takes longer to complete then the CheckStatusResponse message is returned instead.
/// </summary>
/// <param name="request">The HttpRequestMessage which can contain input data for the Orchestrator.</param>
/// <param name="orchestratorClient">An instance of the DurableOrchestrationClient used to start a new Orchestrator.</param>
/// <param name="orchestratorName">The name of the Orchestrator function to start.</param>
/// <param name="log">ILogger implementation.</param>
/// <returns>An HttpResponseMessage containing the id and status of the Orchestrator instance.</returns>
[FunctionName(nameof(HttpStartAndWait))]
public async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "startandwait/{orchestratorName}")]HttpRequestMessage request,
[DurableClient]IDurableClient orchestratorClient,
string orchestratorName,
ILogger log)
{
var orchestratorInput = await request.Content.ReadAsAsync<object>();
string instanceId = await orchestratorClient.StartNewAsync(
orchestratorName,
orchestratorInput);
log.LogInformation($"Started Orchestrator with ID = '{instanceId}'...");
var timeoutTime = GetTimeSpan(request, TimeoutQueryStringKey);
var retryIntervalTime = GetTimeSpan(request, RetryIntervalQueryStringKey);
HttpResponseMessage responseMessage = null;
if (timeoutTime == TimeSpan.Zero && retryIntervalTime == TimeSpan.Zero)
{
// Wait using the default values in the Durable Functions extension (10 sec timeout, 1 sec interval):
responseMessage = await orchestratorClient.WaitForCompletionOrCreateCheckStatusResponseAsync(
request,
instanceId);
}
if (timeoutTime != TimeSpan.Zero && retryIntervalTime == TimeSpan.Zero)
{
// Wait until the specified timeoutTime:
responseMessage = await orchestratorClient.WaitForCompletionOrCreateCheckStatusResponseAsync(
request,
instanceId,
timeoutTime);
}
if (timeoutTime != TimeSpan.Zero && retryIntervalTime != TimeSpan.Zero)
{
// Wait until the specified timeoutTime and check every retryIntervalTime:
responseMessage = await orchestratorClient.WaitForCompletionOrCreateCheckStatusResponseAsync(
request,
instanceId,
timeoutTime,
retryIntervalTime);
}
return responseMessage;
}
private static TimeSpan GetTimeSpan(HttpRequestMessage request, string queryParameterName)
{
string queryParameterStringValue = request.RequestUri.ParseQueryString()[queryParameterName];
if (string.IsNullOrEmpty(queryParameterStringValue))
{
return TimeSpan.Zero;
}
return TimeSpan.FromSeconds(double.Parse(queryParameterStringValue));
}
private const string TimeoutQueryStringKey = "timeout";
private const string RetryIntervalQueryStringKey = "interval";
}
}