This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRabbitEFService.cs
156 lines (132 loc) · 5.42 KB
/
RabbitEFService.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Exceptions;
namespace rabbithole
{
class RabbitEFService : IHostedService, IDisposable
{
private const string Queue = "rabbithole";
private readonly EventSinkContext ctx;
private readonly ILogger<RabbitEFService> logger;
private readonly IApplicationLifetime lifetime;
private RabbitConfig rabbitConfig;
private IConnection rabbitConn;
private IModel channel;
private string tag;
public RabbitEFService(IConfiguration configuration, ILogger<RabbitEFService> logger, IApplicationLifetime applifetime, EventSinkContext context)
{
rabbitConfig = new RabbitConfig();
configuration.Bind("RabbitMQ", rabbitConfig);
lifetime = applifetime;
this.logger = logger;
ctx = context;
}
public Task StartAsync(CancellationToken cancellationToken)
{
return ListenAndStore();
}
public Task ListenAndStore()
{
var tries = 0;
logger.LogInformation($"Connecting to broker {rabbitConfig.HostName} (vhost: {rabbitConfig.VirtualHost}).");
do {
try {
var factory = new ConnectionFactory()
{
HostName = rabbitConfig.HostName,
Password = rabbitConfig.Password,
UserName = rabbitConfig.UserName,
VirtualHost = rabbitConfig.VirtualHost,
};
factory.Ssl.Enabled = rabbitConfig.Ssl.Enabled;
factory.Ssl.ServerName = rabbitConfig.Ssl.ServerName;
rabbitConn = factory.CreateConnection();
channel = rabbitConn.CreateModel();
} catch (BrokerUnreachableException ex) {
logger.LogError($"Unable to connect to broker. {ex.Message}");
tries++;
if (tries >= 10) {
logger.LogCritical($"Unable to connect to broker after {tries} tries.");
lifetime.StopApplication();
return Task.FromException(ex);
}
Thread.Sleep(500*tries);
}
} while (channel == null);
var queue = channel.QueueDeclare(Queue, durable: true, exclusive: false, autoDelete: false);
foreach (var binding in rabbitConfig.Bindings)
{
channel.ExchangeDeclare(binding.Exchange, ExchangeType.Topic, durable: true, autoDelete: true);
channel.QueueBind(queue: queue.QueueName,
exchange: binding.Exchange,
routingKey: binding.Topic);
logger.LogInformation($"Subscribing to {binding.Exchange}:{binding.Topic}");
}
var consumer = new EventingBasicConsumer(channel);
consumer.Received += OnEventRecieved();
tag = channel.BasicConsume(queue: Queue,
autoAck: true,
consumer: consumer);
logger.LogInformation("Listening for messages.");
return Task.CompletedTask;
}
private EventHandler<BasicDeliverEventArgs> OnEventRecieved()
{
return (model, ea) =>
{
var e = new Event()
{
Recieved = DateTime.UtcNow,
Exchange = ea.Exchange,
RoutingKey = ea.RoutingKey,
Content = Encoding.UTF8.GetString(ea.Body),
};
try {
System.Json.JsonValue.Parse(e.Content);
} catch (Exception ex) {
logger.LogError($"Unable to parse message from {e.Exchange}:{e.RoutingKey}.");
return;
}
logger.LogDebug($"Storing ({e.Recieved}, {e.Exchange}, {e.RoutingKey}, {e.Content} ) to database.");
ctx.Events.Add(e);
ctx.SaveChanges();
};
}
public Task StopAsync(CancellationToken cancellationToken)
{
if (channel != null) {
channel.BasicCancel(tag);
}
return Task.CompletedTask;
}
private Task TearDown() {
if (channel != null) {
foreach (var binding in rabbitConfig.Bindings)
{
channel.QueueBind(queue: Queue,
exchange: binding.Exchange,
routingKey: binding.Topic);
channel.ExchangeDelete(binding.Exchange, false);
logger.LogInformation("Subscribing to {binding.Exchange}:{binding.Topic}");
}
}
return Task.CompletedTask;
}
public void Dispose()
{
if (channel != null) {
channel.Dispose();
}
if (rabbitConn != null) {
rabbitConn.Dispose();
}
}
}
}