Azure Timer Triggered Function default timer - azure

I have created an azure function and is triggered by timer on specific schedule. I need this timer schedule configurable hence I would add this trigger schedule to azure function configuration settings using %TimerSchedule%.
public static void Run([TimerTrigger("%TimerSchedule%")]TimerInfo myTimer, ILogger log)
{
// Function Code here
}
Is there a way to have some default schedule if I do not add this configuration setting?
Thank you!

Add a second TimerTrigger function with whatever default schedule you want. At the beginning of that function check the existence of %TimerSchedule% and if it exists then exit.

Related

Azure Function and permanent process

I need to update data from external resource and should do it as often as possible.
I created Azure Function with 1-minute timer and marked it as Singleton:
[Singleton]
[FunctionName("FunctionSync")]
public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
but, seems to me, it's being created queue with waiting instances, so it's not good.
Other way - add queue with one message like semaphore and get message on the start add message on the end of function:
[Singleton]
[FunctionName("FunctionSync")]
public async Task Run(
[QueueTrigger("tablet-management-sync-last-datetime", Connection = "StorageConnectionString")]string myQueueItem,
[Queue("tablet-management-sync-last-datetime", Connection = "StorageConnectionString")]CloudQueue outputQueue,
ILogger log
)
{
but this solution is fragile, if something wrong, message is not added to queue and process is stopped.
Which solution is the best?
Azure Functions that are triggered via a TimerTrigger will only be called once per interval, even if there are multiple instances. So this is something that you don't have to worry about. It does this using Blob Leases, and you can read about it here on the webjob sdk wiki.

Manually trigger time based Azure Functions on dev [duplicate]

This question already has answers here:
What is the simplest way to run a timer-triggered Azure Function locally once?
(9 answers)
Closed 1 year ago.
My task runs once a day when deployed. For development currently I just changed the CRON to "every minute" and wait for that minute to hit in order for the function to be triggered for me to do the debugging. Is there a way such that I can leave my timer code to stay as "Every day" but still be able to kick it off manually.
In Azure I can just go to the function resource and click "Run" that will start it regardless of the timer. I am looking for something similar on my dev.
You are probably looking for this on the Timer Trigger attribute,
[TimerTrigger("", RunOnStartup = true)]TimerInfo timer
That should kick it off on startup.
It doesn't look like there is a direct solution available to manually (or even through and http request) trigger a time based Azure function.
Possible Workaround
Have a second http triggered function that has the same logic/code. You can use this 2nd function for testing on demand basis.
Please see the discussion in these 2 threads, it's very relevant to you -
Any method for testing timer trigger function
Time triggered azure function to trigger immediately after deploy
As #neo99 mentioned, simple answer is it is not possible just out of the box. The reason is input parameters for Run method of Trigger function are different for different type of triggers.
For e.g. you are looking to manually trigger(HttpTrigger) a TimerTrigger
Timer Trigger:
[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
Http Trigger:
[FunctionName("HttpTriggerCSharp")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
TraceWriter log)

Scheduled web job confusing about schedule

I have a web job that needs to run every day at 1am.
My settings.job is configured like this:
{
"schedule": "0 0 1 * * *",
"is_singleton": true
}
I have function declared in the Functions.cs
namespace Dsc.Dmp.SddUpgrade.WebJob
{
using System;
using System.IO;
using Microsoft.Azure.WebJobs;
public class Functions
{
public static void TriggerProcess(TextWriter log)
{
log.Write($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
}
I am getting the following logs:
[09/28/2017 12:02:05 > 9957a4: SYS INFO] Status changed to Running
[09/28/2017 12:02:07 > 9957a4: INFO] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).
As I read the documentation, some people are using a function signature like this:
public static void TriggerProcess([TimerTrigger("0 0 1 * * *")] TimerInfo timerInfo, TextWriter log)
However, this does not seem logic to me, because a have already configured my web job to by scheduled in the settings.job.
What am I missing here?
If you use a settings.job file to schedule your WebJob, your logic should go in the Program.cs's Main function. You can ignore the Functions.cs file if you go this route. This is great for migrating a console app into a WebJob and scheduling it.
The TimerTrigger is a WebJob extension. It's useful because it's possible to have multiple methods in Functions.cs, each with a separate TimerTrigger that executes on a different schedule. To use these, your WebJob needs to be continuous.
You need to put your logic in Program.cs.
The runtime will run your WebJob by executing the executable, running the Main method in Program.cs.
You seem to be missing the [FunctionName("TriggerProcess")] attribute in the function definition, that´s why you´re getting the "job not found" error.

Azure Functions modify timetrigger interval when using CI

Say I have a timetriggered function that should only run in production, or maybe have different schedule in production, how can I do that?
When using CI the the app goes into read-only mode, and timetrigger interval is not configurable.
There's a few options but I'd suggest you define your TimerTrigger to pick up it's schedule from the App Settings of the function app - where you can populate this via your CI process, or manually if need be.
Defining your function in C# in the VS 2017 tooling you need a method signature like this e.g.
[FunctionName("MyTimerFunction")]
public static void Run([TimerTrigger("%TriggerInterval%")] TimerInfo myTimer, TraceWriter log)
And in the function app settings (or local.settings.json to run locally) define the CRON interval for the timer.
{
"Values" : {
"TriggerInterval": "0 0 * * * *" // e.g. hourly
}
}
Additionally, if you need a quick fix, even if your CI process has set the app the read-only mode, you can still set it to read/write and then update the settings - of course that will be overridden the next time the CI deploys to that function app.

Azure Triggered Webjob - Detecting when webjob stops

I am developing a triggered webjob that use TimerTrigger.
Before the webjob stops, I need to dispose some objects but I don't know how to trigger the "webjob stop".
Having a NoAutomaticTrigger function, I know that I can use the WebJobsShutdownWatcher class to handle when the webjob is stopping but with a triggered job I need some help...
I had a look at Extensible Triggers and Binders with Azure WebJobs SDK 1.1.0-alpha1.
Is it a good idea to create a custom trigger (StopTrigger) that used the WebJobsShutdownWatcher class to fire action before the webjob stops ?
Ok The answer was in the question :
Yes I can use the WebJobsShutdownWatcher class because it has a Register function that is called when the cancellation token is canceled, in other words when the webjob is stopping.
static void Main()
{
var cancellationToken = new WebJobsShutdownWatcher().Token;
cancellationToken.Register(() =>
{
Console.Out.WriteLine("Do whatever you want before the webjob is stopped...");
});
var host = new JobHost();
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
EDIT (Based on Matthew comment):
If you use Triggered functions, you can add a CancellationToken parameter to your function signatures. The runtime will cancel that token when the host is shutting down automatically, allowing your function to receive the notification.
public static void QueueFunction(
[QueueTrigger("QueueName")] string message,
TextWriter log,
CancellationToken cancellationToken)
{
...
if(cancellationToken.IsCancellationRequested) return;
...
}
I was recently trying to figure out how to do this without the
WebJobs SDK which contains the WebJobShutdownWatcher, this is what I
found out.
What the underlying runtime does (and what the WebJobsShutdownWatcher referenced above checks), is create a local file at the location specified by the environment variable %WEBJOBS_SHUTDOWN_FILE%. If this file exists, it is essentially the runtime's signal to the webjob that it must shutdown within a configurable wait period (default of 5 seconds for continuous jobs, 30 for triggered jobs), otherwise the runtime will kill the job.
The net effect is, if you are not using the Azure WebJobs SDK, which contains the WebJobsShutdownWatcher as described above, you can still achieve graceful shutdown of your Azure Web Job by monitoring for the shutdown file on an interval shorter than the configured wait period.
Additional details, including how to configure the wait period, are described here: https://github.com/projectkudu/kudu/wiki/WebJobs#graceful-shutdown

Resources