Azure Webjobs

Overview of Azure Webjobs

One of the many features provided by Azure Web Apps is the ability to run background processes to augment your web application. This feature is called Azure WebJobs. Azure WebJobs addresses a need common to many websites, which is to offload time-consuming or CPU-intensive tasks to another process. This helps ensure that the web front end that users are interacting with is able to respond to user requests quickly while asynchronously performing work in another process.

Azure WebJobs also provides an excellent solution for running routine maintenance tasks such as moving log files or updating databases the web app uses. If you use Azure WebJobs as part of your web app solution, the Azure WebJobs Dashboard provides a nice web interface where you can view, monitor, and invoke your web jobs.

Developing Webjobs

Developing an Azure WebJob will not involve using web frameworks such as ASP.NET, MVC, or Web API etc. A web job could be implemented using a variety of scripting languages. The following are all valid candidates for implementing a web job:

  • .cmd, .bat, .exe (Windows)
  • .ps1 (PowerShell)
  • .sh (Bash)
  • .py (Python)
  • .js (Node.Js)

When you add a web job to your web app environment, you must specify how your web job should be invoked. Azure WebJobs provides you three options to indicate how your web job should run.

These options are as follows:

  • Continuous. A continuous web job is always running and therefore may be implemented using a looping structure such as a while loop. But as you will see later, there are more elegant ways of implementing this kind of web job using the Azure WebJobs SDK.
  • Scheduled. A scheduled web job is invoked on a schedule. The schedule can be one where your web job is invoked only once at a date and time you specify. Or it can be a recurring schedule where your web job is invoked sequentially with a specified time span between invocations. A recurring schedule can be defined so that the time span between invocations is specified in minutes, hours, days, weeks, or months.
  • On-Demand. An on-demand web job will be invoked only when you specifically take action to invoke it. You can invoke the web job by using the Azure portal, programmatically by using the WebJobs REST API, or by using Windows PowerShell.

Create an Azure WebJob

To demonstrate some essential concepts for Azure WebJobs, we will leverage the code who createt before to create a very simple .NET console application using Visual Studio. From the main menu, select File > New > Project and choose the Console Application template.

Publish a web job from Visual Studio

To run this simple console application in Azure as a web job, you can leverage the Azure SDK tools to publish it to a web app environment. To do this, right-click the ConsoleWebJob project in Solution Explorer and select Publish as Azure WebJob.

Create Webjobs

This will open a dialog where you can specify the name of the web job and configure how you want it to run. For this demonstration, configure the web job as follows:

  • Set the WebJob run mode setting to Run on a Schedule.
  • Set the Recurrence setting to Recurring Job.
  • Set the Recur every setting to 1 Hours.

Click OK to proceed to the next dialog.

Webjobs in Azure

The ability to publish an Azure WebJob from Visual Studiois provided by the Microsoft.Web.WebJobs .Publish NuGet package. When you click OK in the Add Azure WebJob dialog, you may see a dialog briefly appear, indicating that this package is being downloaded. Whether you see it or not, this step adds a packages.config file to your console application project with a package reference to this NuGet package.

In the Publish Web dialog, click Microsoft Azure Web Apps, as shown in Figure below.

Publish Webjobs in Azure

In the Select Existing Web App dialog, set Existing Web Apps to the web app created before, “Microsoft Azure Web Apps.” If you don’t have an existing web app, then click New to create a new web app. Click OK to continue.

End of publishing a Webjobs

Create an Azure WebJob from Azure portal

To demonstrate this, go to the web app blade in the Azure portal, click the Settings icon. In the Settings blade, click WebJobs.

Webjobs from Azure Portal

Create a new web jobby clicking Add in the WebJobs blade toolbar. In the Add WebJob blade, configure the web job properties as follows:

  • Set the Name field to DemoWebJobFromPortal.
  • Set the How To Run field to On Demand.
  • Click the folder icon next to the File Upload field and locate the SimpleWebJob.exe you compiled in the previous section. Because this application has no dependencies, you can upload the .exe without zipping it first.

The Add WebJob blade will look similar to Figure below.

Add Webjobs from Azure Portal

Click Create at the bottom of the Add WebJob blade. This will return you to the WebJobs blade, and the DemoWebJobFromPortal web job will be present.