You are currently viewing Run Background Tasks using Azure Web Jobs

Run Background Tasks using Azure Web Jobs

In this next article of AZ-203 certification series, we will have look at how web jobs can be used to accomplish the simple background tasks required for your application.

In this article, we will try to create a simple Web Job using Azure Web Job SDK and will try to configure it using the Azure Portal.

For following some of the steps in this article, you will need an Azure account. If you do not have an Azure account,  you can create free account on Azure.

What is background task ?

Every application has some background jobs which should be executed by the server. These jobs are not executed on user who are interacting with your web application, because you do not want to affect the response time of the task.

Some of the examples of such background tasks include:

  • Generating some reports periodically
  • Generating very long transaction (historic) statements for a user’s account
  • Orchestrating or integrating with multiple systems/applications, etc.

When you deploy your application on-premise, such background jobs can be configured e.g. under windows scheduled tasks list or they can run as windows service which periodically performs some operation.

So, what is Azure Web Job ?

On Azure, you can configure the background tasks as Web Jobs.

Web Job is a feature of Azure App Service, which lets you run a program or a script in the same context of your web app, mobile app or API app.

Many file types are supported for Web Jobs, some of them include:

  • *.exe, *.bat, *.cmd (Windows executables)
  • *.ps1 (PowerShell)
  • *.sh (Bash script)
  • *.php (PHP script)
  • *.py (Python script)
  • *.js (NodeJS)
  • *.java (Java).

Web jobs run as a part of same App Service Plan which is used to host your web application. Hence, they run at no additional cost.

The Azure WebJobs SDK provides APIs that can simplify your work of creating a Web Job.

Types of Web Jobs

There are two types of web jobs – continuous and triggered.

The continuous jobs start as soon as they are configured. Generally the code looks like an endless loop performing something. If somehow the job ends, you can restart it. The continuous jobs also support remote debugging. The continuous jobs run on all instances on which web app is running. You can also restrict them to run on single instance.

The triggered jobs execute only if they are triggered manually OR if they are scheduled. The triggered jobs run only on the single instance that Azure chooses. Also, you cannot remotely debug the triggered jobs.

Hello Web Job World !

Let’s try to create a simple Web job using Azure Web Jobs SDK.

We will create a Web Job which is of continuous type and whenever there is a message in the storage queue, it logs that message. For this, we will create a console application and create a storage account which will have the storage queue.

Then we will configure the Web Job.

Remember that Web Jobs use same App Service Plan as of your Web App ? So, we will need an App Service app deployed in Azure. Please refer my earlier blog in this series which explains a bit about what is App Service.

Let’s start with console application.

Let’s put below code in the Main method. This method sets up the “host” for the web job. A “host” is a runtime container, that listens to the events and triggers the function that contains the logic you want to execute.

var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
    {
        b.AddAzureStorageCoreServices();
        b.AddAzureStorage();
    });        
builder.ConfigureLogging((loggingBuilder) =>
    {
        loggingBuilder.AddConsole();
    });

var host = builder.Build();
using (host)
{
    host.Run();
}

Then add a new class which contains the function which should get triggered when some event occurs. Lets call this file as “Functions.cs”.

Please make sure this class has below code. The QueueTrigger attribute checks a queue with name “my-queue” and whenever there is any content on the queue, the string contents are then logged using logger.

public class Functions
{
    public static void ProcessQueueMessage([QueueTrigger("my-queue")] string message, ILogger logger)
    {
        logger.LogInformation("START of Execution of your Web Job");
        logger.LogInformation(message);
        logger.LogInformation("END of Execution of your Web Job");
    }
}

In order to connect with Storage Account, we will need to have connection string of storage account in our console application.

Add a JSON file with name appsettings.json and add below connection string in the file. Select the appsettings.json file in Solution Explorer and in the Properties window, set Copy to Output Directory to Copy if newer.

{
  "AzureWebJobsStorage": "{storage connection string}"
}

Create a storage account which will hold the storage queue. Please refer this blog article for creating storage account steps. Please make sure you replace {storage connection string} with actual connection string of the storage account you create.

Then publish an App service web app. Please refer this blog article for step by step details.

Publish Web Job

You can publish the Web Job through CI/CD pipeline, or using Visual Studio (right click and select publish), or using Azure Portal.

You can refer to my previous blog related to App Services, where we have published a web application as an App Service. Similar steps are required to publish the console application which we created.

In Azure Portal, go to App Service Page and Select Web Jobs and then click on Add.

A new panel will open where you need to provide 4 inputs:

  • Name: Name of the web job, unique within the selected app service
  • File Upload: Zip file that contains executable file
  • Type: The type of web job, continuous or triggered. Select continuous.
  • Scale: On all instances or on single instance. Select single instance for this demo.

When you click on OK button, the job will be created and it will start running as soon as it is created.

Then you can go to Storage Explorer. Login to your account in Storage Explorer. Then go to storage account and create a queue with name “my-queue”. Then click on + button at top to add a new message to queue. Type your message.

Then go to Azure Portal and select the web job you created and click on logs. You will be redirected to some other page. When you click on toggle output button on the new page opened, you will be able to find the logs generated by web job.

I hope you understood how to create a web job. You can try registering the same console application as a manually triggered web job as well. Please do comment and let me know how it goes.

Leave a Reply