在 Azure 计划程序中为作业生成高级计划和重复周期Build advanced schedules and recurrences for jobs in Azure Scheduler

Important

Azure 逻辑应用将替换即将停用的 Azure 计划程序。Azure Logic Apps is replacing Azure Scheduler, which is being retired. 若要继续使用在计划程序中设置的作业,请尽快迁移到 Azure 逻辑应用To continue working with the jobs that you set up in Scheduler, please migrate to Azure Logic Apps as soon as possible.

Azure 计划程序作业中,计划是确定计划程序服务何时以及如何运行作业的核心。Within an Azure Scheduler job, the schedule is the core that determines when and how the Scheduler service runs the job. 可以使用计划程序为作业设置多个一次性计划和重复计划。You can set up multiple one-time and recurring schedules for a job with Scheduler. 一次性计划仅在指定时间运行一次,并且基本上是仅运行一次的重复计划。One-time schedules run only once at a specified time and are basically recurring schedules that run only once. 重复计划按照指定频率运行。Recurring schedules run on a specified frequency. 由于具有这种灵活性,可以使用计划程序支持各种业务方案,例如:With this flexibility, you can use Scheduler for various business scenarios, for example:

  • 定期清理数据:创建每日作业,以删除超过三个月的所有推文。Clean up data regularly: Create a daily job that deletes all tweets older than three months.

  • 数据存档:创建每月作业,以将发票历史记录推送到备份服务。Archive data: Create a monthly job that pushes invoice history to a backup service.

  • 请求外部数据:创建一个每隔 15 分钟运行一次并从 NOAA 拉取新天气报告的作业。Request external data: Create a job that runs every 15 minutes and pulls a new weather report from NOAA.

  • 处理图像:创建工作日作业,以在非高峰时段运行并使用云计算来压缩当天上载的图像。Process images: Create a weekday job that runs during off-peak hours and uses cloud computing for compressing images uploaded during the day.

本文介绍可以使用计划程序和 Azure 计划程序 REST API 创建的示例作业,并涵盖每个计划的 JavaScript 对象表示法 (JSON) 定义。This article describes example jobs you can create by using Scheduler and the Azure Scheduler REST API, and includes the JavaScript Object Notation (JSON) definition for each schedule.

支持的方案Supported scenarios

以下示例展示了 Azure 计划程序支持的方案范围以及如何为各种行为模式创建计划,例如:These examples show the range of scenarios that Azure Scheduler supports and how to create schedules for various behavior patterns, for example:

  • 在特定的日期和时间运行一次。Run once at a specific date and time.
  • 运行并重复特定的次数。Run and recur a specific number of times.
  • 立即运行,然后重复。Run immediately and recur.
  • 运行并每隔 n 分钟、小时、天、周、月在特定的时间开始重复。Run and recur every n minutes, hours, days, weeks, or months, starting at a specific time.
  • 运行并根据每周或每月的频率重复,不过,只会在特定的星期日期或特定的月份日期重复。Run and recur weekly or monthly, but only on specific days of the week or on specific days of the month.
  • 运行并在特定时间段内重复多次。Run and recur more than once for a specific period. 例如,每月的最后一个星期五和星期一,或每天的上午 5:15 和下午 5:15。For example, every month on the last Friday and Monday, or daily at 5:15 AM and at 5:15 PM.

本文稍后将更详细地介绍这些方案。This article later describes these scenarios in more detail.

使用 REST API 创建计划Create schedule with REST API

若要使用 Azure 计划程序 REST API 创建基本计划,请执行以下步骤:To create a basic schedule with the Azure Scheduler REST API, follow these steps:

  1. 使用注册操作 - 资源管理器 REST API 向资源提供程序注册 Azure 订阅。Register your Azure subscription with a resource provider by using the Register operation - Resource Manager REST API. Azure 计划程序服务的提供程序名称为 Microsoft.SchedulerThe provider name for the Azure Scheduler service is Microsoft.Scheduler.

  2. 使用计划程序 REST API 中的为作业集合创建或更新操作创建作业集合。Create a job collection by using the Create or Update operation for job collections in the Scheduler REST API.

  3. 使用为作业创建或更新操作创建作业。Create a job by using the Create or Update operation for jobs.

