[英]How to plug in cosmos change feed processor in Azure Function
我有一個 Azure Function,其中 Startup class i hook 了 Cosmos DB 的自定義配置提供程序。 但是我想在 Cosmos DB 中發生更改時更新設置。 我知道這可以通過更改提要處理器來實現。 是否可以在我的 Azure Function 中實現觀察程序來監聽更改和重新加載設置。 我不是在問如何連接 Azure Function 來監聽更改饋送處理器。
我的 IConfigurationSource 實現如下
public class CosmosConfigurationSource : IConfigurationSource
{
private readonly string _carrierId;
public CosmosConfigurationSource(string carrierId) =>
_carrierId = carrierId;
public IConfigurationProvider Build(IConfigurationBuilder builder) =>
new CosmosConfigurationProvider(_carrierId);
}
和 ConfigurationProvider
public class CosmosConfigurationProvider: Microsoft.Extensions.Configuration.ConfigurationProvider
{
private ContainerWrapper _containerWrapper;
private Container _container;
private CosmosClient _cosmosClient;
private readonly string _carrierId;
public CosmosConfigurationProvider(string carrierId)
{
_carrierId = carrierId;
init();
}
public override void Load()
{
var settings = SearchByDocumentTypeWithPartitionKey(_carrierId).GetAwaiter().GetResult();
Data = settings.AsDictionary();
}
private void init()
{
/* not relevant here */
}
private async Task<SettingsModel> SearchByDocumentTypeWithPartitionKey(string carrierId)
{
/* not relevant here*/
}
}
在我的啟動中: FunctionsStartup
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
builder.ConfigurationBuilder.Add(new CosmosConfigurationSource("405"));
}
插入自定義配置提供程序。 加載設置工作正常。
當 cosmos db 中存在的任何這些設置發生更改時,我希望它們可用甚至知道設置已更改,在我的 Azure Function 中,以便我可以刷新我的設置。
參考
更改提要處理器我在自定義配置提供程序中添加了以下方法
private async Task<ChangeFeedProcessor> StartChangeFeedProcessorAsync(string databaseName, string leaseContainerName, string sourceContainerName)
{
Container leaseContainer = _cosmosClient.GetContainer(databaseName, leaseContainerName);
ChangeFeedProcessor changeFeedProcessor = _cosmosClient.GetContainer(databaseName, sourceContainerName)
.GetChangeFeedProcessorBuilder<T2>(processorName: "changeFeedSample", onChangesDelegate: HandleChangesAsync)
.WithInstanceName("consoleHost")
.WithLeaseContainer(leaseContainer)
.Build();
Console.WriteLine("Starting Change Feed Processor...");
await changeFeedProcessor.StartAsync();
Console.WriteLine("Change Feed Processor started.");
return changeFeedProcessor;
}
async Task HandleChangesAsync(ChangeFeedProcessorContext context,
IReadOnlyCollection<T2> changes,
CancellationToken cancellationToken)
{
this.Load();
}
並在自定義配置提供程序構造器中,啟動更改提要處理器
StartChangeFeedProcessorAsync(databaseName, leaseContainerName, sourceContainerName).GetAwaiter().GetResult();
這重新加載了 CosmosDB 文檔更改的設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.