How to invoke a NoAutomaticTrigger function in local development? - azure

I have some functions that I like to manually execute from time to time. I do not want them to be Timer Triggered.
I figured I can use [NoAutomaticTrigger] for these functions and they can be executed from the Azure Portal.
How can I execute them for testing .etc. during local development when running under func.exe?
(I know I can use an [HttpTrigger] but I prefer not to expose it to outside (even with a secure function authorization key) if possible.)

Have you seen the Disable function attribute?
Decorate the function with [Disable("IsFunctionDisabledSetting")]
This will cause the function to be disabled an any environment where an application setting IsFunctionDisabledSetting=true.
In your production configuration add an application setting of IsFunctionDisabledSetting=true.

You can use a RunOnStartup
public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = 1)])
But I personally prefer to have a queueTrigger and then just insert a message in the queue for the function to run.

Related

Whats is the difference between 'Settings.job' and 'TimerTrigger' in Azure WebJobs SDK 3.0

There are many tutorials using the following code to create a Webjobs via the WebJob SDK 3.0 library. Specifically 'TimerTrigger'
public void DoSomethingUseful([TimerTrigger("0 */1 * * * *", RunOnStartup = false)] TimerInfo timerInfo, TextWriter log)
{
// Act on the DI-ed class:
string thing = _usefulRepository.GetFoo();
Console.WriteLine($"{DateTime.Now} - {thing}");
}
The above example should run this method as a webjob every 1 minute. However this doesn't work.
I have managed to get the webjob to work when including a setting.job file.
setting.job: { "schedule": "0 */1 * * * *" }
My question is what is the different between these two?
Update:
Please go to the azure webjobs log, then you can see it actually runs as per the timerTrigger defined by SDK(even though the Schedule is n/a, and settings.job is blank, it does not matter):
In short, When using webjob sdk 3.x, you can use TimerTrigger attribute to run the function as per the time you defined. Without using webjobs SDK(like use .zip file or publish a console project from visual studio), you can use setting.job to defined timer instead of TimerTrigger attribute.
1.When you're using webjobs SDK 3.x for timer trigger, you should add this line of code: config.AddTimers(); .
Here are my code using webjobs SDK 3.x(it's a .net core 2.2 console project created in visual studio):
The packages with latest version: Microsoft.Azure.WebJobs / Microsoft.Azure.WebJobs.Extensions / Microsoft.Extensions.Logging.Console
The code in Program.cs:
class Program
{
static void Main(string[] args)
{
var builder = new HostBuilder()
.ConfigureWebJobs(config =>
{
config.AddTimers();
config.AddAzureStorageCoreServices();
})
.ConfigureLogging((context, b) =>
{
b.AddConsole();
}
)
.Build();
builder.Run();
}
}
Then create a new file, like SayHelloWebJob.cs, and code in it:
public class SayHelloWebJob
{
public void ProcessCollateFiles([TimerTrigger("0 */1 * * * *", RunOnStartup = false)]TimerInfo timerInfo,TextWriter writer)
{
writer.WriteLine("hi, it is a testing running");
Console.WriteLine("test");
}
}
Note that in the appsettings.json file, add your storage connection string, like below:
{
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net"
}
Then run the project, you can see the function is triggered as per 1 minute:
2.For settings.job, eg. if you're just creating a console project, and does not use the webjobs sdk. Since you're not using webjobs sdk, you cannot use the timerTrigger attribute. At this moment, you can include the settings.job file(in it's property, set "Copy to Output Directory" as "copy if newer") in this project and configure the scheduled timer like you did in your post. After publish as webjob(from visual studio, when publish, select "Webjob run mode" as "run on demand"), it can run as per the schedule you defined in settings.job.
I have been struggling with the same problem. Here is my understanding after longer research.
There are two kind of Webjobs:
triggered
continuous
Triggered one has to be triggered manually or can be triggered by App Service as per CRON expression schedule provided in setting.job. These jobs are not present in memory once not running.
Continuous one runs always, so the process exists in memory all the time. You can schedule it using Webjobs SKD TimeTrigger attribute.
You will also notice difference between these two Webjob types in the Dashboard.
For triggered Webjobs you will see on top level jobs runs then functions invoked and eventually invocation details.
For continuous Webjobs this will be functions invoked and eventually invocation details. Job runs are missing as this is just one long running job.
Check App Service / Process explorer under Kudu w3wp process to see Webjobs processes running.
Note that continuous and triggered Webjobs have to be started in different way in Main method where you provide the configuration. All comes configured when Webjob of specific type is added via Visual Studio.
This is based on WebJobs 2.x.
My recommendation is
for periodical (e.g. once per few hours, days) jobs use triggered
ones, job when not running will not consume resources,
for more frequent jobs use continuous ones with TimeTrigger
attribute, it will consume resources all the time but will not need
extra time for start-up.

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)

TimerTrigger not triggering after start of function app

I am trying to create a very simple TimerTriggered function to prevent my app from getting cold.
The issue is that after deploying the app or after restarting it the TimerTriggered function is never executed. After invoking the function manually the timer starts running as expected.
Some info that might be useful:
My app has a mix of different triggers.
The runtime version used is 2.0.11651.0.
The app service plan has a consumption plan and has to stay that way.
In this case I deploy using Visual Studio
My class looks like the following:
public static class KeepAliveTask
{
[FunctionName("KeepAlive")]
public static void Run([TimerTrigger("0 */4 * * * *", RunOnStartup = true)]TimerInfo timer, TraceWriter log)
{
log.Info("Keeping service alive");
}
}
That's all there is to it. I've been searching but has been unable to find anyone with the same problem. Any suggestions is appreciated.

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.

Resources