Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure WebJob Best Approach

This is my first time that I've done a WebJob Type of application. I have created a webjob project and in the solution it comes with Program.cs and Function.cs.

I have already removed Function.cs because in this project there is no queue I will be getting data from.

Now in Program.cs there is already Main Method as the following:

class Program
{
    // Please set the following connection strings in app.config for this WebJob to run:
    // AzureWebJobsDashboard and AzureWebJobsStorage
    static void Main()
    {
        var host = new JobHost();
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}

As I understand, that RunAndBlock is to run the webjob continuously, but I want the job to run one time only. I want to control the execution from outside by a schedule. I would like to know how to make my code run only one time? As seen below, I have a SupportService Class that has RunOnePoolProvisioingCycle, I want to call this method one time only. Is this the right approach?

static void Main()
{
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    _supportService.RunOnePoolProvisioningCycle();
}

or this one?

static void Main()
{
    var host = new JobHost();
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    host.Call(typeof(SupportService).GetMethod("SetPoolProvisioningConfigurations"));
}

or this one?

static void Main()
{
    var host = new JobHost();
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    host.CallAsync(typeof(SupportService).GetMethod("SetPoolProvisioningConfigurations"));
}

or should I use:

host.Start()

or

host.StartAsync()?
like image 365
Nadeem Tabbaa Avatar asked Sep 26 '22 21:09

Nadeem Tabbaa


2 Answers

What you see is part of the SDK, which is optional. A webjob can be as simple as a console Application that you Zip, upload and run as is.

So this code seems the best option in your case:

static void Main()
{
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    _supportService.RunOnePoolProvisioningCycle();
}
like image 71
benjguin Avatar answered Oct 12 '22 23:10

benjguin


The WebJob created by the template uses the WebJobs SDK. If you don't need to use any of the features of the SDK, then you can just create a console app and set up a CRON schedule to run it on (see "Scheduled jobs" here).

I linked to more information on the WebJobs SDK above. In addition to facilitating scenarios where you want to trigger functions on queues/blobs/etc., it also has the ability to run your jobs on schedule via TimerTrigger (part of the SDK extensions). Give those materials a read to see which suits your needs the best.

like image 22
mathewc Avatar answered Oct 13 '22 00:10

mathewc