Пример #1
0
        /// <summary>
        /// Returns the collection of filters associated with <paramref name="configuration"/>.
        /// </summary>
        /// <remarks>
        /// The implementation invokes <see cref="ProcessorConfiguration.Filters"/>.
        /// </remarks>
        /// <param name="configuration">The configuration.</param>
        /// <param name="descriptor">The handler descriptor. This value is not used.</param>
        /// <returns>A collection of filters.</returns>
        public IEnumerable<FilterInfo> GetFilters(ProcessorConfiguration configuration, HandlerDescriptor descriptor)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            return configuration.Filters;
        }
Пример #2
0
        /// <summary>
        /// Returns the collection of filters associated with <paramref name="descriptor"/>.
        /// </summary>
        /// <remarks>
        /// The implementation invokes <see cref="HandlerDescriptor.GetFilters()"/>.
        /// </remarks>
        /// <param name="configuration">The configuration. This value is not used.</param>
        /// <param name="descriptor">The handler descriptor.</param>
        /// <returns>A collection of filters.</returns>
        public IEnumerable<FilterInfo> GetFilters(ProcessorConfiguration configuration, HandlerDescriptor descriptor)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (descriptor == null)
            {
                throw Error.ArgumentNull("descriptor");
            }

            IEnumerable<FilterInfo> filters = descriptor.GetFilters().Select(instance => new FilterInfo(instance, FilterScope.Handler));

            return filters;
        }
Пример #3
0
        public RootCommand GetRootCommand()
        {
            var outputOption = new Option("--output", "The output directory path for the generated code files.")
            {
                Argument = new Argument <string>(() => "./output")
            };

            outputOption.AddAlias("-o");

            var languageOption = new Option("--language", "The target language for the generated code files.")
            {
                Argument = new Argument <GenerationLanguage?>(() => GenerationLanguage.CSharp)
            };

            languageOption.AddAlias("-l");
            AddEnumValidator <GenerationLanguage>(languageOption.Argument, "language");
            var classOption = new Option("--class-name", "The class name to use for the core client class.")
            {
                Argument = new Argument <string>(() => "ApiClient")
            };

            classOption.AddAlias("-c");
            AddStringRegexValidator(classOption.Argument, @"^[a-zA-Z_][\w_-]+", "class name");

            var namespaceOption = new Option("--namespace-name", "The namespace to use for the core client class specified with the --class-name option.")
            {
                Argument = new Argument <string>(() => "ApiClient")
            };

            namespaceOption.AddAlias("-n");
            AddStringRegexValidator(namespaceOption.Argument, @"^[\w][\w\._-]+", "namespace name");

            var logLevelOption = new Option("--loglevel", "The log level to use when logging messages to the main output.")
            {
                Argument = new Argument <LogLevel>(() => LogLevel.Warning)
            };

            logLevelOption.AddAlias("--ll");
            AddEnumValidator <LogLevel>(logLevelOption.Argument, "log level");
            var descriptionOption = new Option("--openapi", "The path to the OpenAPI description file used to generate the code files.")
            {
                Argument = new Argument <string>(() => "openapi.yml")
            };

            descriptionOption.AddAlias("-d");

            var backingStoreOption = new Option("--backing-store", "The fully qualified name for the backing store class to use.")
            {
                Argument = new Argument <string>()
            };

            backingStoreOption.AddAlias("-b");

            var serializerOption = new Option <List <String> >("--serializer", "The fully qualified class names for serializers.")
            {
                Argument = new Argument <List <string> >(() => new List <string> {
                    "Microsoft.Kiota.Serialization.Json.JsonSerializationWriterFactory"
                })
            };

            serializerOption.AddAlias("-s");

            var deserializerOption = new Option <List <String> >("--deserializer", "The fully qualified class names for deserializers.")
            {
                Argument = new Argument <List <string> >(() => new List <string> {
                    "Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory"
                })
            };

            deserializerOption.AddAlias("--ds");

            var command = new RootCommand {
                outputOption,
                languageOption,
                descriptionOption,
                backingStoreOption,
                classOption,
                logLevelOption,
                namespaceOption,
                serializerOption,
                deserializerOption,
            };

            command.Handler = HandlerDescriptor.FromDelegate(new HandleCommandCallDel(HandleCommandCall)).GetCommandHandler();
            return(command);
        }
