-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathTripMonitorOrchestratorTriggers.cs
140 lines (129 loc) · 5.7 KB
/
TripMonitorOrchestratorTriggers.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
128
129
130
131
132
133
134
135
136
137
138
139
140
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace ServerlessMicroservices.FunctionApp.Orchestrators
{
public static class TripMonitorOrchestratorTriggers
{
[FunctionName("T_StartTripMonitorViaQueueTrigger")]
public static async Task StartTripMonitorViaQueueTrigger(
[DurableClient] IDurableClient context,
[QueueTrigger("%TripMonitorsQueue%", Connection = "AzureWebJobsStorage")] string code,
ILogger log)
{
try
{
// The monitor instance id is the trip code + -M. This is to make sure that a Trip Manager and a monitor can co-exist
var instanceId = $"{code}-M";
await StartInstance(context, code, instanceId, log);
}
catch (Exception ex)
{
var error = $"StartTripMonitorViaQueueTrigger failed: {ex.Message}";
log.LogError(error);
}
}
[FunctionName("T_StartTripMonitor")]
public static async Task<IActionResult> StartTripMonitor([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "tripmonitors/{code}")] HttpRequest req,
[DurableClient] IDurableClient context,
string code,
ILogger log)
{
try
{
// The monitor instance id is the trip code + -M. This is to make sure that a Trip Manager and a monitor can co-exist
var instanceId = $"{code}-M";
await StartInstance(context, code, instanceId, log);
//NOTE: Unfortunately this does not work the same way as before when it was using HttpMessageResponse
//var reqMessage = req.ToHttpRequestMessage();
//var res = context.CreateCheckStatusResponse(reqMessage, trip.Code);
//res.Headers.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromSeconds(10));
//return (ActionResult)new OkObjectResult(res.Content);
return (ActionResult)new OkObjectResult("NOTE: No status URLs are returned!");
}
catch (Exception ex)
{
var error = $"StartTripMonitor failed: {ex.Message}";
log.LogError(error);
return new BadRequestObjectResult(error);
}
}
[FunctionName("T_GetTripMonitor")]
public static async Task<IActionResult> GetTripMonitor([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "tripmonitors/{code}")] HttpRequest req,
[DurableClient] IDurableClient context,
string code,
ILogger log)
{
try
{
var status = await context.GetStatusAsync(code);
if (status == null)
throw new Exception($"{code} does not exist!!");
return (ActionResult)new OkObjectResult(status);
}
catch (Exception ex)
{
var error = $"GetTripMonitor failed: {ex.Message}";
log.LogError(error);
return new BadRequestObjectResult(error);
}
}
[FunctionName("T_TerminateTripMonitor")]
public static async Task<IActionResult> TerminateTripMonitor([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "tripmonitors/{code}/terminate")] HttpRequest req,
[DurableClient] IDurableClient context,
string code,
ILogger log)
{
try
{
await TeminateInstance(context, code, log);
return (ActionResult)new OkObjectResult("Ok");
}
catch (Exception ex)
{
var error = $"TerminateTripMonitor failed: {ex.Message}";
log.LogError(error);
return new BadRequestObjectResult(error);
}
}
//TODO: Implement Get Trip Monitor Instances, Restart Trip Monitor Instances and Terminate Trip Monitor Instances if Persist to table storage if persist instances is activated
/** PRIVATE **/
private static async Task StartInstance(IDurableClient context, string code, string instanceId, ILogger log)
{
try
{
var reportStatus = await context.GetStatusAsync(instanceId);
string runningStatus = reportStatus == null ? "NULL" : reportStatus.RuntimeStatus.ToString();
log.LogInformation($"Instance running status: '{runningStatus}'.");
if (reportStatus == null || reportStatus.RuntimeStatus != OrchestrationRuntimeStatus.Running)
{
await context.StartNewAsync("O_MonitorTrip", instanceId, code);
log.LogInformation($"Started a new trip monitor = '{instanceId}'.");
// TODO: Persist to table storage if persist instances is activated
}
}
catch (Exception ex)
{
throw ex;
}
}
private static async Task TeminateInstance(IDurableClient context, string instanceId, ILogger log)
{
try
{
// TODO: Remove from table storage if persist instances is activated
log.LogInformation($"Terminating trip monitor '{instanceId}'.");
await context.TerminateAsync(instanceId, "Via an API request");
}
catch (Exception ex)
{
throw ex;
}
}
}
}