azure webjob java_使用 WebJobs 运行后台任务 - Azure App Service | Microsoft Docs

您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure 应用服务中使用 WebJobs 运行后台任务Run background tasks with WebJobs in Azure App Service

10/16/2018

本文内容

本文介绍如何使用 Azure 门户部署 WebJobs,以便上传可执行文件或脚本。This article shows how to deploy WebJobs by using the Azure portal to upload an executable or script. 有关如何使用 Visual Studio 开发和部署 WebJobs 的信息,请参阅使用 Visual Studio 部署 WebJobs。For information about how to develop and deploy WebJobs by using Visual Studio, see Deploy WebJobs using Visual Studio.

概述Overview

WebJobs 是 Azure 应用服务的一项功能,它允许你在与 Web 应用、API 应用或移动应用相同的实例中运行程序或脚本。WebJobs is a feature of Azure App Service that enables you to run a program or script in the same instance as a web app, API app, or mobile app. 使用 Web 作业无需支付额外的费用。There is no additional cost to use WebJobs.

重要

Linux 上的应用服务尚不支持 WebJobs。WebJobs is not yet supported for App Service on Linux.

可以结合 WebJobs 使用 Azure WebJobs SDK 来简化许多编程任务。The Azure WebJobs SDK can be used with WebJobs to simplify many programming tasks. For more information, see What is the WebJobs SDK.

Azure Functions 提供了运行程序和脚本的另一种方式。Azure Functions provides another way to run programs and scripts.

Web 作业类型WebJob types

下表描述了 连续 和 触发 Web 作业之间的差别。The following table describes the differences between continuous and triggered WebJobs.

连续Continuous

触发Triggered

创建 Web 作业后立即启动。Starts immediately when the WebJob is created. 若要防止作业终止,程序或脚本通常在无限循环中执行其工作。To keep the job from ending, the program or script typically does its work inside an endless loop. 如果作业确实终止,可将其重启。If the job does end, you can restart it.

仅当手动触发或按计划触发时启动。Starts only when triggered manually or on a schedule.

在运行 Web 应用的所有实例上运行。Runs on all instances that the web app runs on. 可以选择性地将 Web 作业限制为单个实例。You can optionally restrict the WebJob to a single instance.

在 Azure 选择用于负载均衡的单个实例上运行。Runs on a single instance that Azure selects for load balancing.

支持远程调试。Supports remote debugging.

不支持远程调试。Doesn't support remote debugging.

备注

