Best Azure WebJob Approach

This is my first time I created a WebJob application type. I created a webjob project, and in the solution it comes with Program.cs and Function.cs .

I already deleted Function.cs , because in this project there is no queue from which I get data.

Now in Program.cs there already exists the Main method:

 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 it, RunAndBlock should constantly run webjob, but I want the task to be executed only once. I want to control execution from outside on a schedule. I would like to know how to make my code only once? As you can see below, I have a SupportService class that has RunOnePoolProvisioingCycle , I want to call this method only once. 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()? 
+5
source share
2 answers

What you see is part of the SDK, which is optional. A web application can be as simple as a console application that you download, download and run as is.

So this code seems to be the best option in your case:

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

WebJob created using the template uses the WebJobs SDK . If you don’t need to use any SDK features, you can simply create a console application and set up a CRON schedule to run it (see "Scheduled Tasks" here ).

I contacted more information about the WebJobs SDK above. In addition to facilitating scenarios in which you want to run functions in queues / blocks / etc., It also has the ability to run your tasks on a schedule through TimerTrigger (part of the SDK extension ). Let these materials read to see what works best for your needs.

+1
source

Source: https://habr.com/ru/post/1238846/


All Articles