-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEventBusGoogleCloudPubSub.cs
153 lines (124 loc) · 5.74 KB
/
EventBusGoogleCloudPubSub.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
using Autofac;
using EventBus.Abstractions;
using EventBus.Events;
using EventBus.Extensions;
using Google.Cloud.PubSub.V1;
using Google.Protobuf;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
//https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/
namespace EventBus.GoogleCloudPubSub
{
public class EventBusGoogleCloudPubSub : SubscribeProcessEvent, IEventBus, IDisposable
{
const string APP_NAME = "GoogleCloudPubSub";
const string AUTOFAC_SCOPE_NAME = "e_event_bus";
const string EVENT_NAME_ATTRIBUTE = "EVENT_NAME";
private readonly IGoogleCloudPubSubPersistentConnection _persistentConnection;
public EventBusGoogleCloudPubSub(IGoogleCloudPubSubPersistentConnection persistentConnection,
ILogger<EventBusGoogleCloudPubSub> logger,
ILifetimeScope autofac,
IEventBusSubscriptionsManager subsManager)
: base(logger, subsManager, autofac, appName: APP_NAME, autofacScopeName: AUTOFAC_SCOPE_NAME)
{
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
}
private void SubsManager_OnEventRemoved(object sender, string eventName)
{
//TODO:..
}
public void Publish(IntegrationEvent @event)
{
var eventName = @event.GetType().Name;
_logger.LogTrace("Creating GoogleCloudPubSub channel to publish event: {EventId} ({EventName})", @event.Id, eventName);
Publisher(eventName, @event).Wait();
}
public void SubscribeDynamic<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler
{
_logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
_subsManager.AddDynamicSubscription<TH>(eventName);
Consume().Wait();
}
public void Subscribe<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
var eventName = _subsManager.GetEventKey<T>();
_logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
_subsManager.AddSubscription<T, TH>();
Consume().Wait();
}
public void Unsubscribe<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
var eventName = _subsManager.GetEventKey<T>();
_logger.LogInformation("Unsubscribing from event {EventName}", eventName);
_subsManager.RemoveSubscription<T, TH>();
}
public void UnsubscribeDynamic<TH>(string eventName)
where TH : IDynamicIntegrationEventHandler
{
_subsManager.RemoveDynamicSubscription<TH>(eventName);
}
public void Dispose()
{
if (_persistentConnection != null)
{
_persistentConnection.Dispose();
}
_subsManager.Clear();
}
private async Task Publisher(string eventName, IntegrationEvent @event)
{
await _persistentConnection.Pub(async publisher =>
{
var message = new PubsubMessage { Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(@event)) };
message.Attributes.Add(EVENT_NAME_ATTRIBUTE, eventName);
// PublishAsync() has various overloads. Here we're using the string overload.
await publisher.PublishAsync(message);
// PublisherClient instance should be shutdown after use.
// The TimeSpan specifies for how long to attempt to publish locally queued messages.
await publisher.ShutdownAsync(TimeSpan.FromSeconds(1));
});
}
private async Task Consume()
{
_logger.LogTrace("Starting GoogleCloudPubSub basic consume");
while (true)
{
await _persistentConnection.Sub(subscriber =>
{
return subscriber.StartAsync((msg, cancellationToken) =>
{
_logger.LogInformation($"GoogleCloudPubSub Received message id {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
var eventName = msg.Attributes[EVENT_NAME_ATTRIBUTE];
var message = msg.Data.ToStringUtf8();
try
{
if (message.ToLowerInvariant().Contains("throw-fake-exception"))
{
throw new InvalidOperationException($"Fake exception requested: \"{message}\"");
}
base.ProcessEvent(eventName, message).Wait();
}
catch (Exception ex)
{
_logger.LogWarning(ex, "----- ERROR Processing message \"{Message}\"", message);
}
// Stop this subscriber after one message is received.
// This is non-blocking, and the returned Task may be awaited.
subscriber.StopAsync(TimeSpan.FromSeconds(1));
// Return Reply.Ack to indicate this message has been handled.
return Task.FromResult(SubscriberClient.Reply.Ack);
});
});
System.Threading.Thread.Sleep(200);
}
}
}
}