-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathBindingHelper.cs
91 lines (75 loc) · 3.37 KB
/
BindingHelper.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs.Extensions.DurableTask
{
internal class BindingHelper
{
private const string InstanceIdPlaceholder = "INSTANCEID";
private readonly DurableTaskExtension config;
private readonly EndToEndTraceHelper traceHelper;
public BindingHelper(DurableTaskExtension config, EndToEndTraceHelper traceHelper)
{
this.config = config;
this.traceHelper = traceHelper;
}
public IAsyncCollector<StartOrchestrationArgs> CreateAsyncCollector(OrchestrationClientAttribute clientAttribute)
{
DurableOrchestrationClientBase client = this.config.GetClient(clientAttribute);
return new OrchestrationClientAsyncCollector(client);
}
public string DurableOrchestrationClientToString(DurableOrchestrationClient client, OrchestrationClientAttribute attr)
{
var payload = new OrchestrationClientInputData
{
TaskHubName = client.TaskHubName,
CreationUrls = this.config.HttpApiHandler.GetInstanceCreationLinks(),
ManagementUrls = this.config.HttpApiHandler.CreateHttpManagementPayload(InstanceIdPlaceholder, attr?.TaskHub, attr?.ConnectionName),
};
return JsonConvert.SerializeObject(payload);
}
public StartOrchestrationArgs JObjectToStartOrchestrationArgs(JObject input, OrchestrationClientAttribute attr)
{
return input?.ToObject<StartOrchestrationArgs>();
}
public StartOrchestrationArgs StringToStartOrchestrationArgs(string input, OrchestrationClientAttribute attr)
{
return !string.IsNullOrEmpty(input) ? JsonConvert.DeserializeObject<StartOrchestrationArgs>(input) : null;
}
private class OrchestrationClientAsyncCollector : IAsyncCollector<StartOrchestrationArgs>
{
private readonly DurableOrchestrationClientBase client;
public OrchestrationClientAsyncCollector(DurableOrchestrationClientBase client)
{
this.client = client;
}
public Task AddAsync(StartOrchestrationArgs args, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
return Task.CompletedTask;
}
return this.client.StartNewAsync(
args.FunctionName,
args.InstanceId,
args.Input);
}
public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return Task.CompletedTask;
}
}
private class OrchestrationClientInputData
{
[JsonProperty("taskHubName")]
public string TaskHubName { get; set; }
[JsonProperty("creationUrls")]
public HttpCreationPayload CreationUrls { get; set; }
[JsonProperty("managementUrls")]
public HttpManagementPayload ManagementUrls { get; set; }
}
}
}