Пример #1
0
 internal void AddProvider(string workerDir)
 {
     using (_metricsLogger.LatencyEvent(string.Format(MetricEventNames.AddProvider, workerDir)))
     {
         try
         {
             string workerConfigPath = Path.Combine(workerDir, RpcWorkerConstants.WorkerConfigFileName);
             if (!File.Exists(workerConfigPath))
             {
                 _logger.LogDebug($"Did not find worker config file at: {workerConfigPath}");
                 return;
             }
             _logger.LogDebug($"Found worker config: {workerConfigPath}");
             string  json         = File.ReadAllText(workerConfigPath);
             JObject workerConfig = JObject.Parse(json);
             RpcWorkerDescription workerDescription = workerConfig.Property(WorkerConstants.WorkerDescription).Value.ToObject <RpcWorkerDescription>();
             workerDescription.WorkerDirectory = workerDir;
             var languageSection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}:{workerDescription.Language}");
             workerDescription.Arguments = workerDescription.Arguments ?? new List <string>();
             GetDefaultExecutablePathFromAppSettings(workerDescription, languageSection);
             AddArgumentsFromAppSettings(workerDescription, languageSection);
             if (IsHydrationNeeded(workerDescription.DefaultWorkerPath))
             {
                 workerDescription.DefaultWorkerPath = GetHydratedWorkerPath(workerDescription);
             }
             workerDescription.ApplyDefaultsAndValidate(Directory.GetCurrentDirectory(), _logger);
             _workerDescripionDictionary[workerDescription.Language] = workerDescription;
         }
         catch (Exception ex)
         {
             _logger?.LogError(ex, $"Failed to initialize worker provider for: {workerDir}");
         }
     }
 }
        internal void AddProvider(string workerDir)
        {
            using (_metricsLogger.LatencyEvent(string.Format(MetricEventNames.AddProvider, workerDir)))
            {
                try
                {
                    string workerConfigPath = Path.Combine(workerDir, RpcWorkerConstants.WorkerConfigFileName);
                    if (!File.Exists(workerConfigPath))
                    {
                        _logger.LogDebug($"Did not find worker config file at: {workerConfigPath}");
                        return;
                    }
                    // Parse worker config file
                    _logger.LogDebug($"Found worker config: {workerConfigPath}");
                    string  json         = File.ReadAllText(workerConfigPath);
                    JObject workerConfig = JObject.Parse(json);
                    RpcWorkerDescription workerDescription = workerConfig.Property(WorkerConstants.WorkerDescription).Value.ToObject <RpcWorkerDescription>();
                    workerDescription.WorkerDirectory = workerDir;

                    // Check if any appsettings are provided for that langauge
                    var languageSection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}:{workerDescription.Language}");
                    workerDescription.Arguments = workerDescription.Arguments ?? new List <string>();
                    GetWorkerDescriptionFromAppSettings(workerDescription, languageSection);
                    AddArgumentsFromAppSettings(workerDescription, languageSection);

                    // Validate workerDescription
                    workerDescription.ApplyDefaultsAndValidate(Directory.GetCurrentDirectory(), _logger);

                    if (ShouldAddWorkerConfig(workerDescription.Language))
                    {
                        workerDescription.FormatWorkerPathIfNeeded(_systemRuntimeInformation, _environment, _logger);
                        workerDescription.ThrowIfFileNotExists(workerDescription.DefaultWorkerPath, nameof(workerDescription.DefaultWorkerPath));
                        workerDescription.ExpandEnvironmentVariables();

                        WorkerProcessCountOptions workerProcessCount = GetWorkerProcessCount(workerConfig);

                        var arguments = new WorkerProcessArguments()
                        {
                            ExecutablePath = workerDescription.DefaultExecutablePath,
                            WorkerPath     = workerDescription.DefaultWorkerPath
                        };
                        arguments.ExecutableArguments.AddRange(workerDescription.Arguments);
                        var rpcWorkerConfig = new RpcWorkerConfig()
                        {
                            Description  = workerDescription,
                            Arguments    = arguments,
                            CountOptions = workerProcessCount,
                        };
                        _workerDescriptionDictionary[workerDescription.Language] = rpcWorkerConfig;
                        ReadLanguageWorkerFile(arguments.WorkerPath);
                        _logger.LogDebug($"Added WorkerConfig for language: {workerDescription.Language}");
                    }
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, $"Failed to initialize worker provider for: {workerDir}");
                }
            }
        }