-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRabbitManager.cs
112 lines (91 loc) · 4.07 KB
/
RabbitManager.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
/* unified/ban - Management and protection systems
© fabricators SRL, https://fabricators.ltd , https://unifiedban.solutions
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License with our addition
to Section 7 as published in unified/ban's the GitHub repository.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License and the
additional terms along with this program.
If not, see <https://docs.fabricators.ltd/docs/licenses/unifiedban>.
For more information, see Licensing FAQ:
https://docs.fabricators.ltd/docs/licenses/faq */
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using Unifiedban.Next.Common.Telegram;
namespace Unifiedban.Next.Terminal.Telegram;
internal class RabbitManager
{
private static IConnection? _conn;
private static IModel? _resultsChannel;
private static IBasicProperties? _resultsProperties;
private static IModel? _fanoutChannel;
private static IBasicProperties? _fanoutProperties;
public void Init()
{
Common.Utils.WriteLine("Creating RabbitMQ instance...");
var factory = new ConnectionFactory();
factory.UserName = CacheData.Configuration?["RabbitMQ:UserName"];
factory.Password = CacheData.Configuration?["RabbitMQ:Password"];
factory.VirtualHost = CacheData.Configuration?["RabbitMQ:VirtualHost"];
factory.HostName = CacheData.Configuration?["RabbitMQ:HostName"];
factory.Port = int.Parse(CacheData.Configuration?["RabbitMQ:Port"] ?? "0");
factory.DispatchConsumersAsync = true;
Common.Utils.WriteLine("Connecting to RabbitMQ server...");
_conn = factory.CreateConnection();
}
public void Start()
{
_resultsChannel = _conn.CreateModel();
_resultsProperties = _resultsChannel.CreateBasicProperties();
var resultsConsumer = new AsyncEventingBasicConsumer(_resultsChannel);
resultsConsumer.Received += ResultsConsumerOnReceived;
_fanoutChannel = _conn.CreateModel();
_resultsProperties = _fanoutChannel.CreateBasicProperties();
var fanoutConsumer = new AsyncEventingBasicConsumer(_fanoutChannel);
fanoutConsumer.Received += FanoutConsumerOnReceived;
Common.Utils.WriteLine("Start consuming queues...");
_resultsChannel.BasicConsume("tg.results", false, resultsConsumer);
_fanoutChannel.BasicConsume("tg.terminal.fanout", false, fanoutConsumer);
}
public void Shutdown()
{
_resultsChannel?.Close();
_conn?.Close();
}
private async Task ResultsConsumerOnReceived(object sender, BasicDeliverEventArgs ea)
{
try
{
var body = ea.Body.ToArray();
var str = System.Text.Encoding.Default.GetString(body);
var actionRequest = JsonConvert.DeserializeObject<ActionRequest>(str, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
MessageQueueManager.EnqueueMessage(actionRequest);
}
catch (Exception ex)
{
Common.Utils.WriteLine(ex.Message, 3);
Common.Utils.WriteLine(ex.InnerException?.Message, 3);
}
_resultsChannel!.BasicAck(ea.DeliveryTag, false);
await Task.Yield();
}
private async Task FanoutConsumerOnReceived(object sender, BasicDeliverEventArgs ea)
{
var body = ea.Body.ToArray();
var str = System.Text.Encoding.Default.GetString(body);
}
internal static void PublishMessage(string exchange, string routingKey, byte[] body)
{
if (_resultsChannel is { IsClosed: true }) return;
_resultsChannel.BasicPublish(exchange, routingKey, _resultsProperties, body);
}
}