-
Notifications
You must be signed in to change notification settings - Fork 0
/
RabbitMQService.cs
121 lines (91 loc) · 4.38 KB
/
RabbitMQService.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
using Genie.Common;
using Genie.Common.Adapters.RabbitMQ;
using Genie.Common.Types;
using Genie.Common.Utils;
using Google.Protobuf.WellKnownTypes;
using Microsoft.IO;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Buffers;
namespace Genie.IngressConsumer.Services;
public class RabbitMQService
{
private static readonly RecyclableMemoryStreamManager manager = new();
public static async Task Start()
{
var context = GenieContext.Build().GenieContext;
await RabbitMq(context);
Console.WriteLine("RabbitMQ exited");
}
public static async Task<(IChannel IngressChannel, IChannel EventChannel)> Channels()
{
var args = new Dictionary<string, object>();
args.Add("x-max-length", 10000);
var context = GenieContext.Build().GenieContext;
var conn = await RabbitUtils.GetConnection(context.RabbitMQ, true);
var ingressChannel = await conn.CreateChannelAsync();
await ingressChannel.ExchangeDeclareAsync(context.RabbitMQ.Exchange, ExchangeType.Direct);
await ingressChannel.QueueDeclareAsync(context.RabbitMQ.Queue, false, false, false, args);
await ingressChannel.QueueBindAsync(context.RabbitMQ.Queue, context.RabbitMQ.Exchange, context.RabbitMQ.RoutingKey, null);
var eventChannel = await conn.CreateChannelAsync();
return (ingressChannel, eventChannel);
}
public static async Task RabbitMq(GenieContext context)
{
try
{
var schemaBuilder = AvroSupport.GetSchemaBuilder();
var serializer = AvroSupport.GetSerializerBuilder().BuildDelegate<EventTaskJob>(schemaBuilder.BuildSchema<EventTaskJob>());
using CancellationTokenSource cts = new();
Console.WriteLine("Starting RabbitMQ Pump: " + cts.Token);
var channels = await Channels();
var consumer = new AsyncEventingBasicConsumer(channels.IngressChannel);
var timerService = new CounterConsoleLogger();
var pump = RabbitMQPump<byte[]>.Run(
consumer,
async message =>
{
try
{
timerService.Process();
Grpc.PartyBenchmarkRequest proto = Any.Parser.ParseFrom(message.Body.ToArray()).Unpack<Grpc.PartyBenchmarkRequest>();
if (!string.IsNullOrEmpty(message.BasicProperties.ReplyTo))
{
using var ms = manager.GetStream();
serializer(new EventTaskJob
{
//Id = proto.Request.CosmosBase.Identifier.Id,
Job = "Report",
Status = EventTaskJobStatus.Completed
}, new Chr.Avro.Serialization.BinaryWriter(ms));
await channels.EventChannel.BasicPublishAsync(message.BasicProperties.ReplyTo, context.RabbitMQ.RoutingKey, ms.GetReadOnlySequence().ToArray());
}
}
catch (Exception ex)
{
timerService.ProcessError();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error:" + ex.ToString());
if (!string.IsNullOrEmpty(message.BasicProperties.ReplyTo))
{
using var ms = manager.GetStream();
serializer(new EventTaskJob
{
Exception = ex.Message,
Status = EventTaskJobStatus.Errored
}, new Chr.Avro.Serialization.BinaryWriter(ms));
await channels.EventChannel.BasicPublishAsync(message.BasicProperties.ReplyTo, context.RabbitMQ.RoutingKey, ms.GetReadOnlySequence().ToArray());
}
}
},
maxDegreeOfParallelism: 16,
cts.Token);
string consumerTag = await channels.IngressChannel.BasicConsumeAsync(context.RabbitMQ.Queue, true, consumer);
await pump.Completion;
}
catch (Exception ex)
{
await File.AppendAllTextAsync(@"c:\temp\rabbiterror.log", ex.ToString());
}
}
}