Let’s think for a minute about how we run a background task on on-premise systems. Two things that certainly came to your mind are SQL Jobs and Windows Services. Unfortunately, if you think about the Azure as a “Platform as a Service” then I have a bad news for you because SQL Jobs and Windows Services aren’t supported under this setup.
You could run the same setup that you have on your on-premise system on Azure as “Infrastructure as a Service” which are VM’s but then this isn’t some improvement because what you basically do is “cloning” your on-premise VM to Azure which can be an additional expense. Also, there are Cloud Applications (Cloud Services) where you can run anything in an Azure-managed VM under Worker Roles. This will do the job but you have a complex configuration to run even the simplest background job. Clearly, we have a challenge in front of us!
How can I run time or resource consuming tasks in a web site background?
Here are some scenarios where you could use Azure WebJobs:
- CPU-intensive work (eg. Image processing)
- Queue processing with Azure Storage Queue
- File maintenance, loading files or cleaning up log files
- Replacement for SQL Jobs for executing long-running tasks
- Sending e-mails with SendGrid or Office365
How can I start building Azure webJobs
Now that we have everything let’s create a new WebJob by creating a new Cloud Template Azure WebJob:
Now that we have our template let’s change the Main method so that we could run continuous running WebJob by using Timers. First we need to add Microsoft.Azure.WebJobs.Extensions through NuGet Package Manager. This will add additional triggers, binders to WebJobs. Now let’s add UseTimers method in JobHostConfiguration and call our function on the JobHost.
This is used for two reasons:
- Azure Storage is a data component extension to your Azure Storage for triggered operations with the queue, blobs or tables (NoSQL)
- WebJobs is using Storage Account for storing logs.
Now that we add the keys we can create our TimerFunction method inside of a Functions class:
Please notice that the name of the class and method is used in the Main method for calling the method that will be executed in continuously running webjob: host.CallAsync(typeof(Functions).GetMethod(“TimerFunction”));
Also in the implementation of the WebJob method, the [NoAutomaticTrigger] attribute is used which marks the function to be run continuously and the code for a continuous job is written to run in an endless loop (Continuously running WebJobs needs to be written in an endless loop). Please keep in mind the following when you develop this type of WebJobs:
A web app can time out after 20 minutes of inactivity. Only requests to the scm (deployment) site or to the web app’s pages in the portal reset the timer. Requests to the actual site don’t reset the timer. If your app runs continuous or scheduled WebJobs, enable Always On to ensure that the WebJobs run reliably. This feature is available only in the Basic, Standard, and Premium pricing tiers.
And that is basically all. After you fill the logic in the ”Your code goes here…” line, your WebJob is ready. Also, since you have injected TextWriter instance and use this for logging you don’t have to worry about logging because Azure will take care of that. Let’s deploy the WebJob on Azure. When you create a new App Service you on Azure you can download publish profile, also you can create a new App Service plan during the publishing process. How to publish? Easy, Just Right-click your WebJob project and select “Publish as Azure WebJob…”
The new WebJob appears on the WebJobs page from where you can stop, restart, delete, set the properties or view the log of all your WebJobs.
Remember the injected TextWriter object? When you select the WebJob for which you want to see the execution log and select the Logs button:
You will be redirected to WebJobs details page where you will see all executions of the WebJob and you can examine each one individually:
On the WebJob run details page, you can select Toggle Output button and you will see the logs that are generated by using TextWriter object.
Our continuously running WebJob is up and running! In the next blog post, we will examine on-demand (triggered) WebJob by using Azure Queue Storage service.