Пример #4
0
        static Bootstrap()
        {
            var userNameArgument = new Argument <string> {
                Name = "username", Description = "Geek username."
            };
            var passwordArgument = new Argument <string> {
                Name = "password", Description = "Geek password."
            };

            var passwordOption = new Option <string>(new[] { "--password", "-p" }, "Geek password. Defaults to last specified.");

            var dateOption       = new Option <DateTime>(new[] { "--date", "-d" }, () => DateTime.Now.Date, "Date of play. Defaults to current date.").WithFormat("yyyy-MM-dd");
            var locationOption   = new Option <string>(new[] { "--location", "-l" }, "Location of play. Defaults to unspecified.");
            var quantityOption   = new Option <int>(new[] { "--quantity", "-q" }, () => 1, "Quantity of play. Defaults to 1.");
            var gameIdOption     = new Option <int?>(new[] { "--game-id", "-g" }, "Game id of play. Defaults to unspecified.");
            var gameNameOption   = new Option <string>(new[] { "--game-name" }, "Game name of play. Defaults to unspecified.");
            var lengthOption     = new Option <int>(new[] { "--length" }, "Length of play (minutes). Defaults to 0.");
            var incompleteOption = new Option <bool>(new[] { "--incomplete" }, "Incomplete play. Defaults to false.");
            var noWinStatsOption = new Option <bool>(new[] { "--no-win-stats" }, "No win stats for play. Defaults to false.");
            var commentsOption   = new Option <string>(new[] { "--comments" }, "Comments for play. Defaults to unspecified.");

            var allOption  = new Option <bool>(new[] { "--all", "-a" }, "Analyze all override. Defaults to false.");
            var yearOption = new Option <int?>(new[] { "--year", "-y" }, () => DateTime.Now.Year, "Year to analyze. Defaults to current year.");

            var loginUserCommand = new Command("login", "Login user.")
            {
                userNameArgument, passwordArgument
            };

            loginUserCommand.Handler = CommandHandler.Create <string, string>(LoginUserAsync);
            var logPlayCommand = new Command("play", "Log user play.")
            {
                userNameArgument, passwordOption, dateOption, locationOption, quantityOption, gameIdOption, gameNameOption, lengthOption, incompleteOption, noWinStatsOption, commentsOption
            };

            logPlayCommand.AddValidator(symbol => !(symbol.Children.Contains("game-id") ^ symbol.Children.Contains("game-name")) ? "Play must specify one of game id or game name" : null);
            logPlayCommand.Handler = HandlerDescriptor.FromDelegate(new Func <string, string, DateTime, string, int, int?, string, int, bool, bool, string, Task>(LogUserPlayAsync)).GetCommandHandler();
            var getUserPlaysCommand = new Command("plays", "Get user plays.")
            {
                userNameArgument, allOption, yearOption
            };

            getUserPlaysCommand.Handler = CommandHandler.Create <string, bool, int?>(GetUserPlaysAsync);
            var getUserStatsCommand = new Command("stats", "Get user stats.")
            {
                userNameArgument, allOption, yearOption
            };

            getUserStatsCommand.Handler = CommandHandler.Create <string, bool, int?>(GetUserStatsAsync);

            Parser = new CommandLineBuilder(new RootCommand("A command line tool for interacting with the BoardGameGeek API"))
                     .AddCommand(loginUserCommand)
                     .AddCommand(new Command("log", "Log commands.")
            {
                logPlayCommand
            })
                     .AddCommand(new Command("get", "Get commands.")
            {
                getUserPlaysCommand, getUserStatsCommand
            })
                     .UseDefaults()
                     .Build();
        }
