Ejemplo n.º 1
0
        /// <summary>
        /// Rebuilds all read models. This is a potentially lengthy operation!
        /// </summary>
        public void RebuildReadModels()
        {
            var documentStore = (IDocumentStore)this.container.Resolve(typeof(IDocumentStore));

            // wait for indexing to complete
            var indexingTask = Task.Factory.StartNew(
                () =>
                    {
                        while (true)
                        {
                            var s = documentStore.DatabaseCommands.GetStatistics().StaleIndexes;
                            if (!s.Contains("ReadModelIndex")) break;
                            Thread.Sleep(500);
                        }
                    });
            indexingTask.Wait(15000);

            // delete all read models
            documentStore.DatabaseCommands.DeleteByIndex("ReadModelIndex", new IndexQuery());

            // load all event streams and dispatch events
            var dispatcher = new EventDispatcher(this.container);
            var current = 0;
            while (true)
            {
                var session = (IDocumentSession)this.container.Resolve(typeof(IDocumentSession));
                try
                {
                    // allow indexing to take its time
                    var q =
                        session.Query<EventStream>()
                               .Customize(x => x.WaitForNonStaleResultsAsOf(DateTime.Now.AddSeconds(15)));

                    var eventStreams = q.Skip(current).Take(128).ToList();
                    if (eventStreams.Count == 0) break;
                    foreach (var eventStream in eventStreams)
                    {
                        foreach (var domainEvent in eventStream.History)
                        {
                            dispatcher.Dispatch(domainEvent, eventStream.Id);
                        }
                    }

                    session.SaveChanges();
                    current += eventStreams.Count;
                }
                finally
                {
                    this.container.Release(session);
                }
            }
        }
Ejemplo n.º 2
0
        private static void DoReplayEvents(IServiceLocator locator, IDocumentStore documentStore)
        {
            // wait for indexing to complete
            WaitForIndexing(documentStore);

            // delete all read models
            documentStore.DatabaseCommands.DeleteByIndex("ReadModelIndex", new IndexQuery());

            // load all event streams and dispatch events
            var dispatcher = new EventDispatcher(locator);

            var current = 0;
            while (true)
            {
                IDocumentSession session = null;

                try
                {
                    session = (IDocumentSession)locator.Resolve(typeof(IDocumentSession));
                    var eventsQuery =
                        session.Query<EventsIndex.Result, EventsIndex>()
                               .Customize(
                                   x => x.WaitForNonStaleResultsAsOf(DateTime.Now.AddSeconds(15)))
                               .OrderBy(x => x.ChangeSequence);
                    var results = eventsQuery.Skip(current).Take(128).ToList();
                    if (results.Count == 0) break;
                    foreach (var result in results)
                    {
                        var changeSequence = result.ChangeSequence;
                        var ids = result.Id.Select(x => x.Id);
                        var streams = session.Load<EventStream>(ids);

                        var events = from stream in streams
                                     from @event in stream.History
                                     where @event.ChangeSequence == changeSequence
                                     orderby @event.TimeStamp
                                     select new { stream.Id, Event = @event };
                        foreach (var item in events)
                        {
                            dispatcher.Dispatch(item.Event, item.Id);
                        }
                    }

                    session.SaveChanges();
                    current += results.Count;
                }
                finally
                {
                    if (session != null)
                        locator.Release(session);
                }
            }
        }
Ejemplo n.º 3
0
 public EventStoreSession(IDocumentStore documentStore, IDocumentSession documentSession, EventDispatcher dispatcher)
 {
     this.documentStore   = documentStore ?? throw new ArgumentNullException(nameof(documentStore));
     this.documentSession = documentSession ?? throw new ArgumentNullException(nameof(documentSession));
     this.dispatcher      = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
 }