public async Task ContinuouslyCatchUpAllEventStreamsAsync(GlobalCheckpoint initialCommit, Func <Revision, Task> onNext)
        {
            // Set default cancellation token in case it is not provided (it will do nothing)
            CancellationToken cancellationToken = new CancellationTokenSource().Token;

            await ContinuouslyCatchUpAllEventStreamsAsync(initialCommit, onNext, cancellationToken);
        }
Example #2
0
 public EventStreamTracker(string trackerId, GlobalCheckpoint checkpoint)
 {
     this.TrackerId        = trackerId;
     this.GlobalCheckpoint = checkpoint;
     this.Type             = Readers.CheckpointType.GlobalCheckpoint;
     this.StreamCheckpoint = Readers.EventStreamCheckpoint.Empty;
 }
        public async Task ContinuouslyCatchUpAllEventStreamsAsync(GlobalCheckpoint initialCommit, Action <Revision> onNext, CancellationToken cancellationToken)
        {
            await ContinuouslyCatchUpAllEventStreams(initialCommit, async revision =>
            {
                // Execute onNext task specified by the client
                onNext(revision);

                await Task.CompletedTask;
            },
                                                     cancellationToken);
        }
        public async Task CatchUpAllEventStreamsAsync(string trackerId, Func <Revision, Task> onNext, CancellationToken cancellationToken)
        {
            await CatchUpAllEventStreams(trackerId, async state =>
            {
                // Execute onNext task specified by the client
                await onNext(state.Revision);

                state.Tracker.UpdateCheckpoint(GlobalCheckpoint.Create(state.Revision.CommitId));
                await this.TrackerRepository.CommitChanges();
            },
                                         cancellationToken);
        }
Example #5
0
        private static IObservable <IEnumerable <Revision> > AllEventStreamBatchesFrom(IEventStreamReader eventStoreClient, GlobalCheckpoint 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 AllEventStreamsSingleBatch(eventStoreClient, checkpoint,
                                                                                 NumberOfResultsPerRequest, timeout, retries, scheduler);
                return new AllEventStreamsPollingState
                {
                    LastCheckpoint = GlobalCheckpoint.Create(checkpoint.CommitId + result.Count()),
                    LastStreamBatch = result
                };
            },
                                                 value => (value.LastStreamBatch != null && value.LastStreamBatch.Count() > 0) || infinitePollingEnabled,
                                                 async value =>
            {
                var streamBatch = AllEventStreamsSingleBatch(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 AllEventStreamsPollingState
                    {
                        LastCheckpoint = GlobalCheckpoint.Create(value.LastCheckpoint.CommitId + result.Count()),
                        LastStreamBatch = result
                    };
                }
                else
                {
                    return new AllEventStreamsPollingState
                    {
                        LastCheckpoint = GlobalCheckpoint.Create(value.LastCheckpoint.CommitId),
                        LastStreamBatch = Enumerable.Empty <Revision>()
                    };
                }
            },
                                                 value => value.LastStreamBatch, scheduler));
        }
        private async Task <EventStreamTracker> GetTrackerOrCreate(string trackerId)
        {
            // Load last global checkpoint from tracker
            EventStreamTracker tracker = await this.TrackerRepository.GetEventStreamTracker(trackerId, true);

            // Create tracker if it does not exist
            if (tracker == null)
            {
                tracker = new EventStreamTracker(trackerId, GlobalCheckpoint.Create(0));
                await this.TrackerRepository.CreateEventStreamTracker(tracker);
            }

            return(tracker);
        }
        public async Task ContinuouslyCatchUpAllEventStreamsAsync(string trackerId, Action <Revision> onNext, CancellationToken cancellationToken)
        {
            //EventStreamTracker tracker = await GetTrackerOrCreate(trackerId);

            await ContinuouslyCatchUpAllEventStreams(trackerId, async (state) =>
            {
                // Execute onNext task specified by the client
                onNext(state.Revision);

                state.Tracker.UpdateCheckpoint(GlobalCheckpoint.Create(state.Revision.CommitId));
                await this.TrackerRepository.CommitChanges();
            },
                                                     cancellationToken);
        }
 private async Task CatchUpAllEventStreams(GlobalCheckpoint initialCommit, Func <Revision, Task> handleRevision, CancellationToken cancellationToken)
 {
     try
     {
         // Subscribe to all event streams and execute onNext action and save changes for each revision received
         await(EventStoreObservables.AllEventStreamsFrom(this.EventStreamReader, initialCommit)
               .SelectFromAsync(handleRevision)
               .RetryWithBackoffStrategy(retryCount: int.MaxValue, retryOnError: e => e is DbUpdateConcurrencyException)
               .ToTask(cancellationToken));
     }
     catch (InvalidOperationException ex)
     {
         if (ex.HResult == -2146233079)
         {
             // TODO: Write to log instead of the console
             Console.WriteLine(string.Format("An invalid operation exception has been thrown with code {0} when processing results from the server. Perhaps you are already up to date. Detailed error: {1}", ex.HResult, ex.Message));
         }
         else
         {
             throw ex;
         }
     }
 }
