Пример #1
0
 void NotifyStream(ScopeId scopeId, TypeFilterWithEventSourcePartitionDefinition filterDefinition, StreamPosition position)
 {
     if (position == StreamPosition.Start)
     {
         return;
     }
     if (filterDefinition.Public)
     {
         _streamWatcher.NotifyForEvent(filterDefinition.TargetStream, position - 1);
     }
     else
     {
         _streamWatcher.NotifyForEvent(scopeId, filterDefinition.TargetStream, position - 1);
     }
 }
 void NotifyStream(ScopeId scopeId, IStreamDefinition streamDefinition, StreamPosition position)
 {
     if (position == StreamPosition.Start)
     {
         return;
     }
     if (streamDefinition.Public)
     {
         _streamWatcher.NotifyForEvent(streamDefinition.StreamId, position - 1);
     }
     else
     {
         _streamWatcher.NotifyForEvent(scopeId, streamDefinition.StreamId, position - 1);
     }
 }
Пример #3
0
    /// <inheritdoc/>
    public async Task <EventLogSequenceNumber> Write(CommittedEvent @event, ConsentId consentId, ScopeId scope, CancellationToken cancellationToken)
    {
        _logger.WritingEventHorizonEvent(
            @event.EventLogSequenceNumber,
            @event.ExecutionContext.Tenant,
            @event.ExecutionContext.Microservice,
            scope);
        var writtenStreamPosition = await _eventsToStreamsWriter.Write(
            await _streams.GetEventLog(scope, cancellationToken).ConfigureAwait(false),
            _eventFilter,
            streamPosition => _eventConverter.ToEventLogEvent(
                new CommittedExternalEvent(
                    streamPosition.Value,
                    @event.Occurred,
                    @event.EventSource,
                    @event.ExecutionContext,
                    @event.Type,
                    false,
                    @event.Content,
                    @event.EventLogSequenceNumber,
                    DateTimeOffset.UtcNow,
                    consentId)),
            cancellationToken).ConfigureAwait(false);

        _streamWatcher.NotifyForEvent(scope, StreamId.EventLog, writtenStreamPosition);
        return(writtenStreamPosition.Value);
    }
    /// <inheritdoc/>
    public async Task Write(CommittedEvent @event, StreamId streamId, PartitionId partitionId, CancellationToken cancellationToken)
    {
        _logger.WritingEventToPublisStream(@event.EventLogSequenceNumber, streamId);
        var writtenStreamPosition = await _eventsToStreamsWriter.Write(
            await _streams.GetPublic(streamId, cancellationToken).ConfigureAwait(false),
            _filter,
            streamPosition => _eventConverter.ToStoreStreamEvent(@event, streamPosition, partitionId),
            cancellationToken).ConfigureAwait(false);

        _streamWatcher.NotifyForEvent(streamId, writtenStreamPosition);
    }
Пример #5
0
 protected static void setup_event_stream(params StreamEvent[] streamEvents)
 {
     events_fetcher
     .Setup(_ => _.Fetch(It.IsAny <StreamPosition>(), It.IsAny <CancellationToken>()))
     .Returns <StreamPosition, CancellationToken>((position, ct) =>
     {
         var events = streamEvents.Skip((int)position.Value);
         return(events.Any()
                 ? Task.FromResult(Try <IEnumerable <StreamEvent> > .Succeeded(events))
                 : Task.FromResult(Try <IEnumerable <StreamEvent> > .Failed(new Exception())));
     });
     event_waiter.NotifyForEvent(source_stream_id, (ulong)(streamEvents.Length - 1));
 }
Пример #6
0
    /// <inheritdoc/>
    public async Task Write(CommittedEvent @event, ScopeId scope, StreamId stream, PartitionId partition, CancellationToken cancellationToken)
    {
        _logger.WritingEventToStream(@event.EventLogSequenceNumber, stream, scope);
        var writtenStreamPosition = await Write(
            await _streams.Get(scope, stream, cancellationToken).ConfigureAwait(false),
            _streamFilter,
            streamPosition =>
            @event is CommittedExternalEvent externalEvent?
            _eventConverter.ToStoreStreamEvent(externalEvent, streamPosition, partition)
            : _eventConverter.ToStoreStreamEvent(@event, streamPosition, partition),
            cancellationToken).ConfigureAwait(false);

        _streamWatcher.NotifyForEvent(scope, stream, writtenStreamPosition);
    }
Пример #7
0
    /// <inheritdoc />
    public async Task <Try> Persist(Commit commit, CancellationToken cancellationToken)
    {
        var eventsToStore = _commitConverter.ToEvents(commit).ToArray();

        if (eventsToStore.Length == 0)
        {
            return(new NoEventsToCommit());
        }
        using var session = await _streams.StartSessionAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

        try
        {
            session.StartTransaction();
            await _streams.DefaultEventLog.InsertManyAsync(
                session,
                eventsToStore,
                cancellationToken : cancellationToken).ConfigureAwait(false);

            await _aggregateVersions.UpdateAggregateVersions(session, commit, cancellationToken).ConfigureAwait(false);

            await session.CommitTransactionAsync(cancellationToken).ConfigureAwait(false);

            //TODO: Notifying for events should be a concern handled by actors
            _streamWatcher.NotifyForEvent(ScopeId.Default, StreamId.EventLog, eventsToStore.Max(_ => _.EventLogSequenceNumber));
            return(Try.Succeeded());
        }
        catch (MongoWaitQueueFullException ex)
        {
            await session.AbortTransactionAsync(cancellationToken).ConfigureAwait(false);

            return(new EventStoreUnavailable("Mongo wait queue is full", ex));
        }
        catch (Exception ex)
        {
            await session.AbortTransactionAsync(cancellationToken).ConfigureAwait(false);

            return(ex);
        }
    }