Azure Webjobs - Introduction

By: Rachel Scanlon on 9/28/2016
The purpose of this blog post is to explain what an Azure WebJob is, how it can be utilized from within a real world application and provide instruction as to how to set once up from the Microsoft Azure Portal. To follow this instruction, you must already have an existing Azure account and website. The following links provide guidance on how to set these up.

Setting up a Microsoft Azure account

Setting up a web application in Microsoft Azure.

What is an Azure WebJob?

Azure WebJobs provide an easy to run scripts or programs as background processes, within an existing website on Azure. They would be most commonly used for scenarios that need to be repeated often and on a scheduled basis, without input from a user. Some examples of use cases for an Azure WebJob would be;

  1. Maintaining the integrity of files stored in a directory and deleting or moving files that meet certain conditions; such as files older than a certain date, files of a certain type, files saved by a particular user, files over a specified size.
  2. Querying the database for records that meet certain conditions and performing specific tasks based on these conditions.
  3. Sending emails on a schedule.
  4. Managing the frequency of API requests.
  5. Processing a queue of events...

WebJobs are set up to run in two distinct ways. Triggered jobs are set up to run on a schedule or when a specific event occurs. Continuous jobs run constantly via a while loop. Accepted file types for Azure WebJobs are as follows; *.exe, *.js, *.py, *.php, *.cmd, *.bat and *.sh.

How to set up an Azure WebJob

For the purposes of this example, we are going to show you how to set up an Azure WebJob using a .NET Console application First you must build your console application in release mode.

Navigate to where your project is saved and to the “release” folder in the “bin” directory as shown below.

Select all of the files in that directory and add them to a zip folder. As you can see, we have previously done this. Ours is named Yours can be named anything you like as long as it contains all of the files in the “release” directory. The executable alone will not work without it’s dependent files.

Log in to your Azure Portal and navigate to app services. This will show all of your existing Azure websites.

Navigate to the application you want. In our case, this is “slawarnings”. Under “WebJobs”, you will see an option to add a webjob, as well as displaying and existing webjobs you already have set up.

Click “Add” on the top menu and it will give you an option to add a new WebJob. We are going to set up one that runs on a scheduled basis, every 5 minutes.

As you can see, we have taken a few different steps;

  • Entered a name for the WebJob
  • Navigated to our zip file that contains our “release” build, that we created in step 3 and uploaded it.
  • Selected a type of “Triggered”
  • Selected a trigger type of “Continuous”
  • Lastly, and the most crucial part, we have entered a “Chron” expression to tell the WebJob to run every 5 minutes (0 */5 * * * *). 

Press ok and this will set up your WebJob. It will appear to the left. In our case, under the “slawarningsemailWebJob.

How to manage an existing Azure WebJob

As you can see the options for managing an existing Azure WebJob are fairly self-explanatory. You select the WebJob you want and the options are above it.

The only one I feel is worth mentioning in more detail is the logs option. We have a WebJob below that has a status of “Failed 1 h ago”. If we clicked the logs button it gives us details of what caused the error and where it occurred in our app or the WebJob setup. This is obviously really useful .

For more broad instruction on getting started with Microsoft Azure, I have included some links for further self-paced learning.

Deploy an ASP.NET web app to Azure App Service, using Visual Studio

Microsoft Azure Documentation for .NET.

Category: Cloud, Software, Web Tags: Azure