-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (73 loc) · 3.66 KB
/
Program.cs
File metadata and controls
93 lines (73 loc) · 3.66 KB
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
using Tingle.EventBus.Configuration;
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddEventBus(builder =>
{
// Transport agnostic configuration
builder.Configure(o => o.Naming.UseFullTypeNames = false);
builder.AddConsumer<VisualsUploadedConsumer>();
// Add transports
builder.AddInMemoryTransport("in-memory-images");
builder.AddInMemoryTransport("in-memory-videos");
});
services.AddHostedService<VisualsProducerService>();
})
.Build();
await host.RunAsync();
class VisualsProducerService(IEventPublisher publisher, ILogger<VisualsProducerService> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken); // delays a little so that the logs are better visible in a better order (only ended for sample)
logger.LogInformation("Starting production ...");
var delay = TimeSpan.FromSeconds(10);
var times = 10;
var rnd = new Random(DateTimeOffset.UtcNow.Millisecond);
for (var i = 0; i < times; i++)
{
var id = Convert.ToUInt32(rnd.Next()).ToString();
var size = Convert.ToUInt32(rnd.Next());
var image = (i % 2) == 0;
var url = $"https://localhost:8080/{(image ? "images" : "videos")}/{id}.{(image ? "png" : "flv")}";
_ = image
? await DoPublishAsync(new VideoUploaded { VideoId = id, SizeBytes = size, Url = url, }, stoppingToken)
: await DoPublishAsync(new ImageUploaded { ImageId = id, SizeBytes = size, Url = url, }, stoppingToken);
await Task.Delay(delay, stoppingToken);
}
}
private async Task<ScheduledResult?> DoPublishAsync<T>(T @event, CancellationToken cancellationToken) where T : class
=> await publisher.PublishAsync(@event, cancellationToken: cancellationToken);
}
class VisualsUploadedConsumer(ILogger<VisualsUploadedConsumer> logger) : IEventConsumer<ImageUploaded>, IEventConsumer<VideoUploaded>
{
private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(1.3f);
public async Task ConsumeAsync(EventContext<ImageUploaded> context, CancellationToken cancellationToken)
{
var id = context.Event.ImageId;
var thumbnailUrl = $"https://localhost:8080/thumbnails/{id}.jpg";
await Task.Delay(SimulationDuration, cancellationToken);
logger.LogInformation("Generated thumbnail from image '{ImageId}' at '{ThumbnailUrl}'.", id, thumbnailUrl);
}
public async Task ConsumeAsync(EventContext<VideoUploaded> context, CancellationToken cancellationToken = default)
{
var id = context.Event.VideoId;
var thumbnailUrl = $"https://localhost:8080/thumbnails/{id}.jpg";
await Task.Delay(SimulationDuration, cancellationToken);
logger.LogInformation("Generated thumbnail from video '{VideoId}' at '{ThumbnailUrl}'.", id, thumbnailUrl);
}
}
[EventTransportName("in-memory-images")] // can also be configured in the builder
class ImageUploaded
{
public string? ImageId { get; set; }
public string? Url { get; set; }
public long SizeBytes { get; set; }
}
[EventTransportName("in-memory-videos")] // can also be configured in the builder
class VideoUploaded
{
public string? VideoId { get; set; }
public string? Url { get; set; }
public long SizeBytes { get; set; }
}