/// <inheritdoc/>
        public void Commit(CorrelationId correlationId, UncommittedEvents uncommittedEvents)
        {
            _logger.Information($"Committing uncommitted event stream with correlationId '{correlationId}'");
            _logger.Trace("Building the Event Store uncommitted event stream");
            var uncommitted = BuildUncommitted(uncommittedEvents, correlationId.Value);

            _logger.Trace("Committing the events");
            CommittedEventStream committed;

            using (var eventStore = _getEventStore())
            {
                committed = eventStore.Commit(uncommitted);
            }
            try
            {
                _logger.Trace("Process events in same bounded context");
                _eventProcessorHub.Process(committed);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Error processing CommittedEventStream within local event processors '{committed?.Sequence?.ToString() ??  "[NULL]"}'");
            }
            try
            {
                _logger.Trace("Passing committed events through event horizon");
                _eventHorizon.PassThrough(new CommittedEventStreamWithContext(committed, _executionContextManager.Current));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Error processing CommittedEventStream within event horizons '{committed?.Sequence?.ToString() ??  "[NULL]"}'");
            }
        }
Beispiel #2
0
        /// <inheritdoc/>
        public void Commit(CorrelationId correlationId, UncommittedEvents uncommittedEvents)
        {
            _logger.Information($"Committing uncommitted event stream with correlationId '{correlationId}'");
            _logger.Trace("Building the Event Store uncommitted event stream");
            var uncommitted = BuildUncommitted(uncommittedEvents, correlationId.Value);

            _logger.Trace("Committing the events");
            CommittedEventStream committed;

            using (var eventStore = _getEventStore())
            {
                committed = eventStore.Commit(uncommitted);
            }
            _logger.Trace("Process events in same bounded context");
            _eventProcessorHub.Process(committed);
            _logger.Trace("Passing committed events through event horizon");
            _eventHorizon.PassThrough(new CommittedEventStreamWithContext(committed, _executionContextManager.Current));
        }
        /// <summary>
        /// Injects an event
        /// </summary>
        public void InjectEvent(TenantId tenant, EventSourceId eventSourceId, IEvent @event)
        {
            _logger.Information($"Injecting event!");

            _executionContextManager.CurrentFor(tenant);
            var executionContext = _executionContextManager.Current;

            using (var eventStore = _getEventStore())
            {
                var artifact       = _artifactTypeMap.GetArtifactFor(@event.GetType());
                var eventSourceKey = new EventSourceKey(eventSourceId, artifact.Id);
                var version        = eventStore.GetNextVersionFor(eventSourceKey);

                var uncommittedEventStream = new UncommittedEventStream(
                    CommitId.New(),
                    executionContext.CorrelationId,
                    new VersionedEventSource(version, eventSourceKey),
                    DateTimeOffset.Now,
                    EventStream.From(new [] {
                    new EventEnvelope(
                        new EventMetadata(
                            EventId.New(),
                            new VersionedEventSource(version, eventSourceKey),
                            executionContext.CorrelationId,
                            artifact,
                            DateTimeOffset.Now,
                            executionContext
                            ),
                        @event.ToPropertyBag()
                        )
                })
                    );

                _logger.Information("Commit events to store");
                var committedEventStream = eventStore.Commit(uncommittedEventStream);

                _logger.Information("Process committed events");
                _processingHub.Process(committedEventStream);
            }
        }
        /// <summary>
        /// Processes
        /// </summary>
        /// <param name="committedEventStreamWithContext"></param>
        /// <returns></returns>
        public Task <CommitSequenceNumber> Process(CommittedEventStreamWithContext committedEventStreamWithContext)
        {
            try
            {
                var originatingSequence    = committedEventStreamWithContext.EventStream.Sequence;
                var context                = committedEventStreamWithContext.Context;
                var particleStream         = committedEventStreamWithContext.EventStream;
                EventSourceVersion version = null;

                _executionContextManager.CurrentFor(context);
                using (var _ = _getEventStore())
                {
                    version = _.GetNextVersionFor(particleStream.Source.Key);
                }

                var versionedEventSource = new VersionedEventSource(version, new EventSourceKey(particleStream.Source.EventSource, particleStream.Source.Artifact));

                var eventEnvelopes = new List <EventEnvelope>();

                particleStream.Events.ForEach(_ =>
                {
                    _.Metadata.OriginalContext.CommitInOrigin = particleStream.Sequence;
                    var envelope = new EventEnvelope(
                        new EventMetadata(
                            _.Id,
                            new VersionedEventSource(version, new EventSourceKey(particleStream.Source.EventSource, particleStream.Source.Artifact)),
                            _.Metadata.CorrelationId,
                            _.Metadata.Artifact,
                            _.Metadata.Occurred,
                            _.Metadata.OriginalContext
                            ),
                        _.Event
                        );
                    eventEnvelopes.Add(envelope);

                    version = version.NextSequence();
                });

                var uncommittedEventStream = new Store.UncommittedEventStream(
                    particleStream.Id,
                    particleStream.CorrelationId,
                    versionedEventSource,
                    particleStream.Timestamp,
                    new Store.EventStream(eventEnvelopes)
                    );

                _logger.Information("Commit events to store");
                Store.CommittedEventStream committedEventStream = null;
                using (var _ = _getEventStore())
                {
                    committedEventStream = _.Commit(uncommittedEventStream);
                }
                SetOffset(_eventHorizonKey, originatingSequence);
                _logger.Information("Process committed events");
                _processingHub.Process(committedEventStream);
                return(Task.FromResult(committedEventStream.Sequence));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Couldn't handle incoming commit");
                return(Task.FromException <CommitSequenceNumber>(ex));
            }
        }