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

Mukil Deepthi picture Mukil Deepthi · Apr 14, 2015 · Viewed 20.7k times · Source

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

Answer

none picture none · May 31, 2015

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));
    }
}