Scheduled WebJobs have existed from the beginning of Azure WebJobs, they are a result of combining 2 different Azure resources: a triggered WebJob containing the script to run and an Azure Scheduler job containing the schedule to use.
The Azure Scheduler job would point to the triggered WebJob invoke url and would make a request to it on schedule.
There have been some difficulties with this approach mainly around the deployment part of the schedule which convinced us to build another scheduler implementation that is built into kudu (the WebJobs host) which enables a scheduled WebJob to be deployed by simply adding a file with the WebJob binaries.
The way to add a schedule to a triggered WebJob is by adding 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).
If you already have this file with other settings simply add the
schedule
property.
The schedule is configured using a cron expression which is a common way to write schedules.
There are many pages that can teach you how to write a cron expression, I will describe the main format used for the scheduled WebJob.
{second} {minute} {hour} {day} {month} {day of the week}
.,
-
*
/
0 0 13 * * *
- 1pm every day.0 15 9 * * *
- 9:15am every day.0 0/5 16 * * *
- Every 5 minutes starting at 4pm and ending at 4:55pm, every day.0 11 11 11 11 *
- Every November 11th at 11:11am.You can find some more cron expression samples here but note that they have 5 fields, to use them you should add a 0
as the first field (for the seconds).
Important - To use this way of scheduling WebJobs you'll have to configure the website as Always On (just as you would with continuous WebJobs) otherwise the scm website will go down and the scheduling will stop until it is brought up again.
To see the scheduler logs for a scheduled WebJob you need to use the get triggered WebJob api, go to the url: https://{sitename}.scm.azurewebsites.net/api/triggeredwebjobs/{jobname} (remove the job name to see all triggered WebJobs).
You will receive the following json result:
{
name: "jobName",
runCommand: "...\run.cmd",
type: "triggered",
url: "http://.../triggeredwebjobs/jobName",
history_url: "http://.../triggeredwebjobs/jobName/history",
extra_info_url: "http://.../",
scheduler_logs_url: "https://.../vfs/data/jobs/triggered/jobName/job_scheduler.log",
settings: { },
using_sdk: false,
latest_run:
{
id: "20131103120400",
status: "Success",
start_time: "2013-11-08T02:56:00.000000Z",
end_time: "2013-11-08T02:57:00.000000Z",
duration: "00:01:00",
output_url: "http://.../vfs/data/jobs/triggered/jobName/20131103120400/output_20131103120400.log",
error_url: "http://.../vfs/data/jobs/triggered/jobName/20131103120400/error_20131103120400.log",
url: "http://.../triggeredwebjobs/jobName/history/20131103120400",
trigger: "Schedule - 0 0 0 * * *"
}
}
The scheduler_logs_url
property has a url that will get you the scheduler log, that log will tell you some verbose information on the scheduling and invocation of the triggered WebJob.
There is also a trigger
property for a triggered WebJob run that tells you which schedule (or external user agent) invoked the specific run.
More information about WebJobs API.
If you have a Visual Studio Azure WebJob project, the way to add a schedule is by authoring the settings.job
file described above and adding it to the project.
In the solution explorer you'll need to change the properties of that settings.job
file and set the Copy to output directory to Copy always.
This will make sure the file is in the root directory of the WebJob.
Changing (or setting/removing) a schedule of a triggered WebJob is all about updating the
schedule
property of thesettings.job
file in the WebJob's directory (d:\home\site\wwwroot\App_Data\jobs\triggered\{jobname}
), whenever the file is updated the change is picked up and the schedule will change according.This means you can deploy the schedule in any way you wish including by updating the file on your git repository.
There are pros and cons to each way of scheduling a WebJob, review them and choose which way to go.
Pros
Cons
Pros
Cons
To summarize, we've introduced a new way to schedule WebJobs that is continuous deployment friendly, in some cases it won't be the right one to choose but if the cons doesn't bother you it is a simpler and way for you to schedule triggered WebJobs.
Please let us know how it works for you in the comments or better yet on kudu project issues.