27

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

Mukil Deepthi
  • 6,072
  • 13
  • 71
  • 156
  • 2
    You can put it on a schedule that runs every 3 minutes. – lopezbertoni Apr 14 '15 at 12:42
  • 2
    You dont need the whole JobHost thing you have there, you can simply call ProcessMethod from Main and use Console.WriteLine to log. – Amit Apple Apr 14 '15 at 17:12
  • Hi, I have just shown a simple example in ProcessMethod to log. Other than logging, the ProcessMethod does other processes ie. calling database, update sql table , etc., – Mukil Deepthi Apr 17 '15 at 08:43
  • @AmitApple What if you want to have a cancellation token to allow a graceful shutdown? Is it needed then? (Acknowledging support for this may have been added after your comment) – xr280xr Jun 12 '23 at 16:45

2 Answers2

27

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));
    }
}
none
  • 418
  • 5
  • 8
19

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();
    }
Rocklands.Cave
  • 231
  • 2
  • 6