预定的网络作业

Scheduled WebJob

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

我希望这项工作根据时间表 运行 但在 Main() 方法中 -- 见下文 -- Microsoft 为你提供 host.RunAndBlock() 的工作 [=19] =] 连续。

如果我想让工作 运行 定期安排,是否需要更改它?

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

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

我知道有 2 种方法可以安排 Web 作业而不是连续 运行:

  1. 使用 CRON 表达式创建计划的 WebJob
  2. 使用 Azure 计划程序创建计划的 WebJob

您可以在 azure.microsoft.com

上找到这两种方式的文档

我认为您需要 RunAndBlock 以防计划或连续,但如果您的工作是按需的,则可以删除它

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

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

您可以通过安装 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 获得 TimerTrigger 和其他扩展。使用 TimerTrigger 时,请务必在启动代码中添加对 config.UseTimers() 的调用以注册扩展。

使用 Azure WebJobs SDK 时,您将代码部署到 Continuous WebJob,并启用 AlwaysOn。然后,您可以在该 WebJob 中添加任意数量的预定函数。

按计划触发 WebJob 的一种简单方法是将其编码为常规控制台应用程序,然后只需将 'settings.job' 与基于 cron 的计划配置添加到项目中。 例如,以下定义将每 5 分钟触发一次:

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

无需使用 JobHost,只需确保您的 WebApp 配置为 'Always On'。 然后,您应该将该作业部署为触发的 WebJob。