async Task <IFilterResult> Filter(FilterEventRequest request, ExecutionContext executionContext, CancellationToken cancellationToken) { var response = await _dispatcher.Call(request, executionContext, cancellationToken).ConfigureAwait(false); return(response switch { { Failure : null } => new SuccessfulFiltering(response.IsIncluded, response.PartitionId),
async Task <IProcessingResult> Process(HandleEventRequest request, CancellationToken cancellationToken) { var response = await _dispatcher.Call(request, cancellationToken).ConfigureAwait(false); return(response switch { { Failure : null } => new SuccessfulProcessing(),
async Task WriteEventsToEventHorizon( IReverseCallDispatcher <EventHorizonConsumerToProducerMessage, EventHorizonProducerToConsumerMessage, ConsumerSubscriptionRequest, Contracts.SubscriptionResponse, ConsumerRequest, ConsumerResponse> dispatcher, TenantId producerTenant, StreamId publicStream, PartitionId partition, StreamPosition streamPosition, CancellationToken cancellationToken) { try { _executionContextManager.CurrentFor( _thisMicroservice, producerTenant, _executionContextManager.Current.CorrelationId); var publicEvents = await _getEventFetchers().GetPartitionedFetcherFor( ScopeId.Default, new StreamDefinition(new PublicFilterDefinition(StreamId.EventLog, publicStream)), cancellationToken).ConfigureAwait(false); while (!cancellationToken.IsCancellationRequested && !_disposed) { try { var tryGetStreamEvent = await publicEvents.FetchInPartition(partition, streamPosition, cancellationToken).ConfigureAwait(false); if (!tryGetStreamEvent.Success) { await Task.Delay(250).ConfigureAwait(false); continue; } var streamEvent = tryGetStreamEvent.Result; var response = await dispatcher.Call( new ConsumerRequest { Event = streamEvent.ToEventHorizonEvent() }, cancellationToken).ConfigureAwait(false); if (response.Failure != null) { _logger.Warning( "An error occurred while handling request. FailureId: {FailureId} Reason: {Reason}", response.Failure.Id, response.Failure.Reason); return; } streamPosition = streamEvent.Position + 1; } catch (EventStoreUnavailable) { await Task.Delay(1000).ConfigureAwait(false); } } } catch (Exception ex) { _logger.Warning(ex, "An error ocurred while writing events to event horizon"); } }
async Task <IProjectionResult> Process(ProjectionCurrentState state, CommittedEvent @event, PartitionId partitionId, ProjectionRequest request, ExecutionContext executionContext, CancellationToken token) { request.Event = new Contracts.StreamEvent { Event = @event.ToProtobuf(), PartitionId = partitionId.Value, ScopeId = Definition.Scope.ToProtobuf(), }; request.CurrentState = state.ToProtobuf(); var response = await _dispatcher.Call(request, executionContext, token).ConfigureAwait(false); return(response switch { { Failure : null, ResponseCase : ProjectionResponse.ResponseOneofCase.Replace } => new ProjectionReplaceResult(response.Replace.State),
async Task ProcessEventsThroughEventHorizon() { try { var publicEvents = await _getEventFetchers(Id.ProducerTenant).GetPartitionedFetcherFor( ScopeId.Default, new StreamDefinition(new PublicFilterDefinition(StreamId.EventLog, Id.PublicStream)), _cancellationTokenSource.Token).ConfigureAwait(false); var eventWaiter = _getStreamWaiter(Id.ProducerTenant); while (!_cancellationTokenSource.Token.IsCancellationRequested) { try { var tryGetStreamEvent = await publicEvents.FetchInPartition( Id.Partition, CurrentPosition, _cancellationTokenSource.Token).ConfigureAwait(false); if (!tryGetStreamEvent.Success) { var nextEventPositionInAnyPartition = await publicEvents.GetNextStreamPosition(_cancellationTokenSource.Token).ConfigureAwait(false); if (!nextEventPositionInAnyPartition.Success) { throw nextEventPositionInAnyPartition.Exception; } await eventWaiter.WaitForEvent( Id.PublicStream, nextEventPositionInAnyPartition, TimeSpan.FromMinutes(1), _cancellationTokenSource.Token).ConfigureAwait(false); continue; } var streamEvents = tryGetStreamEvent.Result; foreach (var streamEvent in streamEvents) { _metrics.IncrementTotalEventsWrittenToEventHorizon(); var response = await _dispatcher.Call( new ConsumerRequest { Event = streamEvent.ToEventHorizonEvent() }, _executionContexts.TryCreateUsing(streamEvent.Event.ExecutionContext), _cancellationTokenSource.Token).ConfigureAwait(false); if (response.Failure != null) { Log.ErrorOccurredWhileHandlingRequest(_logger, response.Failure.Id.ToGuid(), response.Failure.Reason); return; } CurrentPosition = streamEvent.Position + 1; } } catch (EventStoreUnavailable e) { _logger.LogWarning(e, "Event Store unavailable. Waiting 1 second"); await Task.Delay(1000).ConfigureAwait(false); } } } catch (Exception ex) { Log.ErrorWritingEventToEventHorizon(_logger, ex); } }