Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added full distribution of projections
- Loading branch information
1 parent
2e6f306
commit 9f869ca
Showing
16 changed files
with
300 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Kafka/Core/Kafka/KafkaProducer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Text.Json; | ||
using Confluent.Kafka; | ||
using Marten; | ||
using Marten.Events; | ||
using Marten.Events.Projections; | ||
|
||
namespace Helpdesk.Api.Core.Kafka; | ||
|
||
public class KafkaProducer: IProjection | ||
{ | ||
private const string DefaultConfigKey = "KafkaProducer"; | ||
|
||
private readonly KafkaProducerConfig config; | ||
|
||
public KafkaProducer(IConfiguration configuration) | ||
{ | ||
config = configuration.GetRequiredSection(DefaultConfigKey).Get<KafkaProducerConfig>() ?? | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
public async Task ApplyAsync(IDocumentOperations operations, IReadOnlyList<StreamAction> streamsActions, | ||
CancellationToken ct) | ||
{ | ||
foreach (var @event in streamsActions.SelectMany(streamAction => streamAction.Events)) | ||
{ | ||
await Publish(@event.Data, ct); | ||
} | ||
} | ||
|
||
public void Apply(IDocumentOperations operations, IReadOnlyList<StreamAction> streams) => | ||
throw new NotImplementedException("Producer should be only used in the AsyncDaemon"); | ||
|
||
private async Task Publish(object @event, CancellationToken ct) | ||
{ | ||
try | ||
{ | ||
using var producer = new ProducerBuilder<string, string>(config.ProducerConfig).Build(); | ||
|
||
await producer.ProduceAsync(config.Topic, | ||
new Message<string, string> | ||
{ | ||
// store event type name in message Key | ||
Key = @event.GetType().Name, | ||
// serialize event to message Value | ||
Value = JsonSerializer.Serialize(@event) | ||
}, ct).ConfigureAwait(false); | ||
} | ||
catch (Exception exc) | ||
{ | ||
Console.WriteLine(exc.Message); | ||
throw; | ||
} | ||
} | ||
} | ||
|
||
public class KafkaProducerConfig | ||
{ | ||
public ProducerConfig? ProducerConfig { get; set; } | ||
public string? Topic { get; set; } | ||
} |
19 changes: 19 additions & 0 deletions
19
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Kafka/Helpdesk.Kafka.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Marten.CommandLine" Version="6.0.5" /> | ||
<PackageReference Include="Marten" Version="6.0.5" /> | ||
<PackageReference Include="Confluent.Kafka" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Helpdesk\Helpdesk.csproj" /> | ||
</ItemGroup> | ||
</Project> |
39 changes: 39 additions & 0 deletions
39
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Kafka/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Text.Json.Serialization; | ||
using Helpdesk; | ||
using Helpdesk.Api.Core.Kafka; | ||
using Marten.Events.Projections; | ||
using Microsoft.AspNetCore.Http.Json; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Oakton; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services | ||
.AddMartenForHelpdeskAsyncOnly( | ||
builder.Configuration, | ||
(options, _) => | ||
options.Projections.Add(new KafkaProducer(builder.Configuration), ProjectionLifecycle.Async) | ||
); | ||
|
||
builder.Services | ||
.Configure<JsonOptions>(o => o.SerializerOptions.Converters.Add(new JsonStringEnumConverter())) | ||
.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(o => | ||
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())) | ||
.AddSignalR(); | ||
|
||
builder.Host.ApplyOaktonExtensions(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseCors("ClientPermission"); | ||
app.MapHub<IncidentsHub>("/hubs/incidents"); | ||
|
||
return await app.RunOaktonCommands(args); | ||
|
||
public class IncidentsHub: Hub | ||
{ | ||
} | ||
|
||
public partial class Program | ||
{ | ||
} |
14 changes: 14 additions & 0 deletions
14
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Kafka/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"profiles": { | ||
"Helpdesk.Kafka": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger/index.html", | ||
"applicationUrl": "http://localhost:5250", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Kafka/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Kafka/appsettings.Production.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"ConnectionStrings": { | ||
"Incidents": "PORT = 5432; HOST = postgres; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'" | ||
}, | ||
"KafkaProducer": { | ||
"ProducerConfig": { | ||
"BootstrapServers": "kafka:9092" | ||
}, | ||
"Topic": "Incidents" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Kafka/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"ConnectionStrings": { | ||
"Incidents": "PORT = 5432; HOST = localhost; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'" | ||
}, | ||
"KafkaProducer": { | ||
"ProducerConfig": { | ||
"BootstrapServers": "localhost:9092" | ||
}, | ||
"Topic": "Incidents" | ||
}, | ||
"Helpdesk": { | ||
"DaemonLockId": 7777 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Projections/Helpdesk.Projections.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Marten.CommandLine" Version="6.0.5" /> | ||
<PackageReference Include="Marten" Version="6.0.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Helpdesk\Helpdesk.csproj" /> | ||
</ItemGroup> | ||
</Project> |
30 changes: 30 additions & 0 deletions
30
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Projections/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Text.Json.Serialization; | ||
using Helpdesk; | ||
using Helpdesk.Incidents.GetCustomerIncidentsSummary; | ||
using Marten.Events.Projections; | ||
using Microsoft.AspNetCore.Http.Json; | ||
using Oakton; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services | ||
.AddMartenForHelpdeskAsyncOnly( | ||
builder.Configuration, | ||
(options, _) => | ||
options.Projections.Add<CustomerIncidentsSummaryProjection>(ProjectionLifecycle.Async) | ||
); | ||
|
||
builder.Services | ||
.Configure<JsonOptions>(o => o.SerializerOptions.Converters.Add(new JsonStringEnumConverter())) | ||
.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(o => | ||
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); | ||
|
||
builder.Host.ApplyOaktonExtensions(); | ||
|
||
var app = builder.Build(); | ||
|
||
return await app.RunOaktonCommands(args); | ||
|
||
public partial class Program | ||
{ | ||
} |
14 changes: 14 additions & 0 deletions
14
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Projections/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"profiles": { | ||
"Helpdesk.Projections": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger/index.html", | ||
"applicationUrl": "http://localhost:5251", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Projections/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Projections/appsettings.Production.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"ConnectionStrings": { | ||
"Incidents": "PORT = 5432; HOST = postgres; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'" | ||
}, | ||
"KafkaProducer": { | ||
"ProducerConfig": { | ||
"BootstrapServers": "kafka:9092" | ||
}, | ||
"Topic": "Incidents" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Sample/Helpdesk.ScaledUpProjections/Helpdesk.Projections/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"ConnectionStrings": { | ||
"Incidents": "PORT = 5432; HOST = localhost; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'" | ||
}, | ||
"KafkaProducer": { | ||
"ProducerConfig": { | ||
"BootstrapServers": "localhost:9092" | ||
}, | ||
"Topic": "Incidents" | ||
}, | ||
"Helpdesk": { | ||
"DaemonLockId": 5555 | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
Sample/Helpdesk.ScaledUpProjections/Helpdesk.SignalR/.dockerignore
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters