Exemplo n.º 1
0
        private Task SaveEventAsync(IntegrationEvent @event, IDbContextTransaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            var eventLogEntry = new IntegrationEventLog(@event, transaction.TransactionId);

            //if log DbContext is different from the main, pass the current tranmsaction to its DbContext
            //_bankingDbContext.Database.UseTransaction(transaction.GetDbTransaction());
            _bankingDbContext.IntegrationEventLogs.Add(eventLogEntry);
            return(_bankingDbContext.SaveChangesAsync());
        }
        public Task SaveEventAsync(IntegrationEvent @event)
        {
            var eventLogEntry = new IntegrationEventLog()
            {
                EventId       = @event.Id,
                CreationTime  = @event.CreationDate,
                EventTypeName = @event.GetType().Name,
                Content       = JsonConvert.SerializeObject(@event),
                State         = (int)EventStateEnum.NotPublished,
                TimesSent     = 0
            };

            _integrationEventLogContext.IntegrationEventLog.Add(eventLogEntry);

            return(_integrationEventLogContext.SaveChangesAsync());
        }
Exemplo n.º 3
0
        public void Save(IntegrationEvent @event)
        {
            var eventLogEntry = new IntegrationEventLog(@event);

            _integrationEventLogContext.IntegrationEventsLogs.Add(eventLogEntry);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Publishes IntegrationEvents to all subscriptions
        /// </summary>
        /// <param name="integrationEventName"> Unique name for IntegrationEvent</param>
        /// <param name="entityId">Optional parameter that specifies the entity which was affected</param>
        /// <param name="entityName">Optional parameter that specifies the name of the affected entity</param>
        /// <returns></returns>
        public async Task PublishAsync(string integrationEventName, string entityId = "", string entityName = "")
        {
            //get all subscriptions for the event
            var eventSubscriptions = _eventSubscriptionRepository.Find(0, 1).Items?.
                                     Where(s => s.IntegrationEventName == integrationEventName || s.EntityID == Guid.Parse(entityId));

            if (eventSubscriptions == null)
            {
                return;
            }

            //get current integration event
            var integrationEvent = _eventRepository.Find(0, 1).Items?.Where(e => e.Name == integrationEventName).FirstOrDefault();

            if (integrationEvent == null)
            {
                return;
            }
            WebhookPayload payload = CreatePayload(integrationEvent, entityId, entityName);

            //log integration event
            IntegrationEventLog eventLog = new IntegrationEventLog()
            {
                IntegrationEventName = integrationEventName,
                OccuredOnUTC         = DateTime.UtcNow,
                EntityType           = integrationEvent.EntityType,
                EntityID             = Guid.Parse(entityId),
                PayloadJSON          = JsonConvert.SerializeObject(payload),
                CreatedOn            = DateTime.UtcNow,
                Message    = "",
                Status     = "",
                SHA256Hash = ""
            };

            eventLog = _eventLogRepository.Add(eventLog);


            //get subscriptions that must receive webhook
            foreach (var eventSubscription in eventSubscriptions)
            {
                //handle subscriptions that should not get notified
                if (!((eventSubscription.IntegrationEventName == integrationEventName || eventSubscription.IntegrationEventName == null) &&
                      (eventSubscription.EntityID == new Guid(entityId) || eventSubscription.EntityID == null)))
                {
                    continue; //do not create an attempt in this case
                }

                //create new IntegrationEventSubscriptionAttempt
                IntegrationEventSubscriptionAttempt subscriptionAttempt = new IntegrationEventSubscriptionAttempt()
                {
                    EventLogID                     = eventLog.Id,
                    IntegrationEventName           = eventSubscription.IntegrationEventName,
                    IntegrationEventSubscriptionID = eventSubscription.Id,
                    Status         = "InProgress",
                    AttemptCounter = 0,
                    CreatedOn      = DateTime.UtcNow,
                };

                switch (eventSubscription.TransportType)
                {
                case TransportType.HTTPS:
                    //create a background job to send the webhook
                    _backgroundJobClient.Enqueue <WebhookSender>(x => x.SendWebhook(eventSubscription, payload, subscriptionAttempt));
                    break;

                case TransportType.Queue:
                    QueueItem queueItem = new QueueItem
                    {
                        Name       = eventSubscription.Name,
                        IsLocked   = false,
                        QueueId    = eventSubscription.QUEUE_QueueID ?? Guid.Empty,
                        Type       = "Json",
                        JsonType   = "IntegrationEvent",
                        DataJson   = JsonConvert.SerializeObject(payload),
                        State      = "New",
                        RetryCount = eventSubscription.Max_RetryCount ?? 1,
                        Source     = eventSubscription.IntegrationEventName,
                        Event      = integrationEvent.Description,
                        CreatedOn  = DateTime.UtcNow,
                    };
                    _queueItemRepository.Add(queueItem);
                    subscriptionAttempt.AttemptCounter = 1;
                    _attemptRepository.Add(subscriptionAttempt);
                    break;

                case TransportType.SignalR:
                    await _hub.Clients.All.SendAsync(integrationEventName, JsonConvert.SerializeObject(payload)).ConfigureAwait(false);

                    subscriptionAttempt.AttemptCounter = 1;
                    _attemptRepository.Add(subscriptionAttempt);
                    break;
                }
            }

            return;
        }
Exemplo n.º 5
0
 public IntegrationEventIsNotPersistedYetException(IntegrationEventLog integrationEventLog) : base("The event is not persisted in the storage yet. Please, make sure call a save method before update")
 {
     IntegrationEventLog = integrationEventLog;
 }
Exemplo n.º 6
0
 public void Add(IntegrationEventLog integrationEventLog) => _integrationEventLogs.InsertOne(integrationEventLog);
Exemplo n.º 7
0
 public FailToChangeEventStateException(IntegrationEventLog integrationEventLog, Exception innerException) :
     base($"Fail to mark event:{integrationEventLog.EventTypeName} as {integrationEventLog.State}", innerException)
 {
     IntegrationEventLog = integrationEventLog;
 }
Exemplo n.º 8
0
 public void Add(IntegrationEventLog integrationEventLog)
 {
     IntegrationEventLogs.Add(integrationEventLog);
 }