Skip to content

Instantly share code, notes, and snippets.

@donaldgray
Created March 19, 2018 09:13
Show Gist options
  • Save donaldgray/ee60ae5948bf8880010d2744a21d8163 to your computer and use it in GitHub Desktop.
Save donaldgray/ee60ae5948bf8880010d2744a21d8163 to your computer and use it in GitHub Desktop.
DailySchedule for Azure function that will not run if AzureWebJobEnv == "Development"
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