示例#1
0
        protected async Task StoreCheckpoint(ulong? position, CancellationToken cancellationToken) {
            _lastProcessedPosition = position;

            var checkpoint = new Checkpoint(_subscriptionId, position);

            await _checkpointStore.StoreCheckpoint(checkpoint, cancellationToken);
        }
示例#2
0
        private Task On(RawEvent rawEvent, Position?position, DateTime eventCreationTime)
        {
            var @event = _deserializer.Deserialize(rawEvent);

            _log.Debug("Projecting event {event}", rawEvent.Body.ToString());
            try
            {
                // return Measure(() =>// Should be moved to a messenger decorator
                //     {
                var envelopedEvent = new EventEnvelope(
                    new MessageChainInfo(@event.Id.ToString()
                                         , @event.Metadata.CorrelationId
                                         , @event.Metadata.CausationId)
                    , @event.Body);
                _messenger.Dispatch(envelopedEvent);
                return(_checkpointStore.StoreCheckpoint(position));
                // }, rawEvent.EventType
                // , eventCreationTime
                // , _subscriptionName);
            }
            catch (Exception exception)
            {
                _log.Error(exception
                           , "Error occured when projecting the event {event} from {subscriptionName}"
                           , @event
                           , _subscriptionName);
                throw;
            }
        }
示例#3
0
        async Task EventAppeared(
            EventStoreCatchUpSubscription _,
            ResolvedEvent resolvedEvent)
        {
            if (resolvedEvent.Event.EventType.StartsWith("$"))
            {
                return;
            }

            var @event = resolvedEvent.Deserialze();

            Log.Debug("Projecting event {event}", @event.ToString());

            try
            {
                await Task.WhenAll(_projections.Select(x => x.Project(@event)));

                await _checkpointStore.StoreCheckpoint(
                    resolvedEvent.OriginalPosition.Value
                    );
            }
            catch (Exception e)
            {
                Log.Error(
                    e,
                    "Error occured when projecting the event {event}",
                    @event
                    );
                throw;
            }
        }
        async Task Handler(StreamSubscription sub, ResolvedEvent re, CancellationToken cancellationToken)
        {
            if (re.Event.EventType.StartsWith("$"))
            {
                return;
            }

            var evt = re.Deserialize();
            await Task.WhenAll(_projections.Select(x => x.HandleEvent(evt)));

            _checkpoint.Position = (long?)re.Event.Position.CommitPosition;
            await _checkpointStore.StoreCheckpoint(_checkpoint, cancellationToken);
        }
示例#5
0
        private async Task EventAppeared(EventStoreCatchUpSubscription _, ResolvedEvent resolvedEvent)
        {
            if (resolvedEvent.Event.EventType.StartsWith("$"))
            {
                return;
            }

            var @event = resolvedEvent.Deserialzie();

            Log.Debug("Projecting event {type}", @event.GetType().Name);
            await Task.WhenAll(_projections.Select(x => x.Project(@event)));

            await _checkpointStore.StoreCheckpoint(resolvedEvent.OriginalPosition.Value);
        }
        async Task EventAppeared(
            EventStoreCatchUpSubscription _,
            ResolvedEvent resolvedEvent)
        {
            if (resolvedEvent.Event.EventType.StartsWith("$"))
            {
                return;
            }

            var @event = resolvedEvent.Deserialze();

            _log.Debug("Projecting event {event}", @event.ToString());

            try
            {
                await PrometheusMetrics.Measure(async() =>
                {
                    await Task.WhenAll(_eventHandlers.Select(x => x(@event)));

                    await _checkpointStore.StoreCheckpoint(
                        resolvedEvent.OriginalPosition.Value
                        );
                }, PrometheusMetrics.SubscriptionTimer(_subscriptionName));

                PrometheusMetrics.ObserveLeadTime(
                    resolvedEvent.Event.EventType,
                    resolvedEvent.Event.Created,
                    _subscriptionName);
            }
            catch (Exception e)
            {
                _log.Error(
                    e,
                    "Error occured when projecting the event {event}",
                    @event
                    );
                throw;
            }
        }
        async Task EventAppeared(
            EventStoreCatchUpSubscription _,
            ResolvedEvent resolvedEvent
            )
        {
            if (resolvedEvent.Event.EventType.StartsWith("$"))
            {
                return;
            }

            var @event = resolvedEvent.Deserialze();

            Log.Debug("Projecting event {event}", @event.ToString());

            try
            {
                await Task.WhenAll(
                    _subscriptions.Select(x => x.Project(@event))
                    );

                await _checkpointStore.StoreCheckpoint(
                    // ReSharper disable once PossibleInvalidOperationException
                    _isAllStream
                    ?resolvedEvent.OriginalPosition.Value.CommitPosition
                    : resolvedEvent.Event.EventNumber
                    );
            }
            catch (Exception e)
            {
                Log.Error(
                    e,
                    "Error occured when projecting the event {event}",
                    @event
                    );
                throw;
            }
        }
        public async Task Replicate(
            IEventReader reader, IEventWriter writer, ICheckpointStore checkpointStore,
            CancellationToken cancellationToken,
            Func <EventRead, bool> filter,
            Func <EventRead, ValueTask <EventWrite> > transform
            )
        {
            var cts       = new CancellationTokenSource();
            var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);
            var start     = await checkpointStore.LoadCheckpoint();

            var retryPolicy = Policy
                              .Handle <Exception>(ex => !(ex is OperationCanceledException))
                              .RetryForeverAsync(ex => Log.Warn(ex, "Writer error: {Error}, retrying", ex.Message));

            var channel = Channel.CreateBounded <EventRead>(Capacity)
                          .Source(reader.ReadEvents(start, linkedCts.Token));

            if (filter != null)
            {
                channel = channel.Filter(x => Try(x, filter));
            }

            Func <EventRead, ValueTask <EventWrite> > transformFunction;

            if (transform != null)
            {
                transformFunction = TryTransform;
            }
            else
            {
                transformFunction = DefaultTransform;
            }

            var resultChannel = channel
                                .PipeAsync(5, transformFunction, Capacity, false, linkedCts.Token)
                                .PipeAsync(WriteEvent, Capacity, true, linkedCts.Token)
                                .Batch(1024, true, true);

            var lastPosition = new Position();

            try {
                await resultChannel.ReadUntilCancelledAsync(linkedCts.Token, StoreCheckpoint, true);
            }
            catch (Exception e) {
                Log.Fatal(e, "Unable to proceed: {Error}", e.Message);
            }
            finally {
                Log.Info("Last recorded position: {Position}", lastPosition);
            }

            async ValueTask <Position> WriteEvent(EventWrite write)
            {
                await retryPolicy.ExecuteAsync(() => writer.WriteEvent(write));

                return(write.SourcePosition);
            }

            T Try <T>(EventRead evt, Func <EventRead, T> func)
            {
                try {
                    return(func(evt));
                }
                catch (Exception e) {
                    Log.Error(e, "Error in the pipeline: {Error}", e.Message);
                    cts.Cancel();
                    throw;
                }
            }

            async ValueTask <EventWrite> TryTransform(EventRead evt) => await Try(evt, transform);

            ValueTask StoreCheckpoint(List <Position> positions)
            {
                lastPosition = positions.Last();
                return(checkpointStore.StoreCheckpoint(lastPosition));
            }
        }