-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This replaces `IsTrimmable` with `IsAotCompatible` which contains better analyzers. As a result, the deserialize and consume logic has been updated to handle specifics such as generics that cannot be inferred in AOT. Initial support for trimming in #564 did not have support for JSON serializer. Using the slim event bus meant you needed to create your own serializer and register it as a default. With this PR, the `DefaultJsonEventSerializer` support the use of a `JsonSerializerContext` where each event has been declared as `EventEnvelope<TEvent>`. Consequently, registration of consumers requires explicit events as shown in the sample
- Loading branch information
1 parent
9f1d1bb
commit 8b9bc60
Showing
49 changed files
with
1,000 additions
and
582 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
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
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<PublishAot>true</PublishAot> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Tingle.EventBus.Transports.InMemory\Tingle.EventBus.Transports.InMemory.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,88 @@ | ||
using System.Text.Json.Serialization; | ||
using Tingle.EventBus.Serialization; | ||
|
||
var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices((hostContext, services) => | ||
{ | ||
services.AddSlimEventBus(CustomSrializerContext.Default, builder => | ||
{ | ||
builder.AddConsumer<VideoUploaded, VideoUploadedConsumer>(); | ||
builder.AddDeadLetteredConsumer<VideoUploaded, VideoUploadedConsumer>(); | ||
|
||
builder.AddInMemoryTransport(); | ||
}); | ||
|
||
services.AddHostedService<ProducerService>(); | ||
}) | ||
.Build(); | ||
|
||
await host.RunAsync(); | ||
|
||
class ProducerService(IEventPublisher publisher) : BackgroundService | ||
{ | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
var delay = TimeSpan.FromSeconds(25); | ||
var times = 5; | ||
|
||
var rnd = new Random(DateTimeOffset.UtcNow.Millisecond); | ||
|
||
for (var i = 0; i < times; i++) | ||
{ | ||
var evt = new VideoUploaded | ||
{ | ||
VideoId = Convert.ToUInt32(rnd.Next()).ToString(), | ||
SizeBytes = Convert.ToUInt32(rnd.Next()), | ||
}; | ||
|
||
evt.Url = $"https://localhost:8080/uploads/raw/{evt.VideoId}.flv"; | ||
|
||
await publisher.PublishAsync(evt, cancellationToken: stoppingToken); | ||
|
||
await Task.Delay(delay, stoppingToken); | ||
} | ||
} | ||
} | ||
|
||
class VideoUploadedConsumer(ILogger<VideoUploadedConsumer> logger) : IEventConsumer<VideoUploaded>, IDeadLetteredEventConsumer<VideoUploaded> | ||
{ | ||
private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(3); | ||
|
||
public async Task ConsumeAsync(EventContext<VideoUploaded> context, CancellationToken cancellationToken = default) | ||
{ | ||
var evt = context.Event; | ||
var videoId = evt.VideoId; | ||
logger.LogInformation("Received event Id: {Id} for video '{VideoId}'.", context.Id, videoId); | ||
|
||
// Download video locally | ||
logger.LogInformation("Downloading video from {VideoUrl} ({VideoSize} bytes).", evt.Url, evt.SizeBytes); | ||
await Task.Delay(SimulationDuration, cancellationToken); // simulate using delay | ||
|
||
// Extract thumbnail from video | ||
logger.LogInformation("Extracting thumbnail from video with Id '{VideoId}'.", videoId); | ||
await Task.Delay(SimulationDuration, cancellationToken); // simulate using delay | ||
|
||
// Upload video thumbnail | ||
var thumbnailUrl = $"https://localhost:8080/uploads/thumbnails/{videoId}.jpg"; | ||
logger.LogInformation("Uploading thumbnail for video with Id '{VideoId}' to '{ThumbnailUrl}'.", videoId, thumbnailUrl); | ||
await Task.Delay(SimulationDuration, cancellationToken); // simulate using delay | ||
|
||
logger.LogInformation("Processing video with Id '{VideoId}' completed.", videoId); | ||
} | ||
|
||
public Task ConsumeAsync(DeadLetteredEventContext<VideoUploaded> context, CancellationToken cancellationToken) | ||
{ | ||
logger.LogWarning("Event with Id '{Id}' for video '{VideoId}' was dead-lettered.", context.Id, context.Event.VideoId); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
class VideoUploaded | ||
{ | ||
public string? VideoId { get; set; } | ||
public string? Url { get; set; } | ||
public long SizeBytes { get; set; } | ||
} | ||
|
||
[JsonSerializable(typeof(EventEnvelope<VideoUploaded>))] | ||
partial class CustomSrializerContext : JsonSerializerContext { } |
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,10 @@ | ||
{ | ||
"profiles": { | ||
"AotSupport": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"Microsoft": "Information", | ||
"System": "Information" | ||
}, | ||
"Console": { | ||
"FormatterName": "simple", | ||
"FormatterOptions": { | ||
"SingleLine": true, | ||
"TimestampFormat": "HH:mm:ss " | ||
} | ||
} | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.