按定义的计划运行 ACR 任务Run an ACR task on a defined schedule

本文介绍如何按计划运行 ACR 任务This article shows you how to run an ACR task on a schedule. 通过设置一个或多个计时器触发器来计划任务。 Schedule a task by setting up one or more timer triggers.

任务计划适用于如下所述的方案:Scheduling a task is useful for scenarios like the following:

  • 运行容器工作负荷来执行计划性维护操作。Run a container workload for scheduled maintenance operations. 例如,运行容器化应用以从注册表中删除不需要的映像。For example, run a containerized app to remove unneeded images from your registry.
  • 在工作日针对要在现场监视的生产映像运行一组测试。Run a set of tests on a production image during the workday as part of your live-site monitoring.

可以使用本地安装的 Azure CLI 来运行本文中的示例。You can use a local installation of the Azure CLI to run the examples in this article. 若要在本地使用 Azure CLI,需要安装 2.0.68 或更高版本。If you'd like to use it locally, version 2.0.68 or later is required. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

关于计划任务About scheduling a task

  • 使用 cron 表达式的触发器 - 任务的计时器触发器使用 cron 表达式。 Trigger with cron expression - The timer trigger for a task uses a cron expression. 该表达式是包含五个字段的字符串,这些字段指定要触发任务的分钟、小时、日期、月份和星期。The expression is a string with five fields specifying the minute, hour, day, month, and day of week to trigger the task. 支持的最高频率为每分钟一次。Frequencies of up to once per minute are supported.

    例如,表达式 "0 12 * * Mon-Fri" 在每个工作日的中午(UTC 时间)触发任务。For example, the expression "0 12 * * Mon-Fri" triggers a task at noon UTC on each weekday. 请参阅本文稍后所述的详细信息See details later in this article.

  • 多个计时器触发器 - 允许将多个计时器添加到g wh 任务,前提是计划不同。Multiple timer triggers - Adding multiple timers to a task is allowed, as long as the schedules differ.

    • 可在创建任务时指定多个计时器触发器,也可以在以后添加。Specify multiple timer triggers when you create the task, or add them later.
    • (可选)为触发器命名以便于管理,否则 ACR 任务将提供默认的触发器名称。Optionally name the triggers for easier management, or ACR Tasks will provide default trigger names.
    • 如果多个计时器计划在某个时间发生重叠,ACR 任务将在每个计时器的计划时间触发任务。If timer schedules overlap at a time, ACR Tasks triggers the task at the scheduled time for each timer.
  • 其他任务触发器 - 在计时器触发的任务中,还可以基于源代码提交基础映像更新启用触发器。Other task triggers - In a timer-triggered task, you can also enable triggers based on source code commit or base image updates. 与其他 ACR 任务一样,也可以手动触发计划任务。Like other ACR tasks, you can also manually trigger a scheduled task.

创建具有计时器触发器的任务Create a task with a timer trigger

使用 az acr task create 命令创建任务时,可以选择性地添加计时器触发器。When you create a task with the az acr task create command, you can optionally add a timer trigger. 添加 --schedule 参数并为计时器传递 cron 表达式。Add the --schedule parameter and pass a cron expression for the timer.

举个简单的例子,以下命令将会触发每天 21:00(UTC 时间)从 Docker Hub 运行 hello-world 映像的任务。As a simple example, the following command triggers running the hello-world image from Docker Hub every day at 21:00 UTC. 该任务无需源代码上下文即可运行。The task runs without a source code context.

az acr task create \
  --name mytask \
  --registry myregistry \
  --cmd hello-world \
  --schedule "0 21 * * *" \
  --context /dev/null

运行 az acr task show 命令查看该计时器触发器是否已配置。Run the az acr task show command to see that the timer trigger is configured. 默认情况下,还启用了基础映像更新触发器。By default, the base image update trigger is also enabled.

$ az acr task show --name mytask --registry registry --output table
--------  ----------  --------  -------------------     -----------------
mytask    linux       Enabled                           BASE_IMAGE, TIMER

使用 az acr task run 手动触发任务,以确保正确设置该任务:Trigger the task manually with az acr task run to ensure that it is set up properly:

az acr task run --name mytask --registry myregistry

如果容器成功运行,输出将如下所示:If the container runs successfully, the output is similar to the following:

Queued a run with ID: cf2a
Waiting for an agent...
2019/06/28 21:03:36 Using acb_vol_2ca23c46-a9ac-4224-b0c6-9fde44eb42d2 as the home volume
2019/06/28 21:03:36 Creating Docker network: acb_default_network, driver: 'bridge'
2019/06/28 21:03:38 Launching container with name: acb_step_0

Hello from Docker!
This message shows that your installation appears to be working correctly.

在计划的时间之后,运行 az acr task list-runs 命令验证计时器是否按预期触发了任务:After the scheduled time, run the az acr task list-runs command to verify that the timer triggered the task as expected:

az acr task list-runs --name mytask --registry myregistry --output table

如果计时器成功,输出将如下所示:When the timer is successful, output is similar to the following:

--------  -------- ----------  ---------  ---------  --------------------  ----------
cf2b      mytask   linux       Succeeded  Timer      2019-06-28T21:00:23Z  00:00:06
cf2a      mytask   linux       Succeeded  Manual     2019-06-28T20:53:23Z  00:00:06

