private static async Task <long> ExecutePipelineAsync(Type pipelineType, PipelineConfiguration configuration, CancellationToken cancellationToken)
        {
            IAsyncPipeline asyncPipeline;

            if ((object)pipelineType == null)
            {
                throw new ArgumentNullException(nameof(pipelineType));
            }

            if ((object)configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            asyncPipeline = (IAsyncPipeline)Activator.CreateInstance(pipelineType);

            if ((object)asyncPipeline == null)
            {
                throw new InvalidOperationException(nameof(asyncPipeline));
            }

            using (AsyncDisposal.Await(asyncPipeline, cancellationToken))
            {
                asyncPipeline.Configuration = configuration;
                await asyncPipeline.CreateAsync(cancellationToken);

                IAsyncContext asyncContext;
                using (AsyncDisposal.Await(asyncContext = asyncPipeline.CreateContextAsync(), cancellationToken))
                {
                    await asyncContext.CreateAsync(cancellationToken);

                    return(await asyncPipeline.ExecuteAsync(asyncContext, cancellationToken));
                }
            }
        }
        public static IAsyncProcessorBuilder UseMiddlewareAsync(this IAsyncProcessorBuilder asyncProcessorBuilder, Type asyncProcessorType, StageConfiguration stageConfiguration)
        {
            if ((object)asyncProcessorBuilder == null)
            {
                throw new ArgumentNullException(nameof(asyncProcessorBuilder));
            }

            if ((object)asyncProcessorType == null)
            {
                throw new ArgumentNullException(nameof(asyncProcessorType));
            }

            if ((object)stageConfiguration == null)
            {
                throw new ArgumentNullException(nameof(stageConfiguration));
            }

            return(asyncProcessorBuilder.UseAsync(asyncNext =>
            {
                return async(asyncContext, configuration, asyncChannel, cancellationToken) =>
                {
                    IAsyncProcessor asyncProcessor;
                    IAsyncChannel newAsyncChannel;

                    asyncProcessor = (IAsyncProcessor)Activator.CreateInstance(asyncProcessorType);

                    if ((object)asyncProcessor == null)
                    {
                        throw new InvalidOperationException(nameof(asyncProcessor));
                    }

                    using (AsyncDisposal.Await(asyncProcessor, cancellationToken))
                    {
                        asyncProcessor.Configuration = stageConfiguration;
                        await asyncProcessor.CreateAsync(cancellationToken);

                        await asyncProcessor.PreExecuteAsync(asyncContext, configuration, cancellationToken);
                        newAsyncChannel = await asyncProcessor.ProcessAsync(asyncContext, configuration, asyncChannel, asyncNext, cancellationToken);
                        await asyncProcessor.PostExecuteAsync(asyncContext, configuration, cancellationToken);

                        return newAsyncChannel;
                    }
                };
            }));
        }
        public static IAsyncProcessorBuilder UseMiddlewareAsync(this IAsyncProcessorBuilder asyncProcessorBuilder, IAsyncProcessor asyncProcessor, StageConfiguration stageConfiguration)
        {
            if ((object)asyncProcessorBuilder == null)
            {
                throw new ArgumentNullException(nameof(asyncProcessorBuilder));
            }

            if ((object)asyncProcessor == null)
            {
                throw new ArgumentNullException(nameof(asyncProcessor));
            }

            if ((object)stageConfiguration == null)
            {
                throw new ArgumentNullException(nameof(stageConfiguration));
            }

            return(asyncProcessorBuilder.UseAsync(asyncNext =>
            {
                return async(asyncContext, configuration, asyncChannel, cancellationToken) =>
                {
                    IAsyncProcessor _asyncProcessor = asyncProcessor;                                                                                                             // prevent closure bug
                    IAsyncChannel newAsyncChannel;

                    using (AsyncDisposal.Await(_asyncProcessor, cancellationToken))
                    {
                        _asyncProcessor.Configuration = stageConfiguration;
                        await _asyncProcessor.CreateAsync(cancellationToken);

                        await _asyncProcessor.PreExecuteAsync(asyncContext, configuration, cancellationToken);
                        newAsyncChannel = await _asyncProcessor.ProcessAsync(asyncContext, configuration, asyncChannel, asyncNext, cancellationToken);
                        await _asyncProcessor.PostExecuteAsync(asyncContext, configuration, cancellationToken);

                        return newAsyncChannel;
                    }
                };
            }));
        }
Ejemplo n.º 4
0
        protected override async Task <long> ExecuteAsyncInternal(IAsyncContext asyncContext, CancellationToken cancellationToken)
        {
            IAsyncChannel asyncChannel;

            IAsyncSourceConnector      sourceConnector;
            IAsyncDestinationConnector destinationConnector;

            Type sourceConnectorType;
            Type destinationConnectorType;
            IDictionary <StageConfiguration, Type> processorTypeConfigMappings;

            if ((object)asyncContext == null)
            {
                throw new ArgumentNullException(nameof(asyncContext));
            }

            sourceConnectorType         = this.Configuration.SourceConfiguration.GetStageType();
            destinationConnectorType    = this.Configuration.DestinationConfiguration.GetStageType();
            processorTypeConfigMappings = this.Configuration.ProcessorConfigurations.ToDictionary(c => c, c => c.GetStageType());

            if ((object)sourceConnectorType == null)
            {
                throw new InvalidOperationException(nameof(sourceConnectorType));
            }

            sourceConnector = (IAsyncSourceConnector)Activator.CreateInstance(sourceConnectorType);

            if ((object)sourceConnector == null)
            {
                throw new InvalidOperationException(nameof(sourceConnector));
            }

            using (AsyncDisposal.Await(sourceConnector, cancellationToken))
            {
                sourceConnector.Configuration = this.Configuration.SourceConfiguration;
                await sourceConnector.CreateAsync(cancellationToken);

                if ((object)destinationConnectorType == null)
                {
                    throw new InvalidOperationException(nameof(destinationConnectorType));
                }

                destinationConnector = (IAsyncDestinationConnector)Activator.CreateInstance(destinationConnectorType);

                if ((object)destinationConnector == null)
                {
                    throw new InvalidOperationException(nameof(destinationConnector));
                }

                using (AsyncDisposal.Await(destinationConnector, cancellationToken))
                {
                    RecordConfiguration configuration;

                    AsyncProcessDelegate   asyncProcess;
                    IAsyncProcessorBuilder asyncProcessorBuilder;

                    destinationConnector.Configuration = this.Configuration.DestinationConfiguration;
                    await destinationConnector.CreateAsync(cancellationToken);

                    configuration = this.Configuration.RecordConfiguration ?? new RecordConfiguration();

                    await sourceConnector.PreExecuteAsync(asyncContext, configuration, cancellationToken);

                    await destinationConnector.PreExecuteAsync(asyncContext, configuration, cancellationToken);

                    // --
                    asyncProcessorBuilder = new AsyncProcessorBuilder();

                    if (true)
                    {
                        // regular methods
                        asyncProcessorBuilder.UseAsync(NullProcessor.NullMiddlewareAsyncMethod);

                        // local functions
                        AsyncProcessDelegate _asyncMiddleware(AsyncProcessDelegate _asyncNext)
                        {
                            async Task <IAsyncChannel> _asyncProcessor(IAsyncContext _asyncContext, RecordConfiguration _configuration, IAsyncChannel _asyncChannel, CancellationToken _cancellationToken)
                            {
                                Console.WriteLine("processor_zero");
                                return(await _asyncNext(_asyncContext, _configuration, _asyncChannel, _cancellationToken));
                            }

                            return(_asyncProcessor);
                        }

                        asyncProcessorBuilder.UseAsync(_asyncMiddleware);

                        // lambda expressions
                        asyncProcessorBuilder.UseAsync(asyncNext =>
                        {
                            return((_asyncContext, _configuration, _asyncChannel, _cancelationToken) =>
                            {
                                Console.WriteLine("processor_first");
                                return asyncNext(_asyncContext, _configuration, _asyncChannel, _cancelationToken);
                            });
                        });
                    }

                    foreach (KeyValuePair <StageConfiguration, Type> processorTypeConfigMapping in processorTypeConfigMappings)
                    {
                        if ((object)processorTypeConfigMapping.Key == null)
                        {
                            throw new InvalidOperationException(nameof(processorTypeConfigMapping.Key));
                        }

                        if ((object)processorTypeConfigMapping.Value == null)
                        {
                            throw new InvalidOperationException(nameof(processorTypeConfigMapping.Value));
                        }

                        asyncProcessorBuilder.UseMiddlewareAsync(processorTypeConfigMapping.Value, processorTypeConfigMapping.Key);
                    }

                    asyncProcess = asyncProcessorBuilder.BuildAsync();

                    // --

                    asyncChannel = await sourceConnector.ProduceAsync(asyncContext, configuration, cancellationToken);

                    asyncChannel = await asyncProcess(asyncContext, configuration, asyncChannel, cancellationToken);

                    await destinationConnector.ConsumeAsync(asyncContext, configuration, asyncChannel, cancellationToken);

                    await destinationConnector.PostExecuteAsync(asyncContext, configuration, cancellationToken);

                    await sourceConnector.PostExecuteAsync(asyncContext, configuration, cancellationToken);

                    this.__check();
                }
            }

            return(0);
        }
        protected override async Task <int> OnStartupAsync(string[] args, IDictionary <string, IList <object> > arguments)
        {
            Dictionary <string, object> argz;
            string sourceFilePath;
            IDictionary <string, IList <string> > properties;
            IList <object> argumentValues;
            IList <string> propertyValues;
            bool           hasProperties;

            if ((object)args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if ((object)arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }

            // required
            properties = new Dictionary <string, IList <string> >();

            sourceFilePath = (string)arguments[CMDLN_TOKEN_SOURCEFILE].Single();

            hasProperties = arguments.TryGetValue(CMDLN_TOKEN_PROPERTY, out argumentValues);

            argz = new Dictionary <string, object>();
            argz.Add(CMDLN_TOKEN_SOURCEFILE, sourceFilePath);
            argz.Add(CMDLN_TOKEN_PROPERTY, hasProperties ? (object)argumentValues : null);

            if (hasProperties)
            {
                if ((object)argumentValues != null)
                {
                    foreach (string argumentValue in argumentValues)
                    {
                        string key, value;

                        if (!this.TryParseCommandLineArgumentProperty(argumentValue, out key, out value))
                        {
                            continue;
                        }

                        if (!properties.ContainsKey(key))
                        {
                            properties.Add(key, propertyValues = new List <string>());
                        }
                        else
                        {
                            propertyValues = properties[key];
                        }

                        // duplicate values are ignored
                        if (propertyValues.Contains(value))
                        {
                            continue;
                        }

                        propertyValues.Add(value);
                    }
                }
            }

            IToolHost toolHost;

            using (AsyncDisposal.Await(toolHost = AssemblyDomain.Default.DependencyManager.ResolveDependency <IToolHost>(string.Empty, true), this.CancellationTokenSource.Token))
            {
                await toolHost.RunAsync(sourceFilePath, this.CancellationTokenSource.Token);
            }

            return(0);
        }