azure-webjob-scheduling.jpg

Azure WebJob Scheduling

Azure WebJobs are extremely easy to write. If you have Visual Studio 2015 (earlier versions may work as well), then you can write a Hello World app in one minute. In another two minutes, you can publish it and have it running in Azure.

However, if you want to run it on a schedule, this can be a little tricky. For my first scheduled WebJob this went off without a hitch, but for some reason, my second WebJob was more finicky. It took a bit of investigation to figure this out, so I thought I would share my findings here…

Let’s start from the beginning.

If you have Visual Studio 2015, the simplest way to create an Azure WebJob is to create a console app. You can do this without Visual Studio, but that’s beyond the scope of this post. For a hello world test, your console application can be this simple:



using System;



namespace SimpleWebJob



{
class
Program



{
static
void Main(string[] args)



{
Console.WriteLine("Hello World");



}



}



}



Then simply right click on your project name and choose Publish as Azure WebJob:

The next screen you are presented with asks for a schedule. This may or may not work (more on that later).

Then you have a choice to make for selecting a publish target. The first option has given me more success when it comes to publishing the schedule chosen above. With that option, you’ll have to sign in to Azure.

If you choose the second option, you’ll need to first download a publish profile. On the old Azure portal (manage.windowsazure.com), this can be found in the quick glance portion of the dashboard for your web apps (you’ll have to create a web app if you don’t have one).

On the new Azure portal (portal.azure.com), this can be found near the top of the page for your web app.

Once you download publish profile you can import it into the Visual Studio wizard.

Regardless of your publish target, you can then validate your connection and publish.

After publishing you want to see something like the following in your output window:

Creating the scheduler job

Job schedule created

Web App was published successfully http://<yoursite>.azurewebsites.net/

However, you may notice one of the following message in your output window instead:

Warning : WebJob schedule for SimpleWebJob will not be created. WebJob schedules can only be created when the publish destination is an Azure Website

Web App was published successfully http://<yoursite>.azurewebsites.net/

I tend to see this when importing a publish profile, but don’t have this problem when selecting “Microsoft Azure Web Apps” as the target. Others have had these issues as well as evidenced here and here. However, even with the “Microsoft Azure Web Apps” target I can get this error:

Error : An error occurred while creating the WebJob schedule: Conflict: The provided job has a recurrence that is outside of quota. The minimum recurrence for the job collection is ’01:00:00′.

I get this error if I choose a frequency more often than one hour even if I am on the free tier for my web app. Others have had these issues as well as evidenced here and here. There pricing for these plans is at https://azure.microsoft.com/en-us/pricing/details/app-service/plans/. You may find that upgrading your web app from the free plan helps, but keep reading as there is another option.

If you see either of the above messages, your WebJob was probably published, but the schedule was not. It will only run on demand. Another clue you are having this problem is if you look at the WebJob in Azure you will see that it has a type/schedule of “On Demand”. However, I have seen it say “On Demand” in the new Azure portal when it actually mentioned the right schedule in the old Azure portal. Very strange.

Based on what I have seen, Azure WebJobs are not really intended to own the schedule. The reasons I state this include:

  1. It seems you need Visual Studio to do this approach.
  2. When using a publishing profile, the schedule appears to be ignored.
  3. The new Azure portal does not show the schedule when the old one does. For the new Azure portal, the schedule actually shows in the Scheduler (more on that below).
  4. There is a nice UI for editing the schedule, but to update the schedule you must go into the webjob-publish-settings.json file.

My belief is that built-in scheduling for WebJobs should be done using the Azure Scheduler directly. If you choose the Azure Scheduler, you have 3 pricing options. The free one has some limitations. The most important limitation is not only the maximum execution frequency of 1 hour, but also that you cannot have authentication with each schedule. This is needed if you want to schedule a WebJob. Some have had success at providing credentials on the URL (see this post for more details), but it did not work for me.

My solution was to upgrade the scheduler to Standard ($13.99 per unit; you get 10 “job collections created per hour” for one unit) and create my own schedule by hand. Here’s how I did it from the new Azure portal:

  1. Create a Scheduler or find your existing Scheduler (use search within the portal to find it)
  2. Upgrade to Standard if you are using the Free tier
  3. Download your publish profile as discussed further above and open the file with a text editor.
  4. Add a new scheduler job
  5. Give it a name and set the following properties:
    1. Action: Https
    2. Method: Post
    3. Url: https://<yoursite>.scm.azurewebsites.net/api/triggeredwebjobs/<webjobname>/run
    4. Body: (leave blank)
    5. Authentication Type: Basic
    6. Authentication Username: get the “useName” value from your publish profile – it might start with “$”
    7. Authentication Password: get the “userPWD” value from your publish profile
  6. Set a schedule. I noticed that the UTC offset was ignored for me so I kept it at UTC and just adjusted the advanced schedule settings accordingly.
  7. Make sure you save everything

One issue I noticed with this approach is that my authentication settings would get cleared whenever I made updates to the schedule, so you may want to re-enter the authentication settings if you make any changes to the schedule.

After the schedule is set up, you can go back to your WebJob in the Azure portal and wait for it to run. You can run it manually as well. Once it does run, you should see your Console.WriteLine output. Here is a sample of what you see:

[02/23/2016 03:51:42 > 1cd571: SYS INFO] Status changed to Initializing

[02/23/2016 03:51:42 > 1cd571: SYS INFO] Run script ‘SimpleWebJob.exe’ with script host – ‘WindowsScriptHost’

[02/23/2016 03:51:42 > 1cd571: SYS INFO] Status changed to Running

[02/23/2016 03:51:42 > 1cd571: INFO] Hello World

[02/23/2016 03:51:42 > 1cd571: SYS INFO] Status changed to Success

I know this is a lot of detail, but I hope this post clears things up for some.

Kirk LiemohnAzure WebJob Scheduling

Join the conversation

This site uses Akismet to reduce spam. Learn how your comment data is processed.