Azure Function App Not triggering TimerTrigger in Visual Studio Debug - azure

I have Azure Function app that I was able to debug previously but now it would not trigger when I start it in Visual Studio debug mode. Function looks like this:
public static void RunDownloadTask([TimerTrigger("0 */30 * * * *")] TimerInfo timerInfo, TextWriter log)
{...}
When I started in debug in Visual studio I used to get output like the following, but now I only get the first two lines of message in console. The rest "The next 5 occurrences ..." does not show up, and the function never gets triggered. Has anyone experienced something like this and know how to fix it? Looks like the instances I deployed to Azure App Service continue to work, but I don't know why I can't run this in debug mode locally anymore.
Found the following functions:
ChaseFTPDownloader.Functions.RunDownloadTask
The next 5 occurrences of the schedule will be:
10/5/2020 4:30:00 PM
10/5/2020 5:00:00 PM
...

I can reproduce your problem:
This is the problem of the settings of the Visual Studio.
Solution:
Just right click your functionapp in VS and add this command to the properties of your project:
start --verbose
After that you will see the log like before:(this change is updated in recent months.)
I think your function should start. Why you don't see it triggered is because the cron expression of your timetrigger.(It has an interval of 30 minutes and will not trigger the first time.)
Please do a test and let me know whether this can help you.

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.

Azure WebJob Console Application Empty Args when calling from webhook

I'm working on Azure WebJob.
I started by creating a console application in Visual Studio and I published the application as a webJob in portal.azure from VisualStudio.
the WebJob is Triggered Manualy from its Webhook with username and password https://{MyWebAPP}.scm.azurewebsites.net/api/triggeredwebjobs/{MyWebJob}/run?arguments=1 2 3 from a second program.
this WebJob is verry simple. It only displays a the arguments 1,2 and 3.
when I run the program from CommandeLine like so dotnet MyProject.dll
1 2 3 it works well.
but when I run it from webHook it does not read arguments.
here is my main script :
class Program
{
static void Main(string[] args)
{
Console.WriteLine("PARAMS Passed : " + string.Join(",", args));
}
}
This is the log in the WebJob when I run from WebHook by Post request : [06/09/2018 15:19:37 > 33a9f2: INFO] PARAMS Passed :
and this is the console when I run it from commande Line : [06/09/2018 15:19:37 > 33a9f2: INFO] PARAMS Passed : 1,2,3
Can some One Help PLEASE.
Tha,ks From All.
This comes down to a VS publishing bug. The problem is that it auto-generates a run.cmd that has:
dotnet foo.dll
When it really should have:
dotnet foo.dll %*
So that the arguments get flowed into your console app.
I'll report the issue, but for now you can work around as follows:
Explicitly create a run.cmd at the root of your console app (i.e. next to program.cs). Make it contain the correct line above with %*. And obviously, use your actual dll name instead of foo.dll :)
Add it to your project, and set Copy to Output Directory to Copy Always (default is Do not copy).
That will cause your run.cmd to get deployed, and VS won't auto-generate the faulty one.

Azure WebJob has status "Never finished" in WebJobs Dashboard

I have long running web job in AppService (around 1h).
AppService has "Always on" turned on.
It is initialized with:
var host = new JobHost(config);
host.Call(typeof(Functions).GetMethod("SyncUsers"));
host.Start();
Actual methods SyncUsers wrapped with attributes:
[Timeout("00:59:00", ThrowOnTimeout = true)]
[NoAutomaticTrigger]
Schedule is set with settings file settings.job:
{
"is_singleton": true,
"schedule": "0 0 */4 * * *"
}
Main issue is that in WebJobs Dashboard I see status "Never finished" in 90% of cases (or failed with exception - OK situation). Running time for such jobs is different: from 5 min to 30 mins. Logs just stopped at some moment without any exception or detailed message.
Another thing is that I can see that multiple jobs are running in the same time. So looks like singleton and schedule don't work (since job should run every 4h).
Also some jobs that have this status, displayed without running time, like this: "1 hour ago ( running time)" and I am not able to see logs or download them.
Anybody had such experience?
Thank you
Looks like you're trying to run an executable that never ends as a triggered WebJob, which has no chance of working. You need to either:
Use a continuous WebJob and rely on the WebJobs SDK for your timer
Use a plain console app deployed as a Scheduled WebJob. No need to use the SDK here. Just do what you need to do from your Main() and let it end.
I'd suggest #2 unless you have a specific need to use the WebJobs SDK.

