Azure Cron Expression is a powerful tool for automating tasks, allowing you to schedule jobs to run at specific times or intervals.
To get started, you need to understand the syntax of Azure Cron Expression, which consists of six fields: minute, hour, day of month, month, day of week, and optional year.
The minute field can range from 0 to 59, and it's used to specify the minute when the job should run.
For example, if you want a job to run every minute, you would use the value * for the minute field.
What Are Azure Cron Expressions?
Azure Cron expressions are a way to schedule tasks in Azure. They're similar to traditional Cron jobs, but with some key differences.
A Cron expression consists of five or six fields that define the schedule. These fields are separated by spaces and can be any combination of numbers, asterisks, and special characters.
The first field represents the minute, with values ranging from 0 to 59. You can specify a specific minute, like 15, or use an asterisk to represent every minute.
The second field represents the hour, with values ranging from 0 to 23. You can specify a specific hour, like 9, or use an asterisk to represent every hour.
The third field represents the day of the month, with values ranging from 1 to 31. You can specify a specific day, like 15, or use an asterisk to represent every day.
The fourth field represents the month, with values ranging from 1 to 12. You can specify a specific month, like 6, or use an asterisk to represent every month.
The fifth field represents the day of the week, with values ranging from 0 to 6. You can specify a specific day, like 2, or use an asterisk to represent every day.
The sixth field is optional and represents the year. If not specified, it defaults to the current year.
Creating and Managing Schedules
Creating and managing schedules in Azure is a breeze. You can use the classic editor to select the days and times when you want to run the build, or define a cron expression using a six-field format.
The time zone for cron schedules is UTC, so make sure to keep that in mind when setting your schedules. You can use commas to specify multiple values, such as running a job at 8:00 AM, 12:00 PM, and 4:00 PM.
To add a schedule to a triggered WebJob, create a file called settings.job with the following JSON content: {"schedule": "the schedule as a cron expression"}. This file should be at the root of the WebJob directory, next to the WebJob executable.
Here are some tips for crafting complex schedules:
- Use commas for multiple values.
- Define ranges with a hyphen (-) and specify intervals with a forward slash (/).
- Be careful when specifying both day-of-week and day-of-month fields, as this can lead to confusion. Use ? to leave one unspecified.
Ncrontab
Ncrontab is a dialect of the popular CRON notation used in Azure WebJobs. It allows for more granular control over job execution timing.
You can use commas to separate multiple values within a field, such as running a job at several specific times. For instance, 0 0 8-10 * * * schedules a job every hour between 8 and 10 AM.
A sixth field at the beginning of the expression can be used to specify seconds, giving you more flexibility. This is optional, but it can be useful for precise timing.
To define ranges, use a hyphen (-) and specify intervals with a forward slash (/). For example, 0 0 8-10 * * * schedules a job every hour between 8 and 10 AM.
Here are some examples of Ncrontab expressions:
Be careful when specifying both day-of-week and day-of-month fields, as this can lead to confusion. Use ? to leave one unspecified.
You can play with the timer CRON setting to find the right execution time for your purpose. For example, to run a function every ten seconds, you can use a notation like 0 0 0/10 * * *. This is equal to the notation 0 0 */10 * * *.
Scheduled Triggers
Scheduled triggers are a powerful tool in Azure DevOps, allowing you to run pipelines on a schedule defined using cron syntax. The time zone for cron schedules is UTC, so make sure to keep that in mind when setting up your triggers.
If you have both YAML scheduled triggers and UI defined scheduled triggers in your pipeline, only the UI defined scheduled triggers will be run. To run the YAML defined scheduled triggers, you must remove the scheduled triggers defined in the pipeline settings UI.
Scheduled triggers configure a pipeline to run on a schedule defined using cron syntax. You can specify an exclude clause without an include clause for branches, which is equivalent to specifying * in the include clause. If you use templates in your YAML file, then the schedules must be specified in the main YAML file and not in the template files.
To delete UI scheduled triggers from a YAML pipeline, see UI settings override YAML scheduled triggers. You can't use pipeline variables when specifying schedules.
Here are some tips for crafting complex schedules:
- Use commas for multiple values: If you need a job to run at several specific times, use commas to separate the values within a field.
- Ranges and steps: Define ranges with a hyphen (-) and specify intervals with a forward slash (/). For instance, 0 0 8-10 * * * schedules a job every hour between 8 and 10 AM.
- Day of week vs. day of month: Be careful when specifying both day-of-week and day of month fields, as this can lead to confusion. Use ? to leave one unspecified.
The batch property configures whether to run the pipeline if the previously scheduled run is in progress. The following table describes how always and batch interact:
Advanced Scheduling Techniques
If you're looking to take your Azure WebJobs scheduling to the next level, you'll want to explore advanced techniques.
One powerful feature of Azure WebJobs is its ability to use a NCRONTAB expression, which includes an optional sixth field for seconds, allowing for more granular control over job execution timing.
To craft complex schedules, it's essential to understand how to use commas, ranges, and steps. Commas can be used to separate multiple values within a field, making it easy to schedule jobs at specific times.
For example, using commas, you can schedule a job to run at 8, 9, or 10 AM by using 0 0 8-10 * * *.
Ranges and steps are also crucial in crafting complex schedules. Ranges can be defined using a hyphen (-) to specify a range of values, while steps can be used to specify intervals with a forward slash (/).
Here's a brief rundown of how to use ranges and steps:
Be mindful of day-of-week and day-of-month fields, as specifying both can lead to confusion. To avoid this, use a question mark (?) to leave one field unspecified.
Finally, don't forget to check your time zone settings, as Azure WebJobs uses Coordinated Universal Time (UTC) by default. You can specify a different time zone in your settings if needed.
Sources
- https://learn.microsoft.com/en-us/azure/devops/pipelines/process/scheduled-triggers
- https://sandervandevelde.wordpress.com/2020/09/09/understanding-timertriggers-in-azure-functions/
- https://reintech.io/blog/advanced-job-scheduling-cron-expressions-azure-webjobs
- https://blog.amitapple.com/post/2015/06/scheduling-azure-webjobs/
- https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-scheduled-function
Featured Images: pexels.com