-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVisualsProducerService.cs
More file actions
33 lines (25 loc) · 1.43 KB
/
VisualsProducerService.cs
File metadata and controls
33 lines (25 loc) · 1.43 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
namespace ConfigSample;
internal 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(20);
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);
}