I am developing a azure webjob which should run continuously. I have a public static function. I want this function to be automatically triggered without any queue. Right now i am using while(true) to run continuously. Is there any other way to do this?
Please find below my code
static void Main() { var host = new JobHost(); host.Call(typeof(Functions).GetMethod("ProcessMethod")); // The following code ensures that the WebJob will be running continuously host.RunAndBlock(); } [NoAutomaticTriggerAttribute] public static void ProcessMethod(TextWriter log) { while (true) { try { log.WriteLine("There are {0} pending requests", pendings.Count); } catch (Exception ex) { log.WriteLine("Error occurred in processing pending altapay requests. Error : {0}", ex.Message); } Thread.Sleep(TimeSpan.FromMinutes(3)); } }
Thanks
If you set the web app that hosts your job to run continuously, run on a schedule, or use event-driven triggers, enable the Always on setting on your web app's Azure Configuration page. The Always on setting helps to make sure that these kinds of WebJobs run reliably.
WebJobs is an Azure App Service feature that allows you to execute a program or script in the same instance as a web app, API app, or mobile app. Continuous WebJobs are launched as soon as they're created, and they run in an infinite loop.
To change the schedule, or to Modify the CRON value just use the App Service Editor to modify the WWWROOT/App_Data/jobs/triggered/YOUR_WEBJOB_NAME/settings. job file; By the time of writing, App Service Editor is still in preview but it will work.
These steps will get you to what you want:
I converted your code to reflect the steps below.
static void Main() { var host = new JobHost(); host.CallAsync(typeof(Functions).GetMethod("ProcessMethod")); // The following code ensures that the WebJob will be running continuously host.RunAndBlock(); } [NoAutomaticTriggerAttribute] public static async Task ProcessMethod(TextWriter log) { while (true) { try { log.WriteLine("There are {0} pending requests", pendings.Count); } catch (Exception ex) { log.WriteLine("Error occurred in processing pending altapay requests. Error : {0}", ex.Message); } await Task.Delay(TimeSpan.FromMinutes(3)); } }
Use the Microsoft.Azure.WebJobs.Extensions.Timers, see https://github.com/Azure/azure-webjobs-sdk-extensions/blob/master/src/ExtensionsSample/Samples/TimerSamples.cs to create a trigger that uses a TimeSpan or Crontab instruction to fire the method.
Add Microsoft.Azure.WebJobs.Extensions.Timers from NuGet to your project.
public static void ProcessMethod(TextWriter log)
becomes
public static void ProcessMethod([TimerTrigger("00:05:00",RunOnStartup = true)] TimerInfo timerInfo, TextWriter log)
for five minute triggers (using TimeSpan string)
You will need ensure your Program.cs Main sets up the config to use timers as follows:
static void Main() { JobHostConfiguration config = new JobHostConfiguration(); config.UseTimers(); var host = new JobHost(config); // The following code ensures that the WebJob will be running continuously host.RunAndBlock(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With