Skip to content

Commit

Permalink
Moving some items into settings manager
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocav committed Oct 27, 2016
1 parent 47d2839 commit 2ea07ab
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 48 deletions.
41 changes: 13 additions & 28 deletions src/WebJobs.Script.WebHost/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,16 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost
{
public static class WebApiConfig
{
private static ScriptSettingsManager _settingsManager;

public static void Register(HttpConfiguration config)
{
_settingsManager = ScriptSettingsManager.Instance;
Register(config, _settingsManager, GetDefaultSettings());
}

public static void Register(HttpConfiguration config, ScriptSettingsManager settingsManager, WebHostSettings settings = null, Action<ContainerBuilder, WebHostSettings> dependencyCallback = null)
public static void Register(HttpConfiguration config, ScriptSettingsManager settingsManager = null,
WebHostSettings settings = null, Action<ContainerBuilder, WebHostSettings> dependencyCallback = null)
{
if (config == null)
{
throw new ArgumentNullException("config");
}
if (settings == null)
{
throw new ArgumentNullException("settings");
}

if (settingsManager == null)
{
throw new ArgumentNullException("settingsManager");
}

_settingsManager = settingsManager;
settingsManager = settingsManager ?? ScriptSettingsManager.Instance;
settings = settings ?? GetDefaultSettings(settingsManager);

// Delete hostingstart.html if any. Azure creates that in all sites by default
string hostingStart = Path.Combine(settings.ScriptPath, "hostingstart.html");
Expand All @@ -50,11 +35,11 @@ public static void Register(HttpConfiguration config, ScriptSettingsManager sett
}

// Add necessary folders to the %PATH%
PrependFoldersToEnvironmentPath();
PrependFoldersToEnvironmentPath(settingsManager);

var builder = new ContainerBuilder();
builder.RegisterApiControllers(typeof(FunctionsController).Assembly);
AutofacBootstrap.Initialize(_settingsManager, builder, settings);
AutofacBootstrap.Initialize(settingsManager, builder, settings);

// Invoke registration callback
dependencyCallback?.Invoke(builder, settings);
Expand Down Expand Up @@ -96,10 +81,10 @@ public static void Register(HttpConfiguration config, ScriptSettingsManager sett
config.InitializeReceiveSalesforceWebHooks();
}

private static void PrependFoldersToEnvironmentPath()
private static void PrependFoldersToEnvironmentPath(ScriptSettingsManager settingsManager)
{
// Only do this when %HOME% is defined (normally on Azure)
string home = _settingsManager.GetSetting(EnvironmentSettingNames.AzureWebsiteHomePath);
string home = settingsManager.GetSetting(EnvironmentSettingNames.AzureWebsiteHomePath);
if (!string.IsNullOrEmpty(home))
{
// Create the tools folder if it doesn't exist
Expand All @@ -109,28 +94,28 @@ private static void PrependFoldersToEnvironmentPath()
var folders = new List<string>();
folders.Add(Path.Combine(home, @"site\tools"));

string path = _settingsManager.GetSetting("PATH");
string path = Environment.GetEnvironmentVariable("PATH");
string additionalPaths = String.Join(";", folders);

// Make sure we haven't already added them. This can happen if the appdomain restart (since it's still same process)
if (!path.Contains(additionalPaths))
{
path = additionalPaths + ";" + path;

_settingsManager.SetSetting("PATH", path);
Environment.SetEnvironmentVariable("PATH", path);
}
}
}

private static WebHostSettings GetDefaultSettings()
private static WebHostSettings GetDefaultSettings(ScriptSettingsManager settingsManager)
{
WebHostSettings settings = new WebHostSettings();

string home = _settingsManager.GetSetting(EnvironmentSettingNames.AzureWebsiteHomePath);
string home = settingsManager.GetSetting(EnvironmentSettingNames.AzureWebsiteHomePath);
bool isLocal = string.IsNullOrEmpty(home);
if (isLocal)
{
settings.ScriptPath = _settingsManager.GetSetting(EnvironmentSettingNames.AzureWebJobsScriptRoot);
settings.ScriptPath = settingsManager.GetSetting(EnvironmentSettingNames.AzureWebJobsScriptRoot);
settings.LogPath = Path.Combine(Path.GetTempPath(), @"Functions");
settings.SecretsPath = HttpContext.Current.Server.MapPath("~/App_Data/Secrets");
}
Expand Down
2 changes: 1 addition & 1 deletion src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void EnsureInitialized(WebHostSettings settings)

private static void ReinitializeAppSettings()
{
if (WebScriptHostManager.IsAzureEnvironment)
if (_settingsManager.IsAzureEnvironment)
{
// the nature of this is only add or update (not remove).
// so there may be settings from standby site leak over.
Expand Down
2 changes: 1 addition & 1 deletion src/WebJobs.Script.WebHost/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configure(c => WebApiConfig.Register(c));

var scriptHostManager = GlobalConfiguration.Configuration.DependencyResolver.GetService<WebScriptHostManager>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public DefaultKeyValueConverterFactory(ScriptSettingsManager settingsManager)
_encryptionSupported = IsEncryptionSupported();
}

private static bool IsEncryptionSupported() => WebScriptHostManager.IsAzureEnvironment || _settingsManager.GetSetting(AzureWebsiteLocalEncryptionKey) != null;
private static bool IsEncryptionSupported() => _settingsManager.IsAzureEnvironment || _settingsManager.GetSetting(AzureWebsiteLocalEncryptionKey) != null;

public IKeyValueReader GetValueReader(Key key)
{
Expand Down
8 changes: 0 additions & 8 deletions src/WebJobs.Script.WebHost/WebScriptHostManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ public WebScriptHostManager(ScriptHostConfiguration config, ISecretManager secre
{
}

public static bool IsAzureEnvironment
{
get
{
return !string.IsNullOrEmpty(ScriptSettingsManager.Instance.GetSetting(EnvironmentSettingNames.AzureWebsiteInstanceId));
}
}

public virtual bool Initialized
{
get
Expand Down
10 changes: 7 additions & 3 deletions src/WebJobs.Script/Config/ScriptSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ public class ScriptSettingsManager
{
private static ScriptSettingsManager _instance = new ScriptSettingsManager();

protected ScriptSettingsManager()
{
}

public static ScriptSettingsManager Instance
{
get { return _instance; }
set { _instance = value; }
}

protected ScriptSettingsManager()
{
}
public bool IsAzureEnvironment => !string.IsNullOrEmpty(GetSetting(EnvironmentSettingNames.AzureWebsiteInstanceId));

public bool IsRemoteDebuggingEnabled => !string.IsNullOrEmpty(GetSetting(EnvironmentSettingNames.RemoteDebuggingPort));

public virtual void Reset()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Microsoft.Azure.WebJobs.Script.Description
public sealed class DotNetCompilationServiceFactory : ICompilationServiceFactory
{
private static readonly ImmutableArray<ScriptType> SupportedScriptTypes = new[] { ScriptType.CSharp, ScriptType.FSharp }.ToImmutableArray();

private static OptimizationLevel? _optimizationLevel;

ImmutableArray<ScriptType> ICompilationServiceFactory.SupportedScriptTypes
Expand All @@ -24,10 +23,6 @@ ImmutableArray<ScriptType> ICompilationServiceFactory.SupportedScriptTypes
}
}

private static bool IsLocal => string.IsNullOrEmpty(Environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId));

private static bool IsRemoteDebuggingEnabled => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(EnvironmentSettingNames.RemoteDebuggingPort));

internal static OptimizationLevel OptimizationLevel
{
get
Expand All @@ -37,7 +32,9 @@ internal static OptimizationLevel OptimizationLevel
// Get the release mode setting. If set, this will take priority over environment settings.
string releaseModeSetting = ScriptSettingsManager.Instance.GetSetting(EnvironmentSettingNames.CompilationReleaseMode);
bool releaseMode;
if (!bool.TryParse(releaseModeSetting, out releaseMode) && !IsLocal && !IsRemoteDebuggingEnabled)
if (!bool.TryParse(releaseModeSetting, out releaseMode) &&
ScriptSettingsManager.Instance.IsAzureEnvironment &&
!ScriptSettingsManager.Instance.IsRemoteDebuggingEnabled)
{
// If the release mode setting is not set, we're running in Azure
// and not remote debugging, use release mode.
Expand Down

0 comments on commit 2ea07ab

Please sign in to comment.