Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make azure webjob run continuously and call the public static function without automatic trigger

Tags:

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

like image 888
Mukil Deepthi Avatar asked Apr 14 '15 11:04

Mukil Deepthi


People also ask

Which Azure websites feature should you enable for continuously running WebJobs?

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.

What is continuous WebJob in Azure?

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.

How do I change my Azure WebJob schedule?

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.


2 Answers

These steps will get you to what you want:

  1. Change your method to async
  2. await the sleep
  3. use host.CallAsync() instead of host.Call()

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));     } } 
like image 131
none Avatar answered Oct 01 '22 19:10

none


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();     } 
like image 37
Rocklands.Cave Avatar answered Oct 01 '22 18:10

Rocklands.Cave