作业架构元素Job schema elements

此表简要概述了在设置作业的重复周期和计划时可以使用的主要 JSON 元素。This table provides a high-level overview for the major JSON elements you can use when setting up recurrences and schedules for jobs.

元素Element 必须Required 说明Description
startTimestartTime No ISO 8601 格式的 DateTime 字符串值,用于指定作业在基本计划中首次启动的时间。A DateTime string value in ISO 8601 format that specifies when the job first starts in a basic schedule.

对于复杂的计划,作业的启动时间不早于 startTimeFor complex schedules, the job starts no sooner than startTime.

recurrencerecurrence No 作业运行时的重复规则。The recurrence rules for when the job runs. recurrence 对象支持这些元素:frequencyintervalschedulecountendTimeThe recurrence object supports these elements: frequency, interval, schedule, count, and endTime.

如果使用 recurrence 元素,则还必须使用 frequency 元素,而其他 recurrence 元素是可选的。If you use the recurrence element, you must also use the frequency element, while other recurrence elements are optional.

frequencyfrequency 是,当使用 recurrenceYes, when you use recurrence 两次作业之间的时间单位并支持这些值:“Minute”、“Hour”、“Day”、“Week”、“Month”和“Year”The time unit between occurrences and supports these values: "Minute", "Hour", "Day", "Week", "Month", and "Year"
intervalinterval No 一个正整数,根据 frequency 确定两次作业之间的时间单位数。A positive integer that determines the number of time units between occurrences based on frequency.

例如,如果 interval 为 10,frequency 为“Week”,则作业每隔 10 周重复一次。For example, if interval is 10 and frequency is "Week", the job recurs every 10 weeks.

下面是每种频率的最大间隔数:Here is the most number of intervals for each frequency:

- 18 个月- 18 months
- 78 周- 78 weeks
- 548 天- 548 days
- 对于小时和分钟,范围为 1 <= <interval> <= 1000。- For hours and minutes, the range is 1 <= <interval> <= 1000.

scheduleschedule No 根据指定的分钟标记、小时标记、星期日期和月份日期定义重复周期的更改Defines changes to the recurrence based on the specified minute-marks, hour-marks, days of the week, and days of the month
countcount No 一个正整数,指定作业在完成之前运行的次数。A positive integer that specifies the number of times that the job runs before finishing.

例如,当每日作业将 count 设置为 7,并且开始日期为星期一时,该作业将在星期日结束运行。For example, when a daily job has count set to 7, and the start date is Monday, the job finishes running on Sunday. 如果已经过了开始日期,则从创建时间开始计算第一次运行。If the start date has already passed, the first run is calculated from the creation time.

如果未指定 endTimecount,作业将无限期运行。Without endTime or count, the job runs infinitely. 不能在同一个作业中同时使用 countendTime,但首先完成的规则优先。You can't use both count and endTime in the same job, but the rule that finishes first is honored.

endTimeendTime No ISO 8601 格式的 Date 或 DateTime 字符串值,用于指定作业何时停止运行。A Date or DateTime string value in ISO 8601 format that specifies when the job stops running. 可为 endTime 设置一个过去的值。You can set a value for endTime that's in the past.

如果未指定 endTimecount,作业将无限期运行。Without endTime or count, the job runs infinitely. 不能在同一个作业中同时使用 countendTime,但首先完成的规则优先。You can't use both count and endTime in the same job, but the rule that finishes first is honored.

例如,此 JSON 架构描述了作业的基本计划和重复周期:For example, this JSON schema describes a basic schedule and recurrence for a job:

"properties": {
   "startTime": "2012-08-04T00:00Z", 
   "recurrence": {
      "frequency": "Week",
      "interval": 1,
      "schedule": {
         "weekDays": ["Monday", "Wednesday", "Friday"],
         "hours": [10, 22]                      
      },
      "count": 10,       
      "endTime": "2012-11-04"
   },
},

