在 Azure 应用服务中使用 WebJobs 运行后台任务Run background tasks with WebJobs in Azure App Service
10/19/2020
本文内容
本文介绍如何使用 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.
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 应用发出的请求才会重置计时器。Only requests to the actual web app reset the timer. 在 Azure 门户中查看应用的配置或向高级工具站点 (https://.scm.chinacloudsites.cn) 发出请求不会重置计时器。Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://.scm.chinacloudsites.cn) don't reset the timer. 如果应用运行连续性或计划的(计时器触发器)WebJobs,可启用 Always On 来确保 WebJobs 可靠运行。If your app runs continuous or scheduled (Timer trigger) WebJobs, enable Always On 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
在 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 .
在“Web 作业”页中,选择“添加”。In the WebJobs page, select Add .
使用表中指定的“添加 Web 作业” 设置。Use the Add WebJob settings as specified in the table.
设置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.
要停止或重启某个连续 Web 作业,请在列表中右键单击该 Web 作业,并单击“停止”或“启动”。To stop or restart a continuous WebJob, right-click the WebJob in the list and click Stop or Start .
创建手动触发的 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 .
在“Web 作业”页中,选择“添加”。In the WebJobs page, select Add .
使用表中指定的“添加 Web 作业”设置。Use the Add WebJob settings as specified in the table.
设置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.
如果要运行 Web 作业,请在列表中右键单击其名称,并单击“运行” 。To run the WebJob, right-click its name in the list and click Run .
创建计划的 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 .
在“Web 作业”页中,选择“添加”。In the WebJobs page, select Add .
使用表中指定的“添加 Web 作业”设置。Use the Add WebJob settings as specified in the table.
设置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.
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.
在“Web 作业详细信息”页中,选择一个时间以查看一个运行轮次的详细信息。In the WebJob Details page, select a time to see details for one run.
在“Web 作业运行详细信息”页中,选择“切换输出”查看日志内容的文本。In the WebJob Run Details page, select Toggle Output to see the text of the log contents.
若要在单独的浏览器窗口中查看输出文本,请选择“下载”。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.
后续步骤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.