-
Notifications
You must be signed in to change notification settings - Fork 356
/
BlobTriggerBinding.cs
199 lines (172 loc) · 8.75 KB
/
BlobTriggerBinding.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Blobs.Listeners;
using Microsoft.Azure.WebJobs.Host.Converters;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Listeners;
using Microsoft.Azure.WebJobs.Host.Protocols;
using Microsoft.Azure.WebJobs.Host.Queues;
using Microsoft.Azure.WebJobs.Host.Queues.Listeners;
using Microsoft.Azure.WebJobs.Host.Timers;
using Microsoft.Azure.WebJobs.Host.Triggers;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;
namespace Microsoft.Azure.WebJobs.Host.Blobs.Triggers
{
internal class BlobTriggerBinding : ITriggerBinding
{
private readonly ParameterInfo _parameter;
private readonly StorageAccount _hostAccount;
private readonly StorageAccount _dataAccount;
private readonly CloudBlobClient _blobClient;
private readonly string _accountName;
private readonly IBlobPathSource _path;
private readonly IHostIdProvider _hostIdProvider;
private readonly JobHostQueuesOptions _queueOptions;
private readonly JobHostBlobsOptions _blobsConfiguration;
private readonly IWebJobsExceptionHandler _exceptionHandler;
private readonly IContextSetter<IBlobWrittenWatcher> _blobWrittenWatcherSetter;
private readonly SharedQueueWatcher _messageEnqueuedWatcherSetter;
private readonly ISharedContextProvider _sharedContextProvider;
private readonly ILoggerFactory _loggerFactory;
private readonly IAsyncObjectToTypeConverter<ICloudBlob> _converter;
private readonly IReadOnlyDictionary<string, Type> _bindingDataContract;
private readonly IHostSingletonManager _singletonManager;
public BlobTriggerBinding(ParameterInfo parameter,
StorageAccount hostAccount,
StorageAccount dataAccount,
IBlobPathSource path,
IHostIdProvider hostIdProvider,
JobHostQueuesOptions queueOptions,
JobHostBlobsOptions blobsConfiguration,
IWebJobsExceptionHandler exceptionHandler,
IContextSetter<IBlobWrittenWatcher> blobWrittenWatcherSetter,
SharedQueueWatcher messageEnqueuedWatcherSetter,
ISharedContextProvider sharedContextProvider,
IHostSingletonManager singletonManager,
ILoggerFactory loggerFactory)
{
_parameter = parameter ?? throw new ArgumentNullException(nameof(parameter));
_hostAccount = hostAccount ?? throw new ArgumentNullException(nameof(hostAccount));
_dataAccount = dataAccount ?? throw new ArgumentNullException(nameof(dataAccount));
_blobClient = dataAccount.CreateCloudBlobClient();
_accountName = BlobClient.GetAccountName(_blobClient);
_path = path ?? throw new ArgumentNullException(nameof(path));
_hostIdProvider = hostIdProvider ?? throw new ArgumentNullException(nameof(hostIdProvider));
_queueOptions = queueOptions ?? throw new ArgumentNullException(nameof(queueOptions));
_blobsConfiguration = blobsConfiguration ?? throw new ArgumentNullException(nameof(blobsConfiguration));
_exceptionHandler = exceptionHandler ?? throw new ArgumentNullException(nameof(exceptionHandler));
_blobWrittenWatcherSetter = blobWrittenWatcherSetter ?? throw new ArgumentNullException(nameof(blobWrittenWatcherSetter));
_messageEnqueuedWatcherSetter = messageEnqueuedWatcherSetter ?? throw new ArgumentNullException(nameof(messageEnqueuedWatcherSetter));
_sharedContextProvider = sharedContextProvider ?? throw new ArgumentNullException(nameof(sharedContextProvider));
_singletonManager = singletonManager ?? throw new ArgumentNullException(nameof(singletonManager));
_loggerFactory = loggerFactory;
_converter = CreateConverter(_blobClient);
_bindingDataContract = CreateBindingDataContract(path);
}
public Type TriggerValueType
{
get
{
return typeof(ICloudBlob);
}
}
public IReadOnlyDictionary<string, Type> BindingDataContract
{
get { return _bindingDataContract; }
}
public string ContainerName
{
get { return _path.ContainerNamePattern; }
}
public string BlobName
{
get { return _path.BlobNamePattern; }
}
public string BlobPath
{
get { return _path.ToString(); }
}
private static IReadOnlyDictionary<string, Type> CreateBindingDataContract(IBlobPathSource path)
{
var contract = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
contract.Add("BlobTrigger", typeof(string));
contract.Add("Uri", typeof(Uri));
contract.Add("Properties", typeof(BlobProperties));
contract.Add("Metadata", typeof(IDictionary<string, string>));
IReadOnlyDictionary<string, Type> contractFromPath = path.CreateBindingDataContract();
if (contractFromPath != null)
{
foreach (KeyValuePair<string, Type> item in contractFromPath)
{
// In case of conflict, binding data from the value type overrides the built-in binding data above.
contract[item.Key] = item.Value;
}
}
return contract;
}
private IReadOnlyDictionary<string, object> CreateBindingData(ICloudBlob value)
{
var bindingData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
bindingData.Add("BlobTrigger", value.GetBlobPath());
bindingData.Add("Uri", value.Uri);
bindingData.Add("Properties", value.Properties);
bindingData.Add("Metadata", value.Metadata);
IReadOnlyDictionary<string, object> bindingDataFromPath = _path.CreateBindingData(value.ToBlobPath());
if (bindingDataFromPath != null)
{
foreach (KeyValuePair<string, object> item in bindingDataFromPath)
{
// In case of conflict, binding data from the value type overrides the built-in binding data above.
bindingData[item.Key] = item.Value;
}
}
return bindingData;
}
private static IAsyncObjectToTypeConverter<ICloudBlob> CreateConverter(CloudBlobClient client)
{
return new CompositeAsyncObjectToTypeConverter<ICloudBlob>(
new BlobOutputConverter<ICloudBlob>(new AsyncConverter<ICloudBlob, ICloudBlob>(new IdentityConverter<ICloudBlob>())),
new BlobOutputConverter<string>(new StringToCloudBlobConverter(client)));
}
public async Task<ITriggerData> BindAsync(object value, ValueBindingContext context)
{
ConversionResult<ICloudBlob> conversionResult = await _converter.TryConvertAsync(value, context.CancellationToken);
if (!conversionResult.Succeeded)
{
throw new InvalidOperationException("Unable to convert trigger to IStorageBlob.");
}
IReadOnlyDictionary<string, object> bindingData = CreateBindingData(conversionResult.Result);
return new TriggerData(bindingData);
}
public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var container = _blobClient.GetContainerReference(_path.ContainerNamePattern);
var factory = new BlobListenerFactory(_hostIdProvider, _queueOptions, _blobsConfiguration, _exceptionHandler,
_blobWrittenWatcherSetter, _messageEnqueuedWatcherSetter, _sharedContextProvider, _loggerFactory,
context.Descriptor.Id, _hostAccount, _dataAccount, container, _path, context.Executor, _singletonManager);
return factory.CreateAsync(context.CancellationToken);
}
public ParameterDescriptor ToParameterDescriptor()
{
return new BlobTriggerParameterDescriptor
{
Name = _parameter.Name,
AccountName = _accountName,
ContainerName = _path.ContainerNamePattern,
BlobName = _path.BlobNamePattern,
Access = FileAccess.Read
};
}
}
}