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}");
                }
            }
        }
Exemplo n.º 2
0
        public static IList <RpcWorkerConfig> GetTestWorkerConfigs(bool includeDllWorker           = false, int processCountValue = 1,
                                                                   TimeSpan?processStartupInterval = null, TimeSpan?processRestartInterval = null, TimeSpan?processShutdownTimeout = null, bool workerIndexing = false)
        {
            var      defaultCountOptions = new WorkerProcessCountOptions();
            TimeSpan startupInterval     = processStartupInterval ?? defaultCountOptions.ProcessStartupInterval;
            TimeSpan restartInterval     = processRestartInterval ?? defaultCountOptions.ProcessRestartInterval;
            TimeSpan shutdownTimeout     = processShutdownTimeout ?? defaultCountOptions.ProcessShutdownTimeout;

            var workerConfigs = new List <RpcWorkerConfig>
            {
                new RpcWorkerConfig
                {
                    Description  = GetTestWorkerDescription("node", ".js", workerIndexing),
                    CountOptions = new WorkerProcessCountOptions
                    {
                        ProcessCount           = processCountValue,
                        ProcessStartupInterval = startupInterval,
                        ProcessRestartInterval = restartInterval,
                        ProcessShutdownTimeout = shutdownTimeout
                    }
                },
                new RpcWorkerConfig
                {
                    Description  = GetTestWorkerDescription("java", ".jar", workerIndexing),
                    CountOptions = new WorkerProcessCountOptions
                    {
                        ProcessCount           = processCountValue,
                        ProcessStartupInterval = startupInterval,
                        ProcessRestartInterval = restartInterval,
                        ProcessShutdownTimeout = shutdownTimeout
                    }
                }
            };

            // Allow tests to have a worker that claims the .dll extension.
            if (includeDllWorker)
            {
                workerConfigs.Add(new RpcWorkerConfig()
                {
                    Description = GetTestWorkerDescription("dllWorker", ".dll", workerIndexing)
                });
            }

            return(workerConfigs);
        }
        internal WorkerProcessCountOptions GetWorkerProcessCount(JObject workerConfig)
        {
            WorkerProcessCountOptions workerProcessCount = workerConfig.Property(WorkerConstants.ProcessCount)?.Value.ToObject <WorkerProcessCountOptions>();

            workerProcessCount = workerProcessCount ?? new WorkerProcessCountOptions();

            if (workerProcessCount.SetProcessCountToNumberOfCpuCores)
            {
                workerProcessCount.ProcessCount = _environment.GetEffectiveCoresCount();
                // set Max worker process count to Number of effective cores if MaxProcessCount is less than MinProcessCount
                workerProcessCount.MaxProcessCount = workerProcessCount.ProcessCount > workerProcessCount.MaxProcessCount ? workerProcessCount.ProcessCount : workerProcessCount.MaxProcessCount;
            }

            // Env variable takes precedence over worker.config
            string processCountEnvSetting = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerProcessCountSettingName);

            if (!string.IsNullOrEmpty(processCountEnvSetting))
            {
                workerProcessCount.ProcessCount = int.Parse(processCountEnvSetting) > 1 ? int.Parse(processCountEnvSetting) : 1;
            }

            // Validate
            if (workerProcessCount.ProcessCount <= 0)
            {
                throw new ArgumentOutOfRangeException($"{nameof(workerProcessCount.ProcessCount)}", "ProcessCount must be greater than 0.");
            }
            if (workerProcessCount.ProcessCount > workerProcessCount.MaxProcessCount)
            {
                throw new ArgumentException($"{nameof(workerProcessCount.ProcessCount)} must not be greater than {nameof(workerProcessCount.MaxProcessCount)}");
            }
            if (workerProcessCount.ProcessStartupInterval.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException($"{nameof(workerProcessCount.ProcessStartupInterval)}", "The TimeSpan must not be negative.");
            }

            return(workerProcessCount);
        }