Пример #5
0
        public static string HandlerDescriptorToString(HandlerDescriptor descriptor)
        {
            Contract.Requires(descriptor != null);

            return(descriptor.Name);
        }
Пример #6
0
 public ICamundaWorkerBuilder AddHandlerDescriptor(HandlerDescriptor descriptor)
 {
     Guard.NotNull(descriptor, nameof(descriptor));
     Services.AddSingleton(descriptor);
     return(this);
 }
 public static ICommandHandler Create(MethodInfo method, object?target = null) =>
 HandlerDescriptor.FromMethodInfo(method, target).GetCommandHandler();
 public static ICommandHandler Create <T1, T2, T3, T4, T5, T6, T7>(
     Func <T1, T2, T3, T4, T5, T6, T7, int> action) =>
 HandlerDescriptor.FromDelegate(action).GetCommandHandler();
Пример #9
0
        private void DistributeExecution(object payload, object context, Envelope envelope, ICommand commandWithKey, HandlerDescriptor handler)
        {
            object key = distributor.Distribute(payload);

            log.Debug(commandWithKey, "Distributing execution on the '{0}'.", key);

            queue.Enqueue(key, async() =>
            {
                log.Info(commandWithKey, "Starting execution.");

                Action <Exception> additionalExceptionDecorator = e =>
                {
                    AggregateRootException aggregateException = e as AggregateRootException;
                    if (aggregateException != null)
                    {
                        // If envelope is created and contains source command key, use it.
                        IKey sourceCommandKey;
                        if (aggregateException.SourceCommandKey == null && envelope != null && envelope.TryGetSourceCommandKey(out sourceCommandKey))
                        {
                            aggregateException.SourceCommandKey = sourceCommandKey;
                        }

                        // If command is command with key, use it.
                        if (aggregateException.CommandKey == null && commandWithKey != null)
                        {
                            aggregateException.CommandKey = commandWithKey.Key;
                        }
                    }
                };

                try
                {
                    log.Debug(commandWithKey, "Entered try-catch.");

                    if (handler.IsContext)
                    {
                        await handler.Execute(context, additionalExceptionDecorator);
                    }
                    else if (handler.IsEnvelope)
                    {
                        await handler.Execute(envelope, additionalExceptionDecorator);
                    }
                    else if (handler.IsPlain)
                    {
                        await handler.Execute(payload, additionalExceptionDecorator);
                    }
                    else
                    {
                        throw Ensure.Exception.UndefinedHandlerType(handler);
                    }

                    log.Debug(commandWithKey, "Handler finished.");

                    // If we have command with the key, notify about successful execution.
                    if (store != null && commandWithKey != null)
                    {
                        await store.PublishedAsync(commandWithKey.Key);
                        log.Debug(commandWithKey, "Successfull execution saved to the store.");
                    }
                }
                catch (Exception e)
                {
                    DispatcherExceptionHandlers.Handle(e);
                    log.Fatal(commandWithKey, e.ToString());
                }

                log.Info(commandWithKey, "Execution finished.");
            });
        }
 internal bool TryGet(Type argumentType, out HandlerDescriptor handler)
 {
     return(storage.TryGetValue(argumentType, out handler));
 }
