-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDurableFunctionsOrchestrationTicTacToe.cs
127 lines (99 loc) · 5.32 KB
/
DurableFunctionsOrchestrationTicTacToe.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace Mk.Function
{
public static class DurableFunctionsOrchestrationTicTacToe
{
static string _testMessage;
[FunctionName("DurableFunctionsOrchestrationTicTacToe")]
public static async Task<List<string>> RunOrchestrator(
[OrchestrationTrigger] IDurableOrchestrationContext context,
ILogger log)
{
var timeoutCts = new CancellationTokenSource();
var requestTimeoutAt = context.CurrentUtcDateTime.AddSeconds(60);
var timeoutTask = context.CreateTimer(requestTimeoutAt, timeoutCts.Token);
var serverReadyTask = context.WaitForExternalEvent<string>("EventCheck");
var nextEvent = await Task.WhenAny(timeoutTask, serverReadyTask);
var outputs = new List<string>();
string serverInfo;
if (nextEvent == serverReadyTask)
{
// Get server IP:Port from the event body
serverInfo = serverReadyTask.Result;
log.LogInformation($"serverInfo inside if= '{serverInfo}'" );
}
else // Timeout happened
{
serverInfo = "timed out";
log.LogInformation($"timeoutInfo serverInfo= '{serverInfo}'" );
}
if (!timeoutTask.IsCompleted)
{
log.LogInformation($"About to cancel" );
timeoutCts.Cancel();
}
// Replace "hello" with the name of your Durable Activity Function.
outputs.Add(await context.CallActivityAsync<string>("DurableFunctionsOrchestrationTicTacToe_Hello", "Tokyo"));
outputs.Add(await context.CallActivityAsync<string>("DurableFunctionsOrchestrationTicTacToe_Hello", "Seattle"));
outputs.Add(await context.CallActivityAsync<string>("DurableFunctionsOrchestrationTicTacToe_Hello", "London"));
// returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
return outputs;
}
[FunctionName("DurableFunctionsOrchestrationTicTacToe_Hello")]
public static string SayHello([ActivityTrigger] string name, ILogger log)
{
log.LogInformation($"Saying hello to {name}.");
return $"Hello {name}!";
}
[FunctionName("DurableFunctionsOrchestrationTicTacToe_HttpStart")]
public static async Task<HttpResponseMessage> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
[DurableClient] IDurableOrchestrationClient starter,
ILogger log)
{
OrchestrationStatusQueryCondition status = new OrchestrationStatusQueryCondition();
List<Microsoft.Azure.WebJobs.Extensions.DurableTask.OrchestrationRuntimeStatus> sL = new List<Microsoft.Azure.WebJobs.Extensions.DurableTask.OrchestrationRuntimeStatus>();
sL.Add(OrchestrationRuntimeStatus.Running);
sL.Add(OrchestrationRuntimeStatus.Completed);
status.RuntimeStatus = sL;
string instanceId;
// Function input comes from the request content.
if(string.IsNullOrEmpty(_testMessage)){
instanceId = await starter.StartNewAsync("DurableFunctionsOrchestrationTicTacToe", null);
_testMessage = instanceId;
}else{
instanceId = await starter.StartNewAsync<string>("DurableFunctionsOrchestrationTicTacToe", _testMessage, null);
}
var openInstances = await starter.GetStatusAsync(instanceId, true, false, false);
log.LogInformation($"This is number of running instances: '{openInstances.RuntimeStatus}'");
log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
log.LogInformation($"Test Mesage = '{_testMessage}'.");
return starter.CreateCheckStatusResponse(req, instanceId);
}
[FunctionName("CallSayHello")]
public static async Task<HttpResponseMessage> CheckSayHello (
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
[DurableClient] IDurableOrchestrationClient starter,
ILogger log)
{
// var currentStatus = await starter.GetStatusAsync(instanceId, true, false, false);
// log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
// string oId = req.Query["oId"];
string name = req.Query["name"];
if(!string.IsNullOrEmpty(_testMessage)){
// Leveraging the fact that the orchestrator instance ID is the player GUID
await starter.RaiseEventAsync(_testMessage, "EventCheck", name);
}
return new HttpResponseMessage();
}
}
}