Azure WebJobs

Azure WebJobs are the tasks, which run in the background, which are associated with the Webapp like housekeeping, automated tasks, managing uploaded files or cleaning up the databases. We can define the task as an Application or the script of .cmd, .bat, .ps1, .python or .nodejs format. The WebJobs, which we define with a WebApp are also located on the same virtual machine of the Web app and can be scaled up and down along with the Web app. On scaling, the job will also be copied to every instance, which you create for it. The Web Jobs can run as on-demand, continuous and scheduled; which can be one-time or recurring. The WebJob is charged every time it runs. 

Now, let us see how to work with WebJobs. I have already created a Web app in the previous article. You can see WebJobs section in the dashboard of the Web app.

 

There is an option to add a new WebJob in the form of a script but as the portal allows only zip files, we have to zip the script into a folder and add the zipped folder.

I have written a script called timer in Powershell and zipped the file into folder called timer. The script is simple to follow, which will just get the current date and will output into file called timer.html in the directory given above.

Get-Date | Out-File D:\home\site\wwwroot\timer.html

This is the path, where the Web app is located. You can check the same by just visiting the console Window from the app Service dashboard.

On the portal while clicking Add it will open the Window given below to name the WebJob and for browsing the zip file. I have named it timer and added the zip file.

 

Now, it is asking me whether I want to run it continuously or triggered. I am choosing triggered, as I don’t want to run it continuously. I am selecting the triggers to be scheduled and it is asking for the cron expression for the schedules. A cron expression is a string, which consists of six or seven subexpressions (fields), which describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with the various combinations of the allowed characters for that field. 

I want to run the job every minute. Thus, the cron expression for the same is 0 0/1 * * * *.

You can see the WebJob is created and is ready to run. Select the WebJob and click Run.

 

Now, our WebJob is running in the background and you can check it, using http://your-domain-name.azurewebsites.net/timer.html