Пример #11
0
        static async Task Main(string[] args)
        {
            TransformationLogging.LoggerFactory = LoggerFactory.Create(builder => {
                builder.AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddFilter("Microsoft.Health.Fhir.Transformation", LogLevel.Information)
                .AddConsole();
            });
            System.Net.ServicePointManager.DefaultConnectionLimit = 10 * 1024;

            ILogger logger = TransformationLogging.CreateLogger <Program>();

            var rootCommand = new RootCommand();

            var generateSchemaCommand = new Command("generate-schema")
            {
                new Option <string>("--clientId"),
                new Option <string>("--tenantId"),
                new Option <string>("--adlsAccount"),
                new Option <string>("--cdmFileSystem"),
                new Option <string>("--configurationContainer", getDefaultValue: () => "config"),
                new Option <string>("--clientSecret"),
                new Option <int>("--maxDepth", getDefaultValue: () => 3)
            };

            generateSchemaCommand.Handler = CommandHandler.Create <string, string, string, string, string, string, int>(
                async(clientId, tenantId, adlsAccount, cdmFileSystem, configurationContainer, clientSecret, maxDepth) =>
            {
                logger.LogInformation("Start to generate CDM schema.");
                ClientSecretCredential credential = GetClientSecretCredential(tenantId, clientId, clientSecret);

                StorageDefinitionLoader configLoader = new StorageDefinitionLoader(GetStorageServiceEndpoint(adlsAccount), configurationContainer, credential, maxDepth);
                TabularMappingDefinition[] mappings  = configLoader.Load();

                AdlsCsvSink sink = new AdlsCsvSink(adlsAccount, cdmFileSystem, credential);
                await sink.InitAsync();
                await sink.CreateFileSystemClientIfNotExistAsync();

                CdmCorpusDefinition defination        = InitAdlscdmCorpusDefinition(adlsAccount, "/" + cdmFileSystem, tenantId, clientId, clientSecret);
                CdmSchemaGenerator cdmSchemaGenerator = new CdmSchemaGenerator(defination);
                List <string> entities = await cdmSchemaGenerator.InitializeCdmFolderAsync(mappings, "adls");

                WriteActivityOutputs(entities);

                logger.LogInformation("Generate CDM schema completed.");
            });
            rootCommand.AddCommand(generateSchemaCommand);

            var transformDataCommand = new Command("transform-data")
            {
                new Option <string>("--clientId"),
                new Option <string>("--tenantId"),
                new Option <string>("--adlsAccount"),
                new Option <string>("--cdmFileSystem"),
                new Option <string>("--inputBlobUri"),
                new Option <string>("--configurationContainer"),
                new Option <string>("--clientSecret"),
                new Option <string>("--operationId"),
                new Option <string>("--maxDepth"),
            };

            Func <string, string, string, string, string, string, string, string, int, Task> transformDataAction =
                async(clientId, tenantId, adlsAccount, cdmFileSystem, inputBlobUri, configurationContainer, operationId, clientSecret, maxDepth) =>
            {
                logger.LogInformation("Start to transform data.");
                ClientSecretCredential credential = GetClientSecretCredential(tenantId, clientId, clientSecret);

                StorageDefinitionLoader    configLoader = new StorageDefinitionLoader(GetStorageServiceEndpoint(adlsAccount), configurationContainer, credential, maxDepth);
                TabularMappingDefinition[] mappings     = configLoader.Load();

                Uri     inputUri = new Uri(inputBlobUri);
                ISource source   = new StorageBlobNdjsonSource(inputUri, credential)
                {
                    ConcurrentCount = Environment.ProcessorCount * 2
                };

                string      fileName = Path.GetFileNameWithoutExtension(inputUri.AbsolutePath);
                AdlsCsvSink sink     = new AdlsCsvSink(adlsAccount, cdmFileSystem, credential)
                {
                    CsvFilePath = (string tableName) =>
                    {
                        return($"data/Local{tableName}/partition-data-{tableName}-{fileName}-{operationId}.csv");
                    },
                    ConcurrentCount = Environment.ProcessorCount * 2
                };

                TransformationExecutor executor = new TransformationExecutor(source,
                                                                             sink,
                                                                             mappings,
                                                                             new BasicFhirElementTabularTransformer());
                executor.ConcurrentCount = Environment.ProcessorCount * 2;
                IProgress <(int, int)> progressHandler = new Progress <(int, int)>(progress =>
                {
                    if (progress.Item1 % 100 == 0 || progress.Item2 % 100 == 0)
                    {
                        logger.LogInformation($"({progress.Item1} loaded, {progress.Item2} transformed) to CDM folder. {DateTime.UtcNow.ToLongTimeString()}");
                    }
                });

                await executor.ExecuteAsync(progressHandler);

                logger.LogInformation("Transform data complete.");
            };

            transformDataCommand.Handler = HandlerDescriptor.FromDelegate(transformDataAction).GetCommandHandler();
            rootCommand.AddCommand(transformDataCommand);

            await rootCommand.InvokeAsync(args);
        }
