Example #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}");
                }
            }
        }
        internal static void AddArgumentsFromAppSettings(RpcWorkerDescription workerDescription, IConfigurationSection languageSection)
        {
            var argumentsSection = languageSection.GetSection($"{WorkerConstants.WorkerDescriptionArguments}");

            if (argumentsSection.Value != null)
            {
                ((List <string>)workerDescription.Arguments).AddRange(Regex.Split(argumentsSection.Value, @"\s+"));
            }
        }
        private static void GetWorkerDescriptionFromAppSettings(RpcWorkerDescription workerDescription, IConfigurationSection languageSection)
        {
            var defaultExecutablePathSetting = languageSection.GetSection($"{WorkerConstants.WorkerDescriptionDefaultExecutablePath}");

            workerDescription.DefaultExecutablePath = defaultExecutablePathSetting.Value != null ? defaultExecutablePathSetting.Value : workerDescription.DefaultExecutablePath;

            var defaultRuntimeVersionAppSetting = languageSection.GetSection($"{WorkerConstants.WorkerDescriptionDefaultRuntimeVersion}");

            workerDescription.DefaultRuntimeVersion = defaultRuntimeVersionAppSetting.Value != null ? defaultRuntimeVersionAppSetting.Value : workerDescription.DefaultRuntimeVersion;
        }
Example #5
0
        private static WorkerDescription GetWorkerDescriptionFromProfiles(string key, Dictionary <string, RpcWorkerDescription> descriptionProfiles, RpcWorkerDescription defaultWorkerDescription)
        {
            RpcWorkerDescription profileDescription = null;

            if (descriptionProfiles.TryGetValue(key, out profileDescription))
            {
                profileDescription.Arguments             = profileDescription.Arguments?.Count > 0 ? profileDescription.Arguments : defaultWorkerDescription.Arguments;
                profileDescription.DefaultExecutablePath = string.IsNullOrEmpty(profileDescription.DefaultExecutablePath) ? defaultWorkerDescription.DefaultExecutablePath : profileDescription.DefaultExecutablePath;
                profileDescription.DefaultWorkerPath     = string.IsNullOrEmpty(profileDescription.DefaultWorkerPath) ? defaultWorkerDescription.DefaultWorkerPath : profileDescription.DefaultWorkerPath;
                profileDescription.Extensions            = profileDescription.Extensions ?? defaultWorkerDescription.Extensions;
                profileDescription.Language        = string.IsNullOrEmpty(profileDescription.Language) ? defaultWorkerDescription.Language : profileDescription.Language;
                profileDescription.WorkerDirectory = string.IsNullOrEmpty(profileDescription.WorkerDirectory) ? defaultWorkerDescription.WorkerDirectory : profileDescription.WorkerDirectory;
                return(profileDescription);
            }
            return(defaultWorkerDescription);
        }
Example #6
0
        internal static void AddArgumentsFromAppSettings(RpcWorkerDescription workerDescription, IConfigurationSection languageSection)
        {
            if (workerDescription.Language.Equals(RpcWorkerConstants.JavaLanguageWorkerName))
            {
                // For Java either provide arguments via JAVA_OPTS or languageWorkers:java:arguments. Both cannot be supported
                string javaOpts = ScriptSettingsManager.Instance.GetSetting("JAVA_OPTS");
                if (!string.IsNullOrEmpty(javaOpts))
                {
                    workerDescription.Arguments.Add(javaOpts);
                    return;
                }
            }
            var argumentsSection = languageSection.GetSection($"{WorkerConstants.WorkerDescriptionArguments}");

            if (argumentsSection.Value != null)
            {
                workerDescription.Arguments.AddRange(Regex.Split(argumentsSection.Value, @"\s+"));
            }
        }
Example #7
0
        internal string GetHydratedWorkerPath(RpcWorkerDescription description)
        {
            if (string.IsNullOrEmpty(description.DefaultWorkerPath))
            {
                return(null);
            }

            OSPlatform os = _systemRuntimeInformation.GetOSPlatform();

            Architecture architecture = _systemRuntimeInformation.GetOSArchitecture();
            string       version      = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeVersionSettingName);

            if (string.IsNullOrEmpty(version))
            {
                version = description.DefaultRuntimeVersion;
            }

            description.ValidateWorkerPath(description.DefaultWorkerPath, os, architecture, version);

            return(description.DefaultWorkerPath.Replace(RpcWorkerConstants.OSPlaceholder, os.ToString())
                   .Replace(RpcWorkerConstants.ArchitecturePlaceholder, architecture.ToString())
                   .Replace(RpcWorkerConstants.RuntimeVersionPlaceholder, version));
        }