0

I already have a WebJob that I created in .NET Core 2.1 which also uses DI. This particular WebJob runs continuously and the new one I'm trying to create will run at set intervals.

In the following code, I'm telling the WebJob to run continuously and need to remove those lines but I want to make sure I'm doing it right. I'm putting //REMOVE on the lines that I need to remove. Could someone please verify I'm doing it right?

Again, the idea is to create a WebJob that will run at set intervals so I need to remove the lines that indicate a continuously running WebJob.

static void Main(string[] args)
{
     IServiceCollection serviceCollection = new ServiceCollection();
     ConfigureServices(serviceCollection);

     var configuration = new JobHostConfiguration();
     configuration.Queues.MaxPollingInterval = TimeSpan.FromSeconds(1); // REMOVE
     configuration.Queues.BatchSize = 1; // REMOVE
     configuration.JobActivator = new CustomJobActivator(serviceCollection.BuildServiceProvider());
     configuration.UseTimers();

     var host = new JobHost(configuration);
     host.RunAndBlock(); // REMOVE
}

private static void ConfigureServices(IServiceCollection services)
{
    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .Build();

    // Resolve repositories
    services.AddTransient<IMyRepository, MyRepository>();

    // Create instances of clients
    services.AddSingleton(new MyCustomClient(configuration));

    // Azure connection strings for the WebJob
    Environment.SetEnvironmentVariable("AzureWebJobsDashboard", configuration.GetConnectionString("WebJobsDashboard"));
    Environment.SetEnvironmentVariable("AzureWebJobsStorage", configuration.GetConnectionString("WebJobsStorage"));
}
Sam
  • 26,817
  • 58
  • 206
  • 383

1 Answers1

0

I'm putting //REMOVE on the lines that I need to remove. Could someone please verify I'm doing it right?

AFAIK, the code lines you want to remove could not tell webjob to run at set intervals.

config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(1);

MaxPollingInterval is the max amount of time the WebJob will check the queue. If the queue is empty the WebJob will start checking less frequently up to a max of 10 min.

config.Queues.BatchSize = 2;  //the amount of items your WebJob will process at the same time 

For more details, you could refer to this article about webjob JobHostConfiguration.

When using the Azure WebJobs SDK you can use TimerTrigger to declare job functions that run on a schedule.

public static void StartupJob(
[TimerTrigger("0 0 */2 * * *", RunOnStartup = true)] TimerInfo timerInfo)
{
    Console.WriteLine("Timer job fired!");
}

You can get TimerTrigger and other extensions by installing the Microsoft.Azure.WebJobs.Extensions nuget package.
When using the TimerTrigger, be sure to add a call to config.UseTimers() to your startup code to register the extension.

config.UseTimers();  //allows us to use a timer trigger in our functions.

When using the Azure WebJobs SDK, you deploy your code to a Continuous WebJob, with AlwaysOn enabled. You can then add many scheduled functions you desire in that WebJob.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30