Beispiel #1
0
 public async Task OnBeforeCommitAsync()
 {
     if (commandContext.UnitOfWork != null)
     {
         foreach (var ev in commandContext.UnitOfWork.EventBuffer.Events)
         {
             if (!((ev as IEventStoreEventMessage)?.Record is EventStoreRecordAdapter))
             {
                 externalEventStore.PushEvent(ev);
             }
         }
     }
 }
Beispiel #2
0
        protected async Task <List <QueuedAsyncEvent> > EnqueueEventsToRepositoryAsync()
        {
            var eventStoreEvents = eventsToQueues.Where(x =>
                                                        (x.Key as IEventStoreEventMessage)?.Record is EventStoreRecordAdapter).ToArray();
            var externalEvents = eventsToQueues.Except(eventStoreEvents).ToArray();

            var queuedEvents = new List <QueuedAsyncEvent>();

            //external
            if (externalEvents.Length > 0)
            {
                foreach (var externalEvent in externalEvents)
                {
                    externalEventStore.PushEvent(externalEvent.Key);
                }

                var externalEventRecords = await externalEventStore.CommitAsync();

                foreach (var externalEventRecord in externalEventRecords)
                {
                    if (externalEventRecord.IsDispatchedToAsyncQueues)
                    {
                        continue;
                    }

                    externalEventRecord.MarkDispatchedToAsyncQueues();
                    var sequences = externalEvents.First(x => x.Key.Metadata.GetEventId() == externalEventRecord.Id).Value;

                    foreach (var sequence in sequences)
                    {
                        AsyncEventQueue queue = await GetOrCreateQueueAsync(sequence.SequenceName, null);

                        QueuedAsyncEvent queuedEvent = new QueuedAsyncEvent(queue.Id, externalEventRecord, sequence.EventSequenceNumber);
                        crudRepository.Add(queuedEvent);
                        queuedEvents.Add(queuedEvent);
                    }
                }
            }

            //event store
            var allQueueNames = new HashSet <string>();

            foreach (var eventToQueues in eventStoreEvents)
            {
                foreach (var sequence in eventToQueues.Value)
                {
                    allQueueNames.Add(sequence.SequenceName);
                }
            }

            if (allQueueNames.Count > 10)
            {
                await LoadQueuesAsync(allQueueNames.ToArray());
            }

            foreach (var eventToQueues in eventStoreEvents)
            {
                var storeMessage   = (IEventStoreEventMessage)eventToQueues.Key;
                var eventStreamRow = ((EventStoreRecordAdapter)storeMessage.Record).EventStreamRow;
                if (eventStreamRow.IsDispatchedToAsyncQueues)
                {
                    continue;
                }

                eventStreamRow.MarkDispatchedToAsyncQueues();

                if (!crudRepository.IsAttached(eventStreamRow))
                {
                    crudRepository.Attach(eventStreamRow);
                    crudRepository.SetEntityState(eventStreamRow, EntityState.Modified);
                }

                foreach (var sequence in eventToQueues.Value)
                {
                    AsyncEventQueue queue = await GetOrCreateQueueAsync(sequence.SequenceName, null);

                    QueuedAsyncEvent queuedEvent = new QueuedAsyncEvent(queue.Id, eventStreamRow, sequence.EventSequenceNumber);
                    crudRepository.Add(queuedEvent);
                    queuedEvents.Add(queuedEvent);
                }
            }

            eventsToQueues.Clear();
            return(queuedEvents);
        }