Пример #12
0
        public static string HandlerDescriptorToString(HandlerDescriptor descriptor)
        {
            Contract.Requires(descriptor != null);

            return descriptor.Name;
        }
Пример #13
0
        private THandler TryCreate(HandlerRequest request, HandlerDescriptor descriptor)
        {
            Contract.Requires(request != null);
            Contract.Requires(descriptor != null);

            Func <THandler> activator;

            if (descriptor.Lifetime != HandlerLifetime.Transient)
            {
                THandler commandHandler = this.GetInstanceOrActivator(request, descriptor, out activator);
                if (commandHandler != null)
                {
                    // we have a handler registered with the dependency resolver for this handler type
                    return(commandHandler);
                }
            }
            else if (this.fastCache == null)
            {
                // First check in the local fast cache and if not a match then look in the broader
                // HandlerDescriptor.Properties cache
                THandler commandHandler = this.GetInstanceOrActivator(request, descriptor, out activator);
                if (commandHandler != null)
                {
                    // we have a handler registered with the dependency resolver for this handler type
                    return(commandHandler);
                }

                Tuple <HandlerDescriptor, Func <THandler> > cacheItem = Tuple.Create(descriptor, activator);
                Interlocked.CompareExchange(ref this.fastCache, cacheItem, null);
            }
            else if (this.fastCache.Item1 == descriptor)
            {
                // If the key matches and we already have the delegate for creating an instance.
                activator = this.fastCache.Item2;
            }
            else
            {
                // If the key doesn't match then lookup/create delegate in the CommandHandlerDescriptor.Properties for
                // that CommandHandlerDescriptor instance
                object result;
                if (descriptor.Properties.TryGetValue(this.cacheKey, out result))
                {
                    activator = (Func <THandler>)result;
                }
                else
                {
                    THandler commandHandler = this.GetInstanceOrActivator(request, descriptor, out activator);
                    if (commandHandler != null)
                    {
                        // we have a handler registered with the dependency resolver for this handler type
                        return(commandHandler);
                    }

                    descriptor.Properties.TryAdd(this.cacheKey, activator);
                }
            }

            var instance = activator();

            return(instance);
        }
Пример #14
0
        private static Func <THandler> CreatePerRequestActivator(HandlerRequest request, HandlerDescriptor descriptor)
        {
            Contract.Requires(request != null);
            Contract.Requires(descriptor != null);

            Func <THandler> activator;
            Dictionary <Type, Func <THandler> > activators;

            if (request.Properties.TryGetValue(HandlerInstanceKey, out activators))
            {
                if (activators.TryGetValue(descriptor.HandlerType, out activator))
                {
                    return(activator);
                }

                activator = CreateDelegatingActivator(descriptor.HandlerType);
                activators.Add(descriptor.HandlerType, activator);
                return(activator);
            }

            activator  = CreateDelegatingActivator(descriptor.HandlerType);
            activators = new Dictionary <Type, Func <THandler> >();
            activators.Add(descriptor.HandlerType, activator);
            request.Properties.Add(HandlerInstanceKey, activators);
            return(activator);
        }
Пример #15
0
 /// <summary>
 /// Creates a command handler based on an <see cref="Action"/>.
 /// </summary>
 public static ICommandHandler Create(Action action) =>
 HandlerDescriptor.FromDelegate(action).GetCommandHandler();
Пример #16
0
 public static ICommandHandler Create <T1, T2, T3>(
     Func <T1, T2, T3, Task <int> > action) =>
 HandlerDescriptor.FromDelegate(action).GetCommandHandler();