日期和日期时间值Dates and DateTime values

  • 计划程序作业中的日期只包括日期并遵循 ISO 8601 规范Dates in Scheduler jobs include only the date and follow the ISO 8601 specification.

  • 计划程序作业中的日期时间包括日期和时间,遵循 ISO 8601 规范,并且在未指定 UTC 时差时假定为 UTC。Date-times in Scheduler jobs include both date and time, follow the ISO 8601 specification, and are assumed to be UTC when no UTC offset is specified.

有关详细信息,请参阅概念、术语和实体For more information, see Concepts, terminology, and entities.

详细信息:startTimeDetails: startTime

此表描述了 startTime 如何控制作业的运行方式:This table describes how startTime controls the way a job runs:

startTimestartTime 无循环No recurrence 循环,无计划Recurrence, no schedule 按计划循环Recurrence with schedule
无开始时间No start time 立即运行一次。Run once immediately. 立即运行一次。Run once immediately. 根据上次执行时间进行计算后,运行后续执行。Run later executions calculated from the last execution time. 立即运行一次。Run once immediately. 根据循环计划运行后续执行。Run later executions based on a recurrence schedule.
开始时间在过去Start time in the past 立即运行一次。Run once immediately. 计算开始时间后的第一个将来运行时间,并在该时间运行。Calculate the first future run time after start time, and run at that time.

根据上次执行时间进行计算后,运行后续执行。Run later executions calculated from the last execution time.

请参阅此表后面的示例。See the example after this table.

一旦达到指定的开始时间,就立即启动作业。Start job no sooner than the specified start time. 第一次循环基于从开始时间计算的计划。The first occurrence is based on the schedule calculated from the start time.

根据循环计划运行后续执行。Run later executions based on a recurrence schedule.

开始时间在将来或当前Start time in the future or the current time 在指定的开始时间运行一次。Run once at the specified start time. 在指定的开始时间运行一次。Run once at the specified start time.

根据上次执行时间进行计算后,运行后续执行。Run later executions calculated from the last execution time.

一旦达到指定的开始时间,就立即启动作业。Start job no sooner than the specified start time. 第一次循环基于从开始时间计算的计划。The first occurrence is based on the schedule, calculated from the start time.

根据循环计划运行后续执行。Run later executions based on a recurrence schedule.

假设此示例具有以下条件:开始时间发生在过去,具有重复周期,但没有计划。Suppose you this example with these conditions: a start time in the past with a recurrence, but no schedule.

"properties": {
   "startTime": "2015-04-07T14:00Z", 
   "recurrence": {
      "frequency": "Day",
      "interval": 2
   }
}
  • 当前日期和时间为 2015 年 4 月 8 日下午 1:00。The current date and time is April 08, 2015 at 1:00 PM.

  • 开始日期和时间为 2015 年 4 月 7 日下午 2:00,早于当前日期和时间。The start date and time is April 07, 2015 at 2:00 PM, which is before the current date and time.

  • 每隔两天重复一次。The recurrence is every two days.

  1. 在上述条件下,首次执行时间为 2015 年 4 月 9 日下午 2:00。Under these conditions, the first execution is on April 09, 2015 at 2:00 PM.

    计划程序根据开始时间计算执行次数、放弃过去的所有实例,并使用将来的下一个实例。Scheduler calculates the execution occurrences based on the start time, discards any instances in the past, and uses the next instance in the future. 在本例中,“startTime”为 2015 年 4 月 7 日下午 2:00,因此下一个实例为从该时间算起的两天后,即 2015 年 4 月 9 日下午 2:00 。In this case, startTime is on April 07, 2015 at 2:00 PM, so the next instance is two days from that time, which is April 09, 2015 at 2:00 PM.

    首次执行时间是相同的,不管 startTime 是 2015-04-05 14:00 还是 2015-04-01 14:00。The first execution is the same whether startTime is 2015-04-05 14:00 or 2015-04-01 14:00. 在首次执行后,根据该计划计算后续执行。After the first execution, later executions are calculated based on the schedule.

  2. 后续执行的顺序如下:The executions then follow in this order:

    1. 2015-04-11 下午 2:002015-04-11 at 2:00 PM
    2. 2015-04-13 下午 2:002015-04-13 at 2:00 PM
    3. 2015-04-15 下午 2:002015-04-15 at 2:00 PM
    4. 依此类推...And so on...
  3. 最后,如果为作业指定了计划,但未指定小时和分钟,则这些值分别默认为首次执行的小时和分钟。Finally, when a job has a schedule but no specified hours and minutes, these values default to the hours and minutes in the first execution, respectively.

