Exemplo n.º 1
0
        void ISourceConnector <TTo> .Start(ChainExecuteDelegate <TTo> execute)
        {
            Task <ExecutionResult> ExecuteWrapper(TFrom arg, CancellationToken token)
            {
                return(execute(_mapper.Invoke(arg), token));
            }

            _wrappedConnector.Start(ExecuteWrapper);
        }
Exemplo n.º 2
0
        void ISourceConnector <T> .Start(ChainExecuteDelegate <T> execute)
        {
            if (!_completion.IsCompleted)
            {
                throw new NotSupportedException();
            }

            _executeCancellationTokenSource = new CancellationTokenSource();
            _sourceCancellationTokenSource  = new CancellationTokenSource();
            var executeCancellationToken = _executeCancellationTokenSource.Token;
            var sourceCancellationToken  = _sourceCancellationTokenSource.Token;

            Task <ExecutionResult> ExecuteWrapper(T arg) => execute(arg, executeCancellationToken);

            _completion = Task.Run(async() =>
            {
                var sourceExecutionId  = Guid.NewGuid();
                using var serviceScope = _applicationServices.CreateScope();

                var logger = serviceScope.ServiceProvider.GetRequiredService <ILogger <ISourceConnector> >();
                using var chainLogScope  = logger.BeginScope(_chainName);
                using var sourceLogScope = logger.BeginScope(Name);

                var eventBroker = new EventBroker(serviceScope.ServiceProvider);

                try
                {
                    using var source = CreateInstance(serviceScope.ServiceProvider);
                    eventBroker.Publish(new SourceStartedEvent(_chainName, Name, Index, sourceExecutionId));
                    await source.ExecuteAsync(ExecuteWrapper, sourceCancellationToken);
                    eventBroker.Publish(new SourceStoppedEvent(_chainName, Name, Index, sourceExecutionId,
                                                               SourceResult.Completed));
                    logger.SourceCompleted(Name);
                }
                catch (OperationCanceledException exception) when(exception.CancellationToken == sourceCancellationToken)
                {
                    logger.SourceCanceled(Name, exception);
                    eventBroker.Publish(new SourceStoppedEvent(_chainName, Name, Index, sourceExecutionId, SourceResult.Canceled));
                }
                catch (Exception exception)
                {
                    logger.SourceFatalError(Name, exception);
                    eventBroker.Publish(new SourceStoppedEvent(_chainName, Name, Index, sourceExecutionId,
                                                               SourceResult.Faulted, exception));
                    await eventBroker.PublishAsync(new SourceExceptionEvent(_chainName, Name, Index, sourceExecutionId,
                                                                            exception));
                }
                finally
                {
                    _executeCancellationTokenSource?.Dispose();
                    _executeCancellationTokenSource = null;
                    _sourceCancellationTokenSource?.Dispose();
                    _sourceCancellationTokenSource = null;
                }
            }, CancellationToken.None);
        }