Function App Time trigger not working

I have a function App. type is Timetrigger. I have given the time trigger expression 0 0 * * * * , as my requirement it to run with 1 Hour interval. I have refereed the TimeTrigger Cheat Sheet
But Unfortunetly its triggering in each 5 min. Somehow it's not working. Help me regarding this.
target--> Run the function App in the interval of 1 Hour.
If you have changed your code, I suggest you could right click project>rebuild your project to try again. Or you could try to use expression like '0 0 */1 * * *'.
The result:
But not sure why its getting triggered 5 min while putting the code in Azure Portal.
The code in portal and Visual Studio is different. You could try to right click project>publish>choose Azure function service to publish your function to portal. Then check the schedule in function.json:
In Portal, you could also click '+' to create TimeTrigger in Azure function service directly:
I got a conclusion out of the Issue, Although you will update the Time trigger schedule in the Code level, this will not impact the Running schedule. What ever is there in the configuration ( go to Integrate tab of the function App & check the timer value). Code value changes not impacting the config values. Need to change the config values manually.
You can set the trigger time as a config value, something like this:
[FunctionName(nameof(TimerFunction))]
public static async Task Run(
[TimerTrigger("%schedule%")]
TimerInfo timerInfo,
TraceWriter log)
{}
and then define schedule in the Application Settings of your Function App in Azure portal:
schedule 0 */5 * * * * (in this case every five minutes)
I'm using crontab.guru for choosing triggering intervals

Scheduled WebJob

I'm creating a new Azure WebJob project -- which appears to be a polished version of a console app that can run as a web job.
I want this job to run based on a schedule but in the Main() method -- see below -- Microsoft gives you the host.RunAndBlock() for the job to run continuously.
Do I need to change that if I want the job to run at regularly scheduled intervals?
static void Main()
{
var host = new JobHost();
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
When using the Azure WebJobs SDK you can use TimerTrigger to declare job functions that run on a schedule. For example here's a function that runs immediately on startup, then every two hours thereafter:
public static void StartupJob(
[TimerTrigger("0 0 */2 * * *", RunOnStartup = true)] TimerInfo timerInfo)
{
Console.WriteLine("Timer job fired!");
}
You can get TimerTrigger and other extensions by installing the Microsoft.Azure.WebJobs.Extensions nuget package. More information on TimerTrigger and the other extensions in that package and how to use them can be found in the azure-webjobs-sdk-extensions repo. When using the TimerTrigger, be sure to add a call to config.UseTimers() to your startup code to register the extension.
When using the Azure WebJobs SDK, you deploy your code to a Continuous WebJob, with AlwaysOn enabled. You can then add however many scheduled functions you desire in that WebJob.
An easy way to trigger a WebJob on a schedule would be to code it as a regular console application, and just add a 'settings.job' with the cron based scheduling configuration to the project.
For example, the following definition would trigger it every 5 minutes:
{
"schedule": "0 */5 * * * *"
}
No need to use JobHost, just make sure your WebApp is configured as 'Always On'.
You should then deploy the job as a triggered WebJob.
There are 2 ways that I know of for scheduling a web job instead of making it run continiously:
Create a scheduled WebJob using a CRON expression
Create a scheduled WebJob using the Azure Scheduler
You can find the documentation for both ways on azure.microsoft.com
I think you need RunAndBlock in case of Scheduled or Continuous but you can remove it if you have your job as on-demand

Resources