Scheduled WebJob

2019-03-27 12:30发布

I'm creating a new Azure WebJob project -- which appears to be a polished version of a console app that can run as a web job.

I want this job to run based on a schedule but in the Main() method -- see below -- Microsoft gives you the host.RunAndBlock() for the job to run continuously.

Do I need to change that if I want the job to run at regularly scheduled intervals?

static void Main()
{
    var host = new JobHost();

    // The following code ensures that the WebJob will be running continuously
    host.RunAndBlock();
}

3条回答
\"骚年 ilove
2楼-- · 2019-03-27 13:02

There are 2 ways that I know of for scheduling a web job instead of making it run continiously:

1.Create a scheduled WebJob using a CRON expression

2.Create a scheduled WebJob using the Azure Scheduler

You can find the documentation for both ways here:

https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/

I think you need RunAndBlock in case of Scheduled or Continuous but you can remove it if you have your job as on-demand

查看更多
仙女界的扛把子
3楼-- · 2019-03-27 13:11

When using the Azure WebJobs SDK you can use TimerTrigger to declare job functions that run on a schedule. For example here's a function that runs immediately on startup, then every two hours thereafter:

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. More information on TimerTrigger and the other extensions in that package and how to use them can be found in the azure-webjobs-sdk-extensions repo. When using the TimerTrigger, be sure to add a call to config.UseTimers() to your startup code to register the extension.

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

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-27 13:16

An easy way to trigger a WebJob on a schedule would be to code it as a regular console application, and just add a 'settings.job' with the cron based scheduling configuration to the project. For example, the following definition would trigger it every 5 minutes:

{
  "schedule": "0 */5 * * * *"
}

No need to use JobHost, just make sure your WebApp is configured as 'Always On'. You should then deploy the job as a triggered WebJob.

查看更多
登录 后发表回答