Skip to content

Commit

Permalink
Function cosmos db trigger that adds message to service bus queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Torkelsen committed Oct 14, 2024
1 parent baab22c commit 8d06abd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.11.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.8.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
</ItemGroup>
Expand Down
28 changes: 24 additions & 4 deletions handlenett-backend/serverless/functions/ItemsChangeFeed.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Logging;

namespace HandlenettNotifications
{
public class ItemsChangeFeed
{
private readonly ILogger _logger;
private IQueueClient? queueClient;
private readonly ILogger<ItemsChangeFeed> _logger;

public ItemsChangeFeed(ILoggerFactory loggerFactory)
public ItemsChangeFeed(ILogger<ItemsChangeFeed> logger)
{
_logger = loggerFactory.CreateLogger<ItemsChangeFeed>();
_logger = logger;
}

//only works sometimes, likely because of low throughput on free cost tier
[Function("ItemsChangeFeed")]
public void Run([CosmosDBTrigger(
public async Task Run([CosmosDBTrigger(
databaseName: "Handlenett",
containerName: "ShoppingListItems",
Connection = "ConnectionStrings--AzureFunctionsCosmosDB",
Expand All @@ -29,6 +33,22 @@ public void Run([CosmosDBTrigger(
{
_logger.LogInformation("Documents modified: " + input.Count);
_logger.LogInformation("First document Id: " + input[0].id);


queueClient = new QueueClient(
Environment.GetEnvironmentVariable("ConnectionStrings--ServiceBus"),
"cosmosdb-updates");

foreach (var document in input)
{
var messageBody = $"Document Id: {document.id} changed";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));

await queueClient.SendAsync(message);
_logger.LogInformation($"Message sent to queue: {messageBody}");
}

await queueClient.CloseAsync();
}
else
{
Expand Down
29 changes: 26 additions & 3 deletions handlenett-backend/serverless/functions/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

//logging not working
//var host = new HostBuilder()
// .ConfigureFunctionsWebApplication()
// .ConfigureServices(services =>
// {
// services.AddApplicationInsightsTelemetryWorkerService();
// services.ConfigureFunctionsApplicationInsights();
// })
// .Build();

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureFunctionsWebApplication() // Keep this as ASP.NET Core-based Functions use this
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddApplicationInsightsTelemetry();

// Optional: Custom logging configuration
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddApplicationInsights();
loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);
loggingBuilder.AddConsole();
});
})
.ConfigureLogging(logging =>
{
logging.AddConsole(); // Adds Console Logging
logging.AddApplicationInsights(); // Adds Application Insights Logging
})
.Build();

Expand Down
3 changes: 2 additions & 1 deletion handlenett-backend/serverless/functions/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"logging": {
"logLevel": {
"Default": "Information",
"Function": "Information"
"Function": "Information",
"ApplicationInsights": "Information"
},
"applicationInsights": {
"samplingSettings": {
Expand Down

0 comments on commit 8d06abd

Please sign in to comment.