管理计时器触发器Manage timer triggers

使用 az acr task timer 命令管理 ACR 任务的计时器触发器。Use the az acr task timer commands to manage the timer triggers for an ACR task.

添加或更新计时器触发器Add or update a timer trigger

创建任务后,可以选择性地使用 az acr task timer add 命令添加计时器触发器。After a task is created, optionally add a timer trigger by using the az acr task timer add command. 以下示例将名为 timer2 的计时器触发器添加到前面创建的 mytaskThe following example adds a timer trigger name timer2 to mytask created previously. 此计时器每日 10:30(UTC 时间)触发任务。This timer triggers the task every day at 10:30 UTC.

az acr task timer add \
  --name mytask \
  --registry myregistry \
  --timer-name timer2 \
  --schedule "30 10 * * *"

使用 az acr task timer update 命令更新现有触发器的计划或更改其状态。Update the schedule of an existing trigger, or change its status, by using the az acr task timer update command. 例如,更新名为 timer2 的触发器,以在 11:30(UTC 时间)触发任务:For example, update the trigger named timer2 to trigger the task at 11:30 UTC:

az acr task timer update \
  --name mytask \
  --registry myregistry \
  --timer-name timer2 \
  --schedule "30 11 * * *"

列出计时器触发器List timer triggers

az acr task timer list 命令可显示针对任务设置的计时器触发器:The az acr task timer list command shows the timer triggers set up for a task:

az acr task timer list --name mytask --registry myregistry

示例输出:Example output:

    "name": "timer2",
    "schedule": "30 11 * * *",
    "status": "Enabled"
    "name": "t1",
    "schedule": "0 21 * * *",
    "status": "Enabled"

删除计时器触发器Remove a timer trigger

使用 az acr task timer remove 命令从任务中删除计时器触发器。Use the az acr task timer remove command to remove a timer trigger from a task. 以下示例从 mytask 中删除 timer2 触发器:The following example removes the timer2 trigger from mytask:

az acr task timer remove \
  --name mytask \
  --registry myregistry \
  --timer-name timer2

Cron 表达式Cron expressions

ACR 任务使用 NCronTab 库来解释 cron 表达式。ACR Tasks uses the NCronTab library to interpret cron expressions. ACR 任务支持的表达式包含空格分隔的五个必需字段:Supported expressions in ACR Tasks have five required fields separated by white space:

{minute} {hour} {day} {month} {day-of-week}

cron 表达式使用的时区为协调世界时 (UTC)。The time zone used with the cron expressions is Coordinated Universal Time (UTC). 时间为 24 小时制。Hours are in 24-hour format.


ACR 任务不支持在 cron 表达式中使用 {second}{year} 字段。ACR Tasks does not support the {second} or {year} field in cron expressions. 如果复制其他系统中使用的 cron 表达式,请确保删除这些字段(如果已使用)。If you copy a cron expression used in another system, be sure to remove those fields, if they are used.

每个字段可以具有下列类型之一的值:Each field can have one of the following types of values:

类型Type 示例Example 何时触发When triggered
一个具体值A specific value "5 * * * *" 每小时的第 5 分钟every hour at 5 minutes past the hour
所有值 (*)All values (*) "* 5 * * *" 从 5:00 UTC 开始每隔一分钟(每天 60 次)every minute of the hour beginning 5:00 UTC (60 times a day)
一个范围(- 运算符)A range (- operator) "0 1-3 * * *" 每天 1:00、2:00 和 3:00 UTC,触发 3 次3 times per day, at 1:00, 2:00, and 3:00 UTC
一组值(, 运算符)A set of values (, operator) "20,30,40 * * * *" 每小时的第 20 分钟、30 分钟和 40 分钟,触发 3 次3 times per hour, at 20 minutes, 30 minutes, and 40 minutes past the hour
一个间隔值(/ 运算符)An interval value (/ operator) "*/10 * * * *" 每小时的第 10 分钟、20 分钟...,触发 6 次6 times per hour, at 10 minutes, 20 minutes, and so on, past the hour

若要指定月份或天,可以使用数字值、名称或名称的缩写:To specify months or days you can use numeric values, names, or abbreviations of names:

  • 对于天,数字值为 0 到 6,其中 0 表示星期日。For days, the numeric values are 0 to 6 where 0 starts with Sunday.
  • 名称采用英语。Names are in English. 例如:MondayJanuaryFor example: Monday, January.
  • 名称不区分大小写。Names are case-insensitive.
  • 名称可缩写。Names can be abbreviated. 三字母是建议的缩写长度。Three letters is the recommended abbreviation length. 例如:MonJanFor example: Mon, Jan.

Cron 示例Cron examples

示例Example 何时触发When triggered
"*/5 * * * *" 每五分钟一次once every five minutes
"0 * * * *" 每小时一次(在每小时的开头)once at the top of every hour
"0 */2 * * *" 每两小时一次once every two hours
"0 9-17 * * *" 从 9:00 到 17:00 UTC 每小时触发一次once every hour from 9:00 to 17:00 UTC
"30 9 * * *" 每天 9:30 UTCat 9:30 UTC every day
"30 9 * * 1-5" 每个工作日的 9:30 UTCat 9:30 UTC every weekday
"30 9 * Jan Mon" 一月份的每个星期一 9:30 UTCat 9:30 UTC every Monday in January