Manually trigger time based Azure Functions on dev [duplicate] - azure

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)

Related

Azure Function - Time Trigger Function Status

I would like to ask you if it is possible to check current trigger function status (such as pending, working, succeed etc.) using InvocationId from ExecutionContext?
My current code:
[FunctionName(nameof(ServiceFunction))]
public async Task Run([TimerTrigger("0 5 9 * * *")] TimerInfo timer, ExecutionContext context)
{
_logger.LogInformation($"{nameof(ServiceFunction)} started!");
await Service.Create();
}
You can check it in Application Insight of your function app. First find the Application Insight of your function, then click "Live metrics" tab. You can find the detail logs in this page and check the status of your function running.

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.

Azure Timer Triggered Function default timer

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.

DurableOrchestrationClient.GetStatusAsync() always null for Durable Azure Function

I have a queue trigger azure function with DurableOrchestrationClient. I am able to start a new execution of my orchestration function, which triggers multiple activitytrigger functions and waits for them all to process. Everything works great.
My issue is that I am unable to check on the status of my orchestration function("TestFunction"). GetStatusAsync always returns as null. I need to know when the orchestration function is actually complete and process the return object (bool).
public static async void Run([QueueTrigger("photostodownload", Connection = "QueueStorage")]PhotoInfo photoInfo, [OrchestrationClient]DurableOrchestrationClient starter, TraceWriter log)
{
var x = await starter.StartNewAsync("TestFunction", photoInfo);
Thread.Sleep(2 * 1000);
var y = await starter.GetStatusAsync(x);
}
StartNewAsync enqueues a message into the control queuee, it doesn't mean that the orchestration starts immediately.
GetStatusAsync returns null if the instance either doesn't exist or has not yet started running. So, probably the orchestration just doesn't start yet during those 2 seconds of sleep that you have.
Rather than having a fixed wait timeout, you should either periodically poll the status of your orchestration, or send something like a Done event from the orchestration as the last step of the workflow.
Are you using function 1.0 or 2.0? A similar issue has been reported for Function 2.0 runtime on Github.
https://github.com/Azure/azure-functions-durable-extension/issues/126
Also when you say everything works great do you mean activityTrigger functions complete execution?
Are you running functions locally or is it deployed on Azure?

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.

Resources