Example #1
0
        // Process the integration events
        public void ProcessEvent(string typeName, IntegrationEventReceivedNotificationDto eventReceived)
        {
            // No handlers registered
            if (_handlers.Count == 0)
            {
                return;
            }

            var eventType = GetEventTypeByName(typeName);

            var sqsResponse = (SnsMessage)JsonConvert.DeserializeObject(eventReceived.Message.Body, typeof(SnsMessage));

            var integrationEvent = JsonConvert.DeserializeObject(sqsResponse.Message, eventType);

            if (integrationEvent == null)
            {
                _logger.LogError("Error on integration event deserialization");
                return;
            }

            OnIntegrationEventReceived?.Invoke(this, eventReceived);

            foreach (var handlerCollection in _handlers)
            {
                if (handlerCollection.Key != typeName)
                {
                    continue;
                }

                foreach (var handlerValue in handlerCollection.Value)
                {
                    var handlerFactory = handlerValue.Item2;
                    var handler        = handlerFactory.DynamicInvoke();
                    var concreteType   = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType);

                    var t = concreteType.GetRuntimeMethod("Handle", new[] { eventType });
                    t.Invoke(handler, new [] { integrationEvent });

                    var handlerName = handler.ToString().Split('.').Last();

                    // Update integration event context
                    UpdateIntegrationEventContext(eventReceived, eventType, handlerName);
                }
            }
        }
Example #2
0
        private void UpdateIntegrationEventContext(
            IntegrationEventReceivedNotificationDto eventReceived,
            Type eventType,
            string handlerName)
        {
            var instances = _integrationEventsRespository.GetIntegrationEventRegisteredInstances().Result;

            if (instances == null || !instances.Values.Contains(eventType.Name))
            {
                _logger.LogWarning($"No integration event {eventType.Name} instance present, deleting message.");
                OnIntegrationEventReadyToDelete?.Invoke(this, eventReceived);
                return;
            }

            var instance = instances.First(i => i.Value == eventType.Name);

            _logger.LogTrace($"Mark sub handler as processed, Guid: {instance.Key}, Event type: {instance.Value}, Handler name: {handlerName}");

            var updatedInstance = _integrationEventsRespository.MarkSubscriberHandlerAsProcessed(
                instance.Key,
                instance.Value,
                handlerName).Result;

            if (updatedInstance == null)
            {
                throw new NullReferenceException();
            }

            // Check if the all subscribers have done
            if (updatedInstance.Subscribers.All(i => i.Item2 == true))
            {
                // Delete the message on the queue
                OnIntegrationEventReadyToDelete?.Invoke(this, eventReceived);

                // Delete instance record
                var result = _integrationEventsRespository.DeleteInstanceAsync(updatedInstance.Id,
                                                                               updatedInstance.EventType).Result;

                if (!result)
                {
                    _logger.LogError("Error on deleting integration event instance");
                }
            }
        }