How To: Manually Running Azure Functions that Use Timer Triggers
I have an Azure Function that runs using a Timer Trigger.
This function invokes a service class that performs some period automated tasks for the free journal and mood tracking micro-SaaS https://dailytracker.co I recently shipped.
I was testing it using a separate HTTP Trigger method but for quick tests wanted a way that didn’t involve opening Postman.
You can see the function here:
//runs every hour
[FunctionName("GenerateAnalytics")]
public static void GenerateFromTimer([TimerTrigger("0 0 * * * *"
#if DEBUG
, RunOnStartup = true
#endif
)] TimerInfo myTimer, ILogger log, ExecutionContext context)
{
if (myTimer.IsPastDue)
{
log.LogInformation("Timer is running late!");
}
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables()
.Build();
var cfgValues = config.GetSection("values");
log.LogInformation("C# Timer Trigger function processed a request.");
TextAnalyticsService analyticsService =
new TextAnalyticsService(cfgValues["TextAnalyticsEndpoint"], cfgValues["TextAnalyticsAPIKey"]);
// invoke analytics
analyticsService.ProcessAnalytics();
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
The cron expression 0 0 * * * * invokes the function every hour.
The following command/parameter runs the function on startup or when running in debug mode / via Visual Studio:
#if DEBUG , RunOnStartup = true #endif
Not the prettiest syntax but helpful if you just want it to run when your debugger launches.