Azure App Service is an integral component of the Microsoft Azure platform, providing a highly scalable and self-patching web hosting service. A lesser-known but incredibly useful feature of this service is Azure WebJobs. WebJobs are essentially programs or scripts that can be run in the context of an Azure App Service, which can be triggered to run on demand, continuously, or at a specified schedule. In this blog post, we’re going to dive into Azure WebJobs, their benefits, and how you can implement them in your Azure services.
What are Azure WebJobs?
Azure WebJobs were introduced to simplify the process of running background tasks in the context of an Azure App Service. These tasks could be anything from image processing, queue processing, RSS feed aggregation, to file maintenance and other tasks which need to occur in the background, independent of user interaction.
WebJobs support a variety of file types, including .cmd, .bat, .exe (using windows cmd), .ps1 (PowerShell), .sh (Bash), .php (PHP), .py (Python), .js (Node.js), and .jar (Java), providing developers the freedom to use the languages they’re most comfortable with.
But What About Azure Functions?
You may be familiar with Azure Functions and may be wondering why one would pick WebJobs over Azure Functions. While Azure functions is certainly very powerful and is a good candidate for all sorts of processing tasks, they do have their limitations, as well. One main issue with them is that it has predefined time-limits. In the consumption plan, an Azure Function can run up to ten minutes. In a premium plan, you can run a function for up to 30 minutes. If your workload needs more time, a WebJob could be a good alternative.
Types of Azure WebJobs
There are primarily two types of WebJobs in Azure – Continuous and Triggered.
- Continuous WebJobs: As the name suggests, these are always running in the background. They are ideal for tasks such as listening to queues or monitoring certain data sources. Continuous WebJobs keep running indefinitely until stopped or there’s an error. They start immediately when the WebJob is created and also when the App Service is restarted.
- Triggered WebJobs: These are jobs that run at specified intervals or schedules, much like cron jobs in Linux or task scheduler in Windows. They are executed either manually or on a schedule (using CRON expressions).
Implementing Azure WebJobs
Azure WebJobs are created within the context of an Azure App Service. You can develop WebJobs by using the Azure WebJobs SDK, which simplifies the coding and provides binding and trigger APIs. Here are the steps to create a simple WebJob.
- Create an App Service Plan.
- Create an App Service under the above plan.
- Under the App Service, create a WebJob. Give it a name, upload a .zip file containing your scripts or executable files, and set the type to either Continuous or Triggered.
- If you selected Triggered, set a schedule using CRON expressions.
- Once the WebJob is created, it will run according to its type and schedule.
Benefits of Azure WebJobs
There are many advantages of using Azure WebJobs:
- Easy Setup: WebJobs are created in the context of an App Service, so there’s no need for any additional hosting plan or service.
- Variety of Supported File Types: WebJobs support a wide variety of script types and executable files, allowing developers to use the programming language of their choice.
- Logging and Monitoring: WebJobs integrate with Azure Application Insights and Azure Monitor Logs, making it easy to monitor and troubleshoot WebJobs with the same systems you use for your other Azure services.
- Scalability: As a feature of Azure App Service, WebJobs can take advantage of the auto-scaling capabilities provided by Azure.
Closing Remarks
In conclusion, Azure WebJobs are a valuable tool when working with Azure App Services. They provide a straightforward, flexible, and powerful way to handle background tasks, allowing the main web app to remain responsive and focused on handling user requests. Whether you need to run tasks on a schedule or continuously, Azure WebJobs may just be the solution you need.
Stay tuned for a hands-on look at this powerful, useful service.