Web 应用可在进入非活动状态 20 分钟后超时。A web app can time out after 20 minutes of inactivity. 并且只有向实际 Web 应用发出的请求才会重置计时器。and only requests to the actual web app can reset the timer. 在 Azure 门户中查看应用的配置或向高级工具站点 (https://.scm.azurewebsites.net) 发出请求不会重置计时器。Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://.scm.azurewebsites.net) doesn't reset the timer. 如果将 Web 应用设置为运行连续或计划的(计时器-触发器)Web 作用,请在 Web 应用的 Azure“配置”页面上启用“始终可用”设置,以确保 Web 作业可靠运行 。If you set your web app to run continuous or scheduled (timer-trigger) WebJobs, enable the Always on setting on your web app's Azure Configuration page to ensure that the WebJobs run reliably. 此功能仅在基本、标准和高级定价层中提供。This feature is available only in the Basic, Standard, and Premium pricing tiers.

支持的脚本或程序文件类型Supported file types for scripts or programs

支持以下文件类型:The following file types are supported:

.cmd、.bat、.exe(使用 Windows cmd).cmd, .bat, .exe (using Windows cmd)

.ps1(使用 PowerShell).ps1 (using PowerShell)

.sh(使用 Bash).sh (using Bash)

.php(使用 PHP).php (using PHP)

.py(使用 Python).py (using Python)

.js(使用 Node.js).js (using Node.js)

.jar(使用 Java).jar (using Java)

创建连续 Web 作业Create a continuous WebJob

重要

如果你使用应用程序配置了源代码管理,则 Web 作业应作为源代码管理集成的一部分进行部署。If you have source control configured with your application, the Webjobs should be deployed as part of the source control integration. 在应用程序中配置源代码管理后,将无法从 Azure 门户添加 WebJob。Once source control is configured with your application a WebJob cannot be add from the Azure Portal.

在 Azure 门户中,转到应用服务 Web 应用、API 应用或移动应用的“应用服务”页。In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.

选择“Web 作业”。Select WebJobs .

d15ee5ab0ebeea1ebced77e6eb2b632b.png

在“Web 作业”页中,选择“添加”。In the WebJobs page, select Add .

0a7e5026b346034a3890793d1d70f832.png

使用表中指定的“添加 Web 作业”设置。Use the Add WebJob settings as specified in the table.

6c16146a867862882d0d04ab78ee0235.png

设置Setting

示例值Sample value

说明Description

名称Name

myContinuousWebJobmyContinuousWebJob

在应用服务应用中唯一的名称。A name that is unique within an App Service app. 必须以字母或数字开头,且不能包含除“-”和“”以外的特殊字符。Must start with a letter or a number and cannot contain special characters other than "-" and "".

文件上传File Upload

ConsoleApp.zipConsoleApp.zip

一个 .zip 文件,其中包含可执行文件或脚本文件,以及运行程序或脚本所需的所有支持文件。A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. 支持的文件类型部分中列出了支持的可执行文件或脚本文件类型。The supported executable or script file types are listed in the Supported file types section.

类型Type

连续Continuous

本文前面介绍了 Web 作业类型。The WebJob types are described earlier in this article.

缩放Scale

多实例Multi instance

仅适用于连续 Web 作业。Available only for Continuous WebJobs. 确定程序或脚本是在所有实例还是只在一个实例上运行。Determines whether the program or script runs on all instances or just one instance. 指定要在多个实例上运行的选项不适用于免费或共享定价层。The option to run on multiple instances doesn't apply to the Free or Shared pricing tiers.

单击 “确定” 。Click OK .

新的 Web 作业随即显示在“Web 作业”页上。The new WebJob appears on the WebJobs page.

12d7b069a4d10498676db7dcaeeb7a67.png

要停止或重启某个连续 Web 作业,请在列表中右键单击该 Web 作业,并单击“停止”或“启动”。To stop or restart a continuous WebJob, right-click the WebJob in the list and click Stop or Start .

46d0269def04844a0934c4f09da22d11.png

创建手动触发的 Web 作业Create a manually triggered WebJob

在 Azure 门户中,转到应用服务 Web 应用、API 应用或移动应用的“应用服务”页。In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.

选择“Web 作业”。Select WebJobs .

d15ee5ab0ebeea1ebced77e6eb2b632b.png

在“Web 作业”页中,选择“添加”。In the WebJobs page, select Add .

0a7e5026b346034a3890793d1d70f832.png

使用表中指定的“添加 Web 作业”设置。Use the Add WebJob settings as specified in the table.

d74c7925583064fbb176743b1e4232f1.png

设置Setting

示例值Sample value

说明Description

名称Name

myTriggeredWebJobmyTriggeredWebJob

在应用服务应用中唯一的名称。A name that is unique within an App Service app. 必须以字母或数字开头,且不能包含除“-”和“”以外的特殊字符。Must start with a letter or a number and cannot contain special characters other than "-" and "".

文件上传File Upload

ConsoleApp.zipConsoleApp.zip

一个 .zip 文件,其中包含可执行文件或脚本文件,以及运行程序或脚本所需的所有支持文件。A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. 支持的文件类型部分中列出了支持的可执行文件或脚本文件类型。The supported executable or script file types are listed in the Supported file types section.

类型Type

触发Triggered

本文前面介绍了 Web 作业类型。The WebJob types are described earlier in this article.

触发器Triggers

手动Manual

单击 “确定” 。Click OK .

新的 Web 作业随即显示在“Web 作业”页上。The new WebJob appears on the WebJobs page.

12d7b069a4d10498676db7dcaeeb7a67.png

如果要运行 Web 作业,请在列表中右键单击其名称,并单击“运行” 。To run the WebJob, right-click its name in the list and click Run .

fb5c6e89986454d3e6457cbdde085992.png

创建计划的 Web 作业Create a scheduled WebJob

在 Azure 门户中,转到应用服务 Web 应用、API 应用或移动应用的“应用服务”页。In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.

选择“Web 作业”。Select WebJobs .

d15ee5ab0ebeea1ebced77e6eb2b632b.png

在“Web 作业”页中,选择“添加”。In the WebJobs page, select Add .

0a7e5026b346034a3890793d1d70f832.png

使用表中指定的“添加 Web 作业”设置。Use the Add WebJob settings as specified in the table.

ea67e10198b7abda0130a2870e944664.png

设置Setting

示例值Sample value

说明Description

名称Name

myScheduledWebJobmyScheduledWebJob

在应用服务应用中唯一的名称。A name that is unique within an App Service app. 必须以字母或数字开头,且不能包含除“-”和“”以外的特殊字符。Must start with a letter or a number and cannot contain special characters other than "-" and "".

文件上传File Upload

ConsoleApp.zipConsoleApp.zip

一个 .zip 文件,其中包含可执行文件或脚本文件,以及运行程序或脚本所需的所有支持文件。A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. 支持的文件类型部分中列出了支持的可执行文件或脚本文件类型。The supported executable or script file types are listed in the Supported file types section.

类型Type

触发Triggered

本文前面介绍了 Web 作业类型。The WebJob types are described earlier in this article.

触发器Triggers

计划Scheduled

要使计划可靠运行,请启用 Always On 功能。For the scheduling to work reliably, enable the Always On feature. Always On 仅在基本、标准和高级定价层中提供。Always On is available only in the Basic, Standard, and Premium pricing tiers.

CRON 表达式CRON Expression

0 0/20 * * * *0 0/20 * * * *

以下部分介绍了 CRON 表达式。CRON expressions are described in the following section.

单击 “确定” 。Click OK .

新的 Web 作业随即显示在“Web 作业”页上。The new WebJob appears on the WebJobs page.

12d7b069a4d10498676db7dcaeeb7a67.png

NCRONTAB 表达式NCRONTAB expressions

可以在门户中输入 NCRONTAB 表达式,或者在 Web 作业 .zip 文件的根目录中包含一个 settings.job 文件,如以下示例中所示:You can enter a NCRONTAB expression in the portal or include a settings.job file at the root of your WebJob .zip file, as in the following example:

{

"schedule": "0 */15 * * * *"

}

备注

运行 CRON 表达式使用的默认时区为协调世界时 (UTC)。The default time zone used to run CRON expressions is Coordinated Universal Time (UTC). 若要让 CRON 表达式基于其他时区运行,请为函数应用创建一个名为 WEBSITE_TIME_ZONE 的应用设置。To have your CRON expression run based on another time zone, create an app setting for your function app named WEBSITE_TIME_ZONE. 若要了解详细信息,请参阅 NCRONTAB 时区。To learn more, see NCRONTAB time zones.

查看作业历史记录View the job history

选择要查看其历史记录 Web 作业,并选择“日志”按钮。Select the WebJob you want to see history for, and then select the Logs button.

836a7e9f5f76b924417aae83afb16bcd.png

在“Web 作业详细信息”页中,选择一个时间以查看一个运行轮次的详细信息。In the WebJob Details page, select a time to see details for one run.

86be749a1796d1d6bbcc6bf6b3f07be8.png

在“Web 作业运行详细信息”页中,选择“切换输出”查看日志内容的文本。In the WebJob Run Details page, select Toggle Output to see the text of the log contents.

5dd1adc91f299d4ddcbb539109a20603.png

若要在单独的浏览器窗口中查看输出文本,请选择“下载”。To see the output text in a separate browser window, select download . 若要下载文本本身,请右键单击“下载”,并使用浏览器选项来保存文件内容。To download the text itself, right-click download and use your browser options to save the file contents.

选择页面顶部的“Web 作业”痕迹导航链接转到 Web 作业列表。Select the WebJobs breadcrumb link at the top of the page to go to a list of WebJobs.

b80c8c05c06a0d1e434f23e413d1ea1b.png

bbb3a0c51132ae7e0b4939f10e5adc9e.png

后续步骤Next steps

可以结合 WebJobs 使用 Azure WebJobs SDK 来简化许多编程任务。The Azure WebJobs SDK can be used with WebJobs to simplify many programming tasks. For more information, see What is the WebJobs SDK.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值