Created
March 19, 2018 09:13
-
-
Save donaldgray/ee60ae5948bf8880010d2744a21d8163 to your computer and use it in GitHub Desktop.
DailySchedule for Azure function that will not run if AzureWebJobEnv == "Development"
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
public abstract class DevelopmentNonRunningSchedule : DailySchedule | |
{ | |
protected DevelopmentNonRunningSchedule(params string[] times) : base(times) | |
{ | |
} | |
protected DevelopmentNonRunningSchedule() : base() | |
{ | |
} | |
public override DateTime GetNextOccurrence(DateTime now) | |
{ | |
if (string.Compare("Development", GetSettingFromConfigOrEnvironment("AzureWebJobsEnv"), | |
StringComparison.OrdinalIgnoreCase) == 0) | |
{ | |
// If DevelopmentMode then set next occurrence as MaxValue, effectively never | |
return DateTime.MaxValue; | |
} | |
var nextOccurrence = base.GetNextOccurrence(now); | |
return nextOccurrence; | |
} | |
// This is the same logic for working out JobHostConfiguration.IsDevelopment | |
private static string GetSettingFromConfigOrEnvironment(string settingName) | |
{ | |
if (string.IsNullOrEmpty(settingName)) | |
return null; | |
string appSetting = ConfigurationManager.AppSettings[settingName]; | |
if (!string.IsNullOrEmpty(appSetting)) | |
return appSetting; | |
return Environment.GetEnvironmentVariable(settingName) ?? appSetting; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment