-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cs
53 lines (41 loc) · 1.72 KB
/
Worker.cs
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
using System.Text;
using Microsoft.EntityFrameworkCore;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace BBQueue;
public class Worker : BackgroundService
{
private readonly IModel _channel;
private EventingBasicConsumer _consumer;
private readonly IServiceProvider _serviceProvider;
public Worker(IServiceProvider provider, IConfiguration configuration)
{
var uri = configuration.GetConnectionString("Queue");
var factory = new ConnectionFactory{Uri = new Uri(uri)};
var connection = factory.CreateConnection();
_channel = connection.CreateModel();
_channel.QueueDeclare("votes_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
_serviceProvider = provider;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
//We don't need a loop to consume votes here since they'll trigger the Received event.
_consumer = new EventingBasicConsumer(_channel);
_consumer.Received += async (channel, ea) =>
{
var body = ea.Body.ToArray();
var vote = Encoding.UTF8.GetString(body);
if (!Int32.TryParse(vote, out var id))
{
return;
}
using var scope = _serviceProvider.CreateScope();
var ctx = scope.ServiceProvider.GetRequiredService<VotesContext>();
await ctx.Contestants.Where(c => c.Id == id)
.ExecuteUpdateAsync(b => b.SetProperty(x => x.VoteQty, x => x.VoteQty + 1), stoppingToken);
_channel.BasicAck(ea.DeliveryTag, false);
};
_channel.BasicConsume("votes_queue", false, _consumer);
return Task.CompletedTask;
}
}