Sunday, September 27, 2020

What is azure web job?

WebJob is a feature of Azure App Service that enables you to run a program or script in the same instance as a web app, API app, or mobile app. There is no additional cost to use WebJob. WebJob is part of web app both will use same computing resources to execute.

App Service is the managed computing platform that Azure Web Apps runs on! So Azure Web App is one type of “App” that runs on App Service. For example, there are Web Apps, Web API, Logic Apps, Web Job, Mobile Apps and Azure Function Apps etc.

 


These “Apps” all have one thing in common i.e. they all do some kind of “compute work” without the developer worrying or managing infrastructure.

Why need WebJobs?

The purpose of the web job is to simplify the code and to perform the background jobs like window scheduler task, time taking process with help of queue (Storage Account), common tasks, such as image processing, file maintenance, and sending mails. The web job SDK has a great in-built feature for working with Azure storage and Service bus.

In below example in our To-do App (Web App) we need to update the to-do task status if due date passed. In this case our WebJob will run in background and it will check if Today date greater than due date it will update the record status as “Pending” in to-do database. Our WebJob will do all the time taking task in background and it will get task information from Queue in Storage Account (djblogsstorageaccount). We will create “tasks” queue in “djblogsstorageaccount” storage account.

How to create WebJobs?

First we will create WebApp then add WebJob into it. As I told before WebJob is part of WebApp. First we need to create WebApp and after that same publisher profile we will use to deploy "WebJob". We need to follow below steps to publish WebJob with help of  WebApp publisher profile.

1.       Go to azure portal https://portal.azure.com

2.       Once we login in portal then need to create new web app for to-do application.

After filling all this information our web app ready for publish. Our To-do web app url will be

URL: https://djblogstodo.azurewebsites.net/

For more information how to create web app in azure. Please read my previous blog Create WebApp

3.       Once webapp created in azure now I will publish our “To-Do” application from my visual studio with help of webapp publisher as below


  This application URL will be  https://djblogstodo.azurewebsites.net/

  GitHub Code Repository: https://github.com/deepakjoshi-info/Azure-DJBlogToDo

This is the screenshot of the application

4.       Now our webapp created, we can directly add WebJob from azure portal and select our WebJob program file like below screenshot


5.    We can also directly publish WebJob from visual studio 2019 with help of  WebApp publisher. Open visual studio 2019 and create “WebJob” project

      


6.       Click on next button and given the project name to  WebJobs

7.       WebJob will start from “Main” method. Where we will create “JobHost” instance and call the method “ProcessContinuouslyMethod”.

    GitHub:  https://github.com/deepakjoshi-info/Azure-WebJobs/blob/master/WebJobs/Program.cs

8.       We will create one method inside WebJobs project for update the task status. If task is active for past date it will just update it as “Pending” in SQL database.

ProcessContinuouslyMethod: Other WebJob method will continually run daily. You can change it based on your requirement. We need to call this method directly from “JobHost”.

host. CallAsync(typeof (Functions).GetMethod("ProcessContinuouslyMethod"));

GitHub:  https://github.com/deepakjoshi-info/Azure-WebJobs/blob/master/WebJobs/Functions.cs


9.       It is very hard to diagnose WebJobs and Asynchronous jobs because very nature of execution. To diagnose the execution of Jobs we need to enable logging. WebJob needs two Azure storage account connection strings to be configured

a.       AzureWebJobsDashboard: This storage account is primarily used by Azure WebJob SDK to store logs from the WebJobs Dashboard. This connection string is optional and is required only if you are planning to use the dashboard for monitoring WebJobs.

The WebJob runtime creates two containers under this storage account with the names ‘azure-webjobs-dashboard’ and ‘azure-jobs-host-archive’. The azure-webJobs-dashboard container is used by the WebJob dashboard to store host and execution endpoint (function) details. Azure-jobs-host-archive is used as an archive for execution logs.

 

b.      AzureWebJobsStorage: AzureWebJobsStorage should point to a storage account which will be primarily used for logging. WebJob runtime creates two containers in this storage account with the names ‘azure-jobs-host-output’ and ‘azure-webjobs-host’. If you point AzureWebJobsDashboard and AzureWebJobsStorage at two different storage accounts, you will notice that these two containers are duplicated in both the storage accounts.

We can use only one storage account for both the purpose. We are added our storage account into app.config file.

You can see these storage account with help of Microsoft Azure Storage. It will look like as below


10.       We need to add three connection strings in “app.config”. Two for storage account and one for SQL database as below


11.   We will use same publisher profile for “WebJob” which we have used to publish “djblogstodo” web app as below.

 


GitHub Code Repository: https://github.com/deepakjoshi-info/Azure-WebJobs

12.   Once web job published in azure. You can see the WebJobs under web app in azure portal as below


 13.   We can see the WebJob Logs in azure portal. It will look like as below
  

You can test the application by creating new task and set the date or just update the pending status task. if date passed it will make that task as pending. Currently I have set time interval for task scheduler is 1 sec. It will update every 1 second. 

URL: https://djblogstodo.azurewebsites.net/

Hope it will help you to create Web Job in azure. Please let me know, if you any suggestions and feedback.

Keep sharing keep learning. Cheers  

1 comment:

  1. These 3D printing recordsdata do not symbolize the texture, colour, or another model attributes of the file. This is a search engine that will find the related stl file for you by crawling all over the the} web and bringing you the best results for completely free STL recordsdata, along with various different fashions as well. Each and each certainly one of Cults3D’s fashions are 3D printable assured. Their work precision machining is centered round connecting the assorted 3D print recordsdata makers with the people who actually need to put their 3D printers via their paces.

    ReplyDelete