forked from Azure/azure-webjobs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding DisableAttribute for multi-level job function disabling
- Loading branch information
Showing
17 changed files
with
543 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Configuration; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host | ||
{ | ||
internal static class ConfigurationUtility | ||
{ | ||
public static string GetSettingFromConfigOrEnvironment(string settingName) | ||
{ | ||
string configValue = ConfigurationManager.AppSettings[settingName]; | ||
if (!string.IsNullOrEmpty(configValue)) | ||
{ | ||
// config values take precedence over environment values | ||
return configValue; | ||
} | ||
|
||
return Environment.GetEnvironmentVariable(settingName) ?? configValue; | ||
} | ||
|
||
public static string GetConnectionFromConfigOrEnvironment(string connectionName) | ||
{ | ||
string configValue = null; | ||
var connectionStringEntry = ConfigurationManager.ConnectionStrings[connectionName]; | ||
if (connectionStringEntry != null) | ||
{ | ||
configValue = connectionStringEntry.ConnectionString; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(configValue)) | ||
{ | ||
// config values take precedence over environment values | ||
return configValue; | ||
} | ||
|
||
return Environment.GetEnvironmentVariable(connectionName) ?? configValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using Microsoft.Azure.WebJobs.Host.Bindings.Runtime; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host | ||
{ | ||
internal static class TypeUtility | ||
{ | ||
/// <summary> | ||
/// Walk from the parameter up to the containing type, looking for an instance | ||
/// of the specified attribute type, returning it if found. | ||
/// </summary> | ||
/// <param name="parameter">The parameter to check.</param> | ||
internal static T GetHierarchicalAttributeOrNull<T>(ParameterInfo parameter) where T : Attribute | ||
{ | ||
if (parameter == null || | ||
parameter.GetType() == typeof(AttributeBindingSource.FakeParameterInfo)) | ||
{ | ||
return null; | ||
} | ||
|
||
T attribute = parameter.GetCustomAttribute<T>(); | ||
if (attribute != null) | ||
{ | ||
return attribute; | ||
} | ||
|
||
attribute = parameter.Member.GetCustomAttribute<T>(); | ||
if (attribute != null) | ||
{ | ||
return attribute; | ||
} | ||
|
||
attribute = parameter.Member.DeclaringType.GetCustomAttribute<T>(); | ||
if (attribute != null) | ||
{ | ||
return attribute; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Azure.WebJobs | ||
{ | ||
/// <summary> | ||
/// Attribute that can be applied to job functions, trigger parameters and classes | ||
/// to conditionally disable triggered functions. | ||
/// <remarks> | ||
/// For example, by using this attribute, you can dynamically disable functions temporarily | ||
/// by changing application settings. Note that the disable check is done on startup only. | ||
/// If a <see cref="DisableAttribute"/> in the hierarchy (Parameter/Method/Class) exists and | ||
/// indicates that the function should be disabled, the listener for that function will not be | ||
/// started. The attribute only affects triggered functions. | ||
/// </remarks> | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Parameter, AllowMultiple = false)] | ||
public sealed class DisableAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Constructs a new instance. | ||
/// </summary> | ||
public DisableAttribute() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Constructs a new instance. | ||
/// </summary> | ||
/// <param name="settingName">The name of an application setting or environment variable that | ||
/// governs whether the function(s) should be disabled. If the specified setting exists and its | ||
/// value is "1" or "True", the function will be disabled. The setting name can contain binding | ||
/// parameters (e.g. {MethodName}, {MethodShortName}, %test%, etc.).</param> | ||
public DisableAttribute(string settingName) | ||
{ | ||
SettingName = settingName; | ||
} | ||
|
||
/// <summary> | ||
/// Constructs a new instance. | ||
/// </summary> | ||
/// <param name="providerType">A Type which implements a method named "IsDisabled" taking | ||
/// a <see cref="System.Reflection.MethodInfo"/> and returning <see cref="bool"/>. This | ||
/// function will be called to determine whether the target function should be disabled. | ||
/// </param> | ||
public DisableAttribute(Type providerType) | ||
{ | ||
ProviderType = providerType; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the application setting or environment variable that will | ||
/// be used to determine whether the function(s) will be disabled. | ||
/// </summary> | ||
public string SettingName { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the custom <see cref="Type"/> that will be invoked to determine | ||
/// whether the function(s) will be disabled. | ||
/// </summary> | ||
public Type ProviderType { get; private set; } | ||
} | ||
} |
Oops, something went wrong.