详细信息:scheduleDetails: schedule

可以使用 schedule 来限制作业执行的次数。 You can use schedule to limit the number of job executions. 例如,如果“frequency”为“month”的作业具有仅在 31 号运行的计划,该作业仅在包含 31 号的月份运行 。For example, if a job with a frequency of "month" has a schedule that runs only on day 31, the job runs only in months that have a 31st day.

还可以使用 schedule 来扩展作业执行的次数。 You can also use schedule to expand the number of job executions. 例如,如果频率为“month”的作业具有在1 号和 2 号运行的计划,则该作业将在每月的 1 号和 2 号运行,而不只是在每月运行一次。For example, if a job with a frequency of "month" has a schedule that runs on month days 1 and 2, the job runs on the first and second days of the month instead of only once a month.

如果指定了多个计划元素,则求值顺序为从大到小:周次、月份日期、星期、小时和分钟。If you specify more than one schedule element, the order of evaluation is from the largest to smallest: week number, month day, weekday, hour, and minute.

下表详细描述了 schedule 元素:The following table describes schedule elements in detail:

JSON 名称JSON name 说明Description 有效值Valid values
minutesminutes 运行作业的小时中的分钟。Minutes of the hour at which the job runs. 整数数组。An array of integers.
小时数hours 运行作业的日期中的小时。Hours of the day at which the job runs. 整数数组。An array of integers.
工作日weekDays 运行作业的星期日期。Days of the week the job runs. 只能配合每周频率指定。Can be specified only with a weekly frequency. 包含以下任意值的数组(最大数组大小为 7):An array of any of the following values (maximum array size is 7):
- "Monday"- "Monday"
- "Tuesday"- "Tuesday"
- "Wednesday"- "Wednesday"
- "Thursday"- "Thursday"
- "Friday"- "Friday"
- "Saturday"- "Saturday"
- "Sunday"- "Sunday"

不区分大小写。Not case-sensitive.
monthlyOccurrencesmonthlyOccurrences 确定运行作业的月份日期。Determines which days of the month the job runs. 只能配合每月频率指定。Can be specified only with a monthly frequency. monthlyOccurrences 对象的数组:An array of monthlyOccurrences objects:
{ "day": day, "occurrence": occurrence}

day 是运行作业的星期日期。day is the day of the week the job runs. 例如, {Sunday} 表示月份中的每个星期日。For example, {Sunday} is every Sunday of the month. 必需。Required.

occurrence 是月份中重复的日期。occurrence is the occurrence of the day during the month. 例如, {Sunday, -1} 表示月份中的最后一个星期日。For example, {Sunday, -1} is the last Sunday of the month. 可选。Optional.
monthDaysmonthDays 运行作业的月份日期。Day of the month the job runs. 只能配合每月频率指定。Can be specified only with a monthly frequency. 以下值的数组:An array of the following values:
<= -1 且 >= -31 的任意值- Any value <= -1 and >= -31
>= 1 且 <= 31 的任意值- Any value >= 1 and <= 31

示例:循环计划Examples: Recurrence schedules

以下示例演示各种循环计划。The following examples show various recurrence schedules. 这些示例着重于计划对象及其子元素。The examples focus on the schedule object and its subelements.

这些计划假设 interval 设置为 1.These schedules assume that interval is set to 1. 此外,这些示例假设正确的频率值符合 scheduleThe examples also assume the correct frequency values for the values in schedule. 例如,不能在使用 "day" 作为频率的同时,在计划中使用 monthDays 修改项。For example, you can't use a frequency of "day" and have a monthDays modification in schedule. 本文前面已描述这些限制。We describe these restrictions earlier in the article.

示例Example 说明Description
{"hours":[5]} 在每天的 5:00 AM 运行。Run at 5 AM every day.