Example #9
0
 public static IObservable <Revision> ContinuousAllEventStreamsFrom(IEventStreamReader eventStoreClient, GlobalCheckpoint checkpoint,
                                                                    TimeSpan timeout, int retries = DefaultRetries, IScheduler scheduler = null)
 {
     return(ContinuousAllEventStreamsFrom(eventStoreClient, checkpoint, timeout, DefaultPollingInterval, retries, scheduler));
 }
Example #10
0
 private static IObservable <IEnumerable <Revision> > AllEventStreamsSingleBatch(IEventStreamReader eventStoreClient,
                                                                                 GlobalCheckpoint checkpoint, int numberOfResults, TimeSpan timeout, int retries, IScheduler scheduler)
 {
     return(Observable.Return(1, scheduler)
            .SelectMany(_ =>
     {
         return eventStoreClient.GetAllEventStreamsFromAsync(checkpoint.CommitId, numberOfResults);
     })
            .Do(_ => Console.WriteLine(string.Format("Requested all event streams range (commit) [{0} - {1}]", checkpoint.CommitId, checkpoint.CommitId + numberOfResults - 1)))
            .Timeout(timeout, scheduler)
            .Retry(retries)
            .Select(stream => stream.Revisions.AsEnumerable()));
 }
Example #11
0
 private static IObservable <IEnumerable <Revision> > ContinuousAllEventStreamBatchesFrom(IEventStreamReader eventStoreClient, GlobalCheckpoint checkpoint,
                                                                                          TimeSpan timeout, TimeSpan pollingInterval, int retries = DefaultRetries, IScheduler scheduler = null)
 {
     return(AllEventStreamBatchesFrom(eventStoreClient, checkpoint, timeout, pollingInterval, true, retries, scheduler));
 }
Example #12
0
 private static IObservable <IEnumerable <Revision> > AllEventStreamBatchesFrom(IEventStreamReader eventStoreClient, GlobalCheckpoint checkpoint, TimeSpan timeout,
                                                                                int retries, IScheduler scheduler)
 {
     return(AllEventStreamBatchesFrom(eventStoreClient, checkpoint, timeout, TimeSpan.Zero, false, retries, scheduler));
 }
Example #13
0
        public static IObservable <Revision> ContinuousAllEventStreamsFrom(IEventStreamReader eventStoreClient, GlobalCheckpoint checkpoint,
                                                                           TimeSpan timeout, TimeSpan pollingInterval, int retries = DefaultRetries, IScheduler scheduler = null)
        {
            return(Observable.Create <Revision>(observer =>
            {
                var poll = ContinuousAllEventStreamBatchesFrom(eventStoreClient, checkpoint, timeout, pollingInterval, retries, scheduler);

                return poll.Subscribe(
                    value =>
                {
                    foreach (var revision in value)
                    {
                        observer.OnNext(revision);
                    }
                },
                    ex => observer.OnError(ex),
                    () => observer.OnCompleted());
            }));
        }
 public async Task ContinuouslyCatchUpAllEventStreamsAsync(GlobalCheckpoint initialCommit, Func <Revision, Task> onNext, CancellationToken cancellationToken)
 {
     await ContinuouslyCatchUpAllEventStreams(initialCommit, onNext, cancellationToken);
 }
