5

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()?
mathewc
  • 13,312
  • 2
  • 45
  • 53
Nadeem Tabbaa
  • 119
  • 3
  • 9
  • I believe all answers here are correct, as my question is about approach. but i have to select one. this note for any one who is seeing this post. – Nadeem Tabbaa Dec 22 '15 at 06:13

2 Answers2

3

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();
}
benjguin
  • 1,496
  • 1
  • 12
  • 21
  • so it will be the first code, that call the supportservice directly. but i have read the SDK provided in MSDN, and I really could not understand anything from it. do you recommend any documentation for the JobHost Class and Properties and functions. and when i should use everyone of them, and what is the best practice of each? – Nadeem Tabbaa Dec 21 '15 at 15:20
  • Yes I would use the first code you propose.The SDK does a great job in exposing Azure storage as a set of triggers and streams; in that case, the host has the role of the "infinite loop". – benjguin Dec 21 '15 at 16:53
1

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.

mathewc
  • 13,312
  • 2
  • 45
  • 53