Beispiel #1
0
    async Task <Try <IEnumerable <Task> > > TryStartFilter <TClientMessage, TConnectRequest, TResponse, TFilterDefinition>(
        IReverseCallDispatcher <TClientMessage, FilterRuntimeToClientMessage, TConnectRequest, FilterRegistrationResponse, FilterEventRequest, TResponse> dispatcher,
        StreamProcessor streamProcessor,
        ScopeId scopeId,
        TFilterDefinition filterDefinition,
        Func <TenantId, IFilterProcessor <TFilterDefinition> > getFilterProcessor,
        CancellationToken cancellationToken)
        where TClientMessage : IMessage, new()
        where TConnectRequest : class
        where TResponse : class
        where TFilterDefinition : IFilterDefinition
    {
        _logger.StartingFilter(filterDefinition.TargetStream);
        try
        {
            var runningDispatcher = dispatcher.Accept(new FilterRegistrationResponse(), cancellationToken);
            await streamProcessor.Initialize().ConfigureAwait(false);
            await ValidateFilter(
                scopeId,
                filterDefinition,
                getFilterProcessor,
                cancellationToken).ConfigureAwait(false);

            return(new[] { streamProcessor.Start(), runningDispatcher });
        }
        catch (Exception ex)
        {
            if (!cancellationToken.IsCancellationRequested)
            {
                _logger.ErrorWhileStartingFilter(ex, filterDefinition.TargetStream, scopeId);
            }

            return(ex);
        }
    }
Beispiel #2
0
        async Task <Try <IEnumerable <Task> > > TryStartEventHandler <TClientMessage, TConnectRequest, TResponse, TFilterDefinition>(
            IReverseCallDispatcher <TClientMessage, EventHandlerRuntimeToClientMessage, TConnectRequest, EventHandlerRegistrationResponse, HandleEventRequest, TResponse> dispatcher,
            StreamProcessor filterStreamProcessor,
            StreamProcessor eventProcessorStreamProcessor,
            ScopeId scopeId,
            TFilterDefinition filterDefinition,
            Func <IFilterProcessor <TFilterDefinition> > getFilterProcessor,
            CancellationToken cancellationToken)
            where TClientMessage : IMessage, new()
            where TConnectRequest : class
            where TResponse : class
            where TFilterDefinition : IFilterDefinition
        {
            _logger.Debug("Starting Event Handler '{EventHandlerId}'", filterDefinition.TargetStream);
            try
            {
                var runningDispatcher = dispatcher.Accept(new EventHandlerRegistrationResponse(), cancellationToken);
                await filterStreamProcessor.Initialize().ConfigureAwait(false);

                await eventProcessorStreamProcessor.Initialize().ConfigureAwait(false);
                await ValidateFilter(
                    scopeId,
                    filterDefinition,
                    getFilterProcessor,
                    cancellationToken).ConfigureAwait(false);

                return(new[] { filterStreamProcessor.Start(), eventProcessorStreamProcessor.Start(), runningDispatcher });
            }
            catch (Exception ex)
            {
                if (!cancellationToken.IsCancellationRequested)
                {
                    _logger.Warning(
                        ex,
                        "Error occurred while trying to start Event Handler '{EventHandlerId}'",
                        filterDefinition.TargetStream);
                }

                return(ex);
            }
        }
Beispiel #3
0
 /// <summary>
 /// Starts the event horizon.
 /// </summary>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public async Task Start()
 {
     try
     {
         var tasks = new TaskGroup(
             _dispatcher.Accept(new SubscriptionResponse {
             ConsentId = Consent.ToProtobuf()
         }, _cancellationTokenSource.Token),
             ProcessEventsThroughEventHorizon());
         tasks.OnFirstTaskFailure += (_, ex) => Log.ErrorOccurredInEventHorizon(
             _logger,
             ex,
             Id.ConsumerMicroservice,
             Id.ConsumerTenant,
             Id.ProducerTenant,
             Id.Partition,
             Id.PublicStream);;
         tasks.OnAllTasksCompleted += () => Log.EventHorizonStopped(
             _logger,
             Id.ConsumerMicroservice,
             Id.ConsumerTenant,
             Id.ProducerTenant,
             Id.Partition,
             Id.PublicStream);
         await tasks.WaitForAllCancellingOnFirst(_cancellationTokenSource).ConfigureAwait(false);
     }
     finally
     {
         Log.EventHorizonDisconnecting(
             _logger,
             Id.ConsumerMicroservice,
             Id.ConsumerTenant,
             Id.ProducerTenant,
             Id.Partition,
             Id.PublicStream);
     }
 }