计划程序会将 "hours" 中的每个值与 "minutes" 中的每个值一一匹配,以创建运行作业的所有时间的列表。Scheduler matches up each value in "hours" with each value in "minutes", one by one, to create a list of all the times at which the job runs.
{"minutes":[15], "hours":[5]} 在每天早晨 5:15 运行。Run at 5:15 AM every day.
{"minutes":[15], "hours":[5,17]} 在每天早晨 5:15 和下午 5:15 运行。Run at 5:15 AM and 5:15 PM every day.
{"minutes":[15,45], "hours":[5,17]} 在每天早晨 5:15、5:45 和下午 5:15、5:45 运行。Run at 5:15 AM, 5:45 AM, 5:15 PM, and 5:45 PM every day.
{"minutes":[0,15,30,45]} 每 15 分钟运行一次。Run every 15 minutes.
{hours":[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]} 每小时运行一次。Run every hour.

此作业每隔一小时运行一次。This job runs every hour. 分钟由“startTime”的值(如果已指定)控制 。The minute is controlled by the value for startTime, if it's specified. 如果未指定 startTime 值,则分钟由创建时间控制。If no startTime value is specified, the minute is controlled by the creation time. 例如,如果开始时间或创建时间(以适用的为准)为 12:25 PM,则作业会在 00:25、01:25、02:25 ... 23:25 运行。For example, if the start time or creation time (whichever applies) is 12:25 PM, the job runs at 00:25, 01:25, 02:25, …, 23:25.

该计划与“frequency”为“hour”、“interval”为 1 且无“schedule”值的作业相同 。The schedule is the same as a job with a frequency of "hour", an interval of 1, and no schedule value. 区别是,可以配合不同的 frequencyinterval 值使用此计划来创建其他作业。The difference is that you can use this schedule with different frequency and interval values to create other jobs. 例如,如果 frequency 为“month”,则该计划将每月运行一次,而不是每天运行一次(如果 frequency 为“day”)。For example, if frequency is "month", the schedule runs only once a month instead of every day (if frequency is "day").
{minutes:[0]} 每小时整点运行。Run every hour on the hour.

此作业也是每隔一小时运行一次,不过是在整点运行(例如 12:00 AM、1:00 AM、2:00 AM,等等)。This job also runs every hour, but on the hour (12 AM, 1 AM, 2 AM, and so on). 此计划与“frequency”为“hour”、“startTime”值为 0 分钟且无“schedule”(“frequency”为“day”时)的作业相同 。This schedule is the same as a job with a frequency of "hour", a startTime value of zero minutes, and no schedule, if the frequency is "day". 但是,如果频率为 "week" 或 "month",则计划分别只会在一个星期日期或月份日期执行。However, if the frequency is "week" or "month", the schedule executes only one day a week or one day a month, respectively.
{"minutes":[15]} 在过去每隔一小时的第 15 分钟运行。Run at 15 minutes past the hour every hour.

每隔一小时运行,从 00:15 AM、1:15 AM、2:15 AM 等开始。Runs every hour, starting at 00:15 AM, 1:15 AM, 2:15 AM, and so on. 在 11:15 PM 结束。It ends at 11:15 PM.
{"hours":[17], "weekDays":["saturday"]} 在每周星期六的 5:00 PM 运行。Run at 5 PM on Saturday every week.
{hours":[17], "weekDays":["monday", "wednesday", "friday"]} 在每周星期一、星期三和星期五的 5:00 PM 运行。Run at 5 PM on Monday, Wednesday, and Friday every week.
{"minutes":[15,45], "hours":[17], "weekDays":["monday", "wednesday", "friday"]} 在每周星期一、星期三和星期五的下午 5:15 和下午 5:45 运行。Run at 5:15 PM and 5:45 PM on Monday, Wednesday, and Friday every week.
{"hours":[5,17], "weekDays":["monday", "wednesday", "friday"]} 在每周星期一、星期三和星期五的 5:00 AM 和 5:00 PM 运行。Run at 5 AM and 5 PM on Monday, Wednesday, and Friday every week.
{"minutes":[15,45], "hours":[5,17], "weekDays":["monday", "wednesday", "friday"]} 在每周星期一、星期三和星期五的 5:15 AM、5:45 AM、5:15 PM 和 5:45 PM 运行。Run at 5:15 AM, 5:45 AM, 5:15 PM, and 5:45 PM on Monday, Wednesday, and Friday every week.
{"minutes":[0,15,30,45], "weekDays":["monday", "tuesday", "wednesday", "thursday", "friday"]} 在工作日每 15 分钟运行一次。Run every 15 minutes on weekdays.
{"minutes":[0,15,30,45], "hours": [9, 10, 11, 12, 13, 14, 15, 16] "weekDays":["monday", "tuesday", "wednesday", "thursday", "friday"]} 在工作日的 9:00 AM 到 4:45 PM 每 15 分钟运行一次。Run every 15 minutes on weekdays, between 9 AM and 4:45 PM.
{"weekDays":["sunday"]} 在星期日的开始时间运行。Run on Sundays at start time.
{"weekDays":["tuesday", "thursday"]} 在星期二和星期四的开始时间运行。Run on Tuesdays and Thursdays at start time.
{"minutes":[0], "hours":[6], "monthDays":[28]} 在每月 28 日的早晨 6 点运行(假设“frequency”为“month”) 。Run at 6 AM on the 28th day of every month (assuming a frequency of "month").
{"minutes":[0], "hours":[6], "monthDays":[-1]} 在当月最后一天的 6:00 AM 运行。Run at 6 AM on the last day of the month.

如果要在月份的最后一天运行作业,请使用 -1 而不是日期 28、29、30 或 31。If you want to run a job on the last day of a month, use -1 instead of day 28, 29, 30, or 31.
{"minutes":[0], "hours":[6], "monthDays":[1,-1]} 在每月第一天和最后一天的 6:00 AM 运行。Run at 6 AM on the first and last day of every month.
{monthDays":[1,-1]} 在每月第一天和最后一天的开始时间运行。Run on the first and last day of every month at start time.
{monthDays":[1,14]} 在每月第一天和第 14 天的开始时间运行。Run on the first and 14th day of every month at start time.
{monthDays":[2]} 在月份第二天的开始时间运行。Run on the second day of the month at start time.
{"minutes":[0], "hours":[5], "monthlyOccurrences":[{"day":"friday", "occurrence":1}]} 在每月第一个星期五的 5:00 AM 运行。Run on the first Friday of every month at 5 AM.
{"monthlyOccurrences":[{"day":"friday", "occurrence":1}]} 在每月第一个星期五的开始时间运行。Run on the first Friday of every month at start time.
{"monthlyOccurrences":[{"day":"friday", "occurrence":-3}]} 在每月从月末算起的第三个星期五的开始时间运行。Run on the third Friday from the end of the month, every month, at start time.
{"minutes":[15], "hours":[5], "monthlyOccurrences":[{"day":"friday", "occurrence":1},{"day":"friday", "occurrence":-1}]} 在每月第一个和最后一个星期五的早晨 5:15 运行。Run on the first and last Friday of every month at 5:15 AM.
{"monthlyOccurrences":[{"day":"friday", "occurrence":1},{"day":"friday", "occurrence":-1}]} 在每月第一个和最后一个星期五的开始时间运行。Run on the first and last Friday of every month at start time.
{"monthlyOccurrences":[{"day":"friday", "occurrence":5}]} 在每月第五个星期五的开始时间运行。Run on the fifth Friday of every month at start time.

如果月份中没有第五个星期五,则不运行作业。If there's no fifth Friday in a month, the job doesn't run. 如果想要在月份的最后一个星期五运行作业,可以考虑为循环使用 -1 而不是 5。You might consider using -1 instead of 5 for the occurrence if you want to run the job on the last occurring Friday of the month.
{"minutes":[0,15,30,45], "monthlyOccurrences":[{"day":"friday", "occurrence":-1}]} 在月份的最后一个星期五每 15 分钟运行一次。Run every 15 minutes on the last Friday of the month.
{"minutes":[15,45], "hours":[5,17], "monthlyOccurrences":[{"day":"wednesday", "occurrence":3}]} 在每月第三个星期三的早晨 5:15、早晨 5:45、下午 5:15、下午 5:45 运行。Run at 5:15 AM, 5:45 AM, 5:15 PM, and 5:45 PM on the third Wednesday of every month.

另请参阅See also