예제 #1
0
        private bool TryDispatchEventBatch()
        {
            using (var session = store.OpenSession())
            {
                RavenQueryStatistics stats;
                var awaitingDispatching = session.Query <ExternalIntegrationDispatchRequest>().Statistics(out stats).Take(settings.ExternalIntegrationsDispatchingBatchSize).ToArray();

                if (awaitingDispatching.Length == 0)
                {
                    // If the index hasn't caught up, try again
                    return(stats.IndexEtag.CompareTo(latestEtag) < 0);
                }

                var allContexts = awaitingDispatching.Select(r => r.DispatchContext).ToArray();
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Dispatching {allContexts.Length} events.");
                }
                var eventsToBePublished = eventPublishers.SelectMany(p => p.PublishEventsForOwnContexts(allContexts, session));

                foreach (var eventToBePublished in eventsToBePublished)
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Publishing external event on the bus.");
                    }

                    try
                    {
                        bus.Publish(eventToBePublished);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("Failed dispatching external integration event.", e);

                        var m = new ExternalIntegrationEventFailedToBePublished
                        {
                            EventType = eventToBePublished.GetType()
                        };
                        try
                        {
                            m.Reason = e.GetBaseException().Message;
                        }
                        catch (Exception)
                        {
                            m.Reason = "Failed to retrieve reason!";
                        }
                        bus.Publish(m);
                    }
                }
                foreach (var dispatchedEvent in awaitingDispatching)
                {
                    session.Delete(dispatchedEvent);
                }

                session.SaveChanges();
            }

            return(true);
        }
예제 #2
0
        async Task <bool> TryDispatchEventBatch()
        {
            using (var session = store.OpenAsyncSession())
            {
                var awaitingDispatching = await session
                                          .Query <ExternalIntegrationDispatchRequest>()
                                          .Statistics(out var stats)
                                          .Take(settings.ExternalIntegrationsDispatchingBatchSize)
                                          .ToListAsync()
                                          .ConfigureAwait(false);

                if (awaitingDispatching.Count == 0)
                {
                    // If the index hasn't caught up, try again
                    return(stats.IndexEtag.CompareTo(latestEtag) < 0);
                }

                var allContexts = awaitingDispatching.Select(r => r.DispatchContext).ToArray();
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Dispatching {allContexts.Length} events.");
                }

                var eventsToBePublished = new List <object>();
                foreach (var publisher in eventPublishers)
                {
                    var events = await publisher.PublishEventsForOwnContexts(allContexts, session)
                                 .ConfigureAwait(false);

                    eventsToBePublished.AddRange(events);
                }

                foreach (var eventToBePublished in eventsToBePublished)
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Publishing external event on the bus.");
                    }

                    try
                    {
                        await bus.Publish(eventToBePublished)
                        .ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("Failed dispatching external integration event.", e);

                        var m = new ExternalIntegrationEventFailedToBePublished
                        {
                            EventType = eventToBePublished.GetType()
                        };
                        try
                        {
                            m.Reason = e.GetBaseException().Message;
                        }
                        catch (Exception)
                        {
                            m.Reason = "Failed to retrieve reason!";
                        }

                        await domainEvents.Raise(m)
                        .ConfigureAwait(false);
                    }
                }

                foreach (var dispatchedEvent in awaitingDispatching)
                {
                    session.Delete(dispatchedEvent);
                }

                await session.SaveChangesAsync()
                .ConfigureAwait(false);
            }

            return(true);
        }