Example #15
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();
        }
Example #16
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=EventStreamReactiveReaderExample;Integrated Security=False")
                          .Options;
            var                        dbContext         = new EventStoreDbContext(options);
            IEventStore                eventStore        = new EFEventStore(dbContext, null);
            IEventStreamReader         eventStreamReader = new InProcessEventStreamReader(eventStore);
            IEventStreamReactiveReader reactiveReader    = new EventStreamReactiveReader(eventStreamReader);


            Console.WriteLine("******** REACTIVE READER EXAMPLES ********");
            Console.WriteLine();
            Console.WriteLine("CATCHING UP ALL EVENT STREAMS");

            try
            {
                t = reactiveReader.CatchUpAllEventStreamsAsync(GlobalCheckpoint.CreateFromStart(), revision =>
                {
                    Console.WriteLine(string.Format("--> Processed revision: {0}", revision.ToString()));
                });
                t.Wait();
            }
            catch (AggregateException e)
            {
                Console.WriteLine(string.Format("ERROR: {0}", e.InnerException.Message));
            }

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


            Console.WriteLine();
            Console.WriteLine("CONTINUOUSLY CATCHING UP ALL EVENT STREAMS");
            ct = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            try
            {
                t = reactiveReader.ContinuouslyCatchUpAllEventStreamsAsync(GlobalCheckpoint.CreateFromStart(), revision =>
                {
                    Console.WriteLine(string.Format("--> Processed revision: {0}", revision.ToString()));
                }, ct.Token);
                t.Wait();
            }
            catch (AggregateException e)
            {
                Console.WriteLine(string.Format("ERROR: {0}", e.InnerException.Message));
            }

            Console.WriteLine();
            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();
            Console.WriteLine("CATCHING UP ALL EVENT STREAMS (WITH ASYNCHRONOUS HANDLER)");

            try
            {
                t = reactiveReader.CatchUpAllEventStreamsAsync(GlobalCheckpoint.CreateFromStart(), async revision =>
                {
                    Console.WriteLine(string.Format("--> Processed revision: {0}", revision.ToString()));
                    await Task.FromResult(true);
                });
                t.Wait();
            }
            catch (AggregateException e)
            {
                Console.WriteLine(string.Format("ERROR: {0}", e.InnerException.Message));
            }

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

            Console.WriteLine();
            Console.WriteLine("CONTINUOUSLY CATCHING UP ALL EVENT STREAMS (WITH ASYNCHRONOUS HANDLER)");
            ct = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            try
            {
                t = reactiveReader.ContinuouslyCatchUpAllEventStreamsAsync(GlobalCheckpoint.CreateFromStart(), async revision =>
                {
                    Console.WriteLine(string.Format("--> Processed revision: {0}", revision.ToString()));
                    await Task.FromResult(true);
                }, ct.Token);
                t.Wait();
            }
            catch (AggregateException e)
            {
                Console.WriteLine(string.Format("ERROR: {0}", e.InnerException.Message));
            }

            // Dispose resources
            dbContext.Dispose();

            Console.WriteLine();
            Console.WriteLine("Press ENTER to exit...");
            Console.ReadLine();
        }
Example #17
0
 public static IObservable <Revision> AllEventStreamsFrom(IEventStreamReader eventStoreClient, GlobalCheckpoint checkpoint,
                                                          int retries = DefaultRetries, IScheduler scheduler = null)
 {
     return(AllEventStreamsFrom(eventStoreClient, checkpoint, DefaultTimeout, retries, scheduler));
 }