Example #1
0
        private static IObservable <IEnumerable <Revision> > EventStreamBatchesFrom(IEventStreamReader eventStoreClient, EventStreamCheckpoint checkpoint,
                                                                                    TimeSpan timeout, TimeSpan pollingInterval, bool infinitePollingEnabled = false, int retries = DefaultRetries, IScheduler scheduler = null)
        {
            scheduler = scheduler ?? Scheduler.Default;

            return(ObservableExtensions.Generate(async() =>
            {
                IEnumerable <Revision> result = await EventStreamSingleBatch(eventStoreClient, checkpoint,
                                                                             NumberOfResultsPerRequest, timeout, retries, scheduler);
                return new EventStreamPollingState
                {
                    LastCheckpoint = EventStreamCheckpoint.CreateStreamCheckpoint(checkpoint.StreamId, checkpoint.RevisionId + result.Count(), 0),
                    LastStreamBatch = result
                };
            },
                                                 value => (value.LastStreamBatch != null && value.LastStreamBatch.Count() > 0) || infinitePollingEnabled,
                                                 async value =>
            {
                var streamBatch = EventStreamSingleBatch(eventStoreClient, value.LastCheckpoint,
                                                         NumberOfResultsPerRequest, timeout, retries, scheduler);

                IEnumerable <Revision> result = null;

                //Introduce delay before performing next request (if required)
                var delay = Observable.Empty <IEnumerable <Revision> >().Delay(pollingInterval);
                if (value.LastStreamBatch == null || value.LastStreamBatch.Count() == 0 && infinitePollingEnabled)
                {
                    result = await delay.Concat(streamBatch);
                }
                else
                {
                    result = await streamBatch;
                }

                if (result != null && result.Count() > 0)
                {
                    return new EventStreamPollingState
                    {
                        LastCheckpoint = EventStreamCheckpoint.CreateStreamCheckpoint(value.LastCheckpoint.StreamId, value.LastCheckpoint.RevisionId + result.Count(), 0),
                        LastStreamBatch = result
                    };
                }
                else
                {
                    return new EventStreamPollingState
                    {
                        LastCheckpoint = EventStreamCheckpoint.CreateStreamCheckpoint(value.LastCheckpoint.StreamId, value.LastCheckpoint.RevisionId, 0),
                        LastStreamBatch = Enumerable.Empty <Revision>()
                    };
                }
            },
                                                 value => value.LastStreamBatch, scheduler));
        }
Example #2
0
        static void Main(string[] args)
        {
            Task t = null;
            CancellationTokenSource ct = null;

            AppInitializer.Initialize();

            var options = new DbContextOptionsBuilder <EventStoreDbContext>()
                          .UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=EventStreamReactiveObservablesExample;Integrated Security=False")
                          .Options;
            var                dbContext         = new EventStoreDbContext(options);
            IEventStore        eventStore        = new EFEventStore(dbContext, null);
            IEventStreamReader eventStreamReader = new InProcessEventStreamReader(eventStore);


            Console.WriteLine("CATCHING UP EVENT STREAM");
            t = EventStoreObservables.EventStreamFrom(eventStreamReader, EventStreamCheckpoint.CreateStreamCheckpoint("19e2a7fc-d0eb-44b9-8348-e7678ccd37bc", 0, 0))
                .ForEachAsync(revision => Console.WriteLine("--> Aggregate: " + revision.AggregateType + " - Revision: " + revision.RevisionId));

            t.Wait();
            Console.WriteLine("Completed (up to date)");

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to continue...");
            Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine("CONTINUOUSLY CATCHING UP EVENT STREAM");
            ct = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            try
            {
                t = EventStoreObservables.ContinuousEventStreamFrom(eventStreamReader, EventStreamCheckpoint.CreateStreamCheckpoint("19e2a7fc-d0eb-44b9-8348-e7678ccd37bc", 0, 0))
                    .ForEachAsync(revision => Console.WriteLine("--> Aggregate: " + revision.AggregateType + " - Revision: " + revision.RevisionId), ct.Token);
                t.Wait();
            }
            catch (AggregateException e)
            {
                if (e.InnerException is TaskCanceledException)
                {
                    Console.WriteLine("ERROR: The operation has been canceled => " + e.InnerException.Message);
                }
                else
                {
                    throw e;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to continue...");
            Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine("CATCHING UP ALL EVENT STREAMS");
            t = EventStoreObservables.AllEventStreamsFrom(eventStreamReader, GlobalCheckpoint.Create(27))
                .ForEachAsync(revision => Console.WriteLine("--> Aggregate: " + revision.AggregateType + " - Revision: " + revision.RevisionId));

            t.Wait();
            Console.WriteLine("Completed (up to date)");

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to continue...");
            Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine("CONTINUOUSLY CATCHING UP ALL EVENT STREAMS");
            ct = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            try
            {
                t = EventStoreObservables.ContinuousAllEventStreamsFrom(eventStreamReader, GlobalCheckpoint.Create(1))
                    .ForEachAsync(revision => Console.WriteLine("--> Aggregate: " + revision.AggregateType + " - Revision: " + revision.RevisionId), ct.Token);
                t.Wait();
            }
            catch (AggregateException e)
            {
                if (e.InnerException is TaskCanceledException)
                {
                    Console.WriteLine("ERROR: The operation has been canceled => " + e.Message);
                }
                else
                {
                    throw e;
                }
            }

            // Dispose resources
            dbContext.Dispose();

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to exit...");
            Console.ReadLine();
        }