Пример #17
0
        private async Task HandleInternalAsync(HandlerDescriptor handler, ArgumentDescriptor argument, object commandPayload, bool isPersistenceUsed, bool isEnvelopeDelayUsed)
        {
            try
            {
                bool hasContextHandler  = handler.IsContext;
                bool hasEnvelopeHandler = hasContextHandler || handler.IsEnvelope;

                object   payload  = commandPayload;
                object   context  = null;
                Envelope envelope = null;

                ICommand commandWithKey = null;
                if (argument.IsContext)
                {
                    // If passed argument is context, throw.
                    log.Fatal("Passed in a context object.");
                    throw Ensure.Exception.NotSupported("PersistentCommandDispatcher doesn't support passing in a command handler context.");
                }
                else
                {
                    // If passed argument is not context, try to create it if needed.
                    if (argument.IsEnvelope)
                    {
                        // If passed argument is envelope, extract payload.
                        envelope = (Envelope)payload;
                        payload  = envelope.Body;
                    }
                    else
                    {
                        commandWithKey     = payload as ICommand;
                        hasEnvelopeHandler = hasEnvelopeHandler || commandWithKey != null;

                        // If passed argument is not envelope, try to create it if needed.
                        if (hasEnvelopeHandler)
                        {
                            envelope = EnvelopeFactory.Create(payload, argument.ArgumentType);
                        }
                    }

                    if (hasContextHandler)
                    {
                        log.Fatal("Context handler not supported.");
                        throw Ensure.Exception.NotSupported("PersistentCommandDispatcher doesn't support command handler context.");
                    }
                }

                if (commandWithKey == null)
                {
                    commandWithKey = payload as ICommand;
                }

                log.Info(commandWithKey, "Got a command.");
                log.Info(commandWithKey, "Execution on the handler '{0}'.", handler);

                // If we have command with the key, serialize it for persisten delivery.
                if (store != null && isPersistenceUsed && commandWithKey != null)
                {
                    string serializedEnvelope = await formatter.SerializeAsync(envelope);

                    store.Save(new CommandModel(commandWithKey.Key, serializedEnvelope));

                    log.Debug(commandWithKey, "Saved to the store.");
                }

                // If isEnvelopeDelayUsed, try to schedule the execution.
                // If succeeded, return.
                if (isEnvelopeDelayUsed && TrySchedule(envelope, handler, argument))
                {
                    return;
                }

                // Distribute the execution.
                DistributeExecution(payload, context, envelope, commandWithKey, handler);
            }
            catch (Exception e)
            {
                DispatcherExceptionHandlers.Handle(e);
            }
        }
Пример #18
0
 public static ICommandHandler Create <T1, T2, T3, T4, T5>(
     Action <T1, T2, T3, T4, T5> action) =>
 HandlerDescriptor.FromDelegate(action).GetCommandHandler();
Пример #19
0
 public static Command ProcessStatusCommand(string description) =>
 new Command(name: "ps", description)
 {
     HandlerDescriptor.FromDelegate((ProcessStatusDelegate)ProcessStatus).GetCommandHandler()
 };
Пример #20
0
 public static ICommandHandler Create <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(
     Action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> action) =>
 HandlerDescriptor.FromDelegate(action).GetCommandHandler();
 public static ICommandHandler Create(Delegate @delegate) =>
 HandlerDescriptor.FromDelegate(@delegate).GetCommandHandler();
Пример #22
0
 public static ICommandHandler Create <T>(
     Func <T, int> action) =>
 HandlerDescriptor.FromDelegate(action).GetCommandHandler();
 public static ICommandHandler Create <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(
     Func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, int> action) =>
 HandlerDescriptor.FromDelegate(action).GetCommandHandler();
 public static ICommandHandler Create(MethodInfo method) =>
 HandlerDescriptor.FromMethodInfo(method).GetCommandHandler();