预定的Web作业 [英] Scheduled WebJob

查看:53
本文介绍了预定的Web作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的Azure WebJob项目-它似乎是可以作为Web作业运行的控制台应用程序的精简版本.

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.

我希望此作业根据计划运行,但要使用Main()方法-见下文-Microsoft为您提供host.RunAndBlock()以便该作业连续运行.

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();
}

推荐答案

使用Azure WebJobs SDK时,可以使用TimerTrigger声明按计划运行的作业功能.例如,下面的函数在启动时立即运行,然后每两个小时运行一次:

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!");
}

通过安装 Microsoft.Azure,可以获得TimerTrigger和其他扩展名. .WebJobs.Extensions nuget软件包.有关TimerTrigger和该软件包中其他扩展以及如何使用它们的更多信息,请参见

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.

使用Azure WebJobs SDK时,将代码部署到连续 WebJob,并启用 AlwaysOn .然后,您可以在该WebJob中添加所需的许多计划功能.

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.

这篇关于预定的Web作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