Azure WebJobs Simplified: Job Creation and Management

Author

Reads 477

Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.
Credit: pexels.com, Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.

Azure WebJobs is a powerful tool for running background tasks in your Azure web app. It's like a personal assistant that helps you automate tasks, making your life easier.

To create a WebJob, you can use the Azure portal or the Azure CLI. Simply navigate to your Azure web app, click on the "Platform features" tab, and then click on "WebJobs" to get started.

WebJobs can be triggered by various events, such as file uploads, timer triggers, or API requests. This flexibility makes it easy to automate tasks that are specific to your web app's needs.

You can also manage your WebJobs by scaling them up or down, depending on your web app's requirements. This ensures that your WebJobs are running efficiently and effectively.

Job Creation

To create an Azure WebJob, you'll need to follow these steps. First, go to the App Service page of your App Service web app, API app, or mobile app in the Azure portal.

Credit: youtube.com, How To Create Azure Web Job

There are three types of WebJobs: continuous, triggered, and scheduled. Each type has its own settings and requirements.

To create a continuous WebJob, you'll need to fill in the Add WebJob settings, including a unique name, a .zip file containing your executable or script, and the type as Continuous. The Scale setting determines whether the program or script runs on all instances or one instance.

You can also create a triggered WebJob, which can be manually triggered or scheduled to run at a specific time. To create a manually triggered WebJob, fill in the Add WebJob settings, including a unique name, a .zip file containing your executable or script, and the type as Triggered. For manual triggers, select Manual as the trigger type.

To create a scheduled WebJob, fill in the Add WebJob settings, including a unique name, a .zip file containing your executable or script, and the type as Triggered. For scheduled triggers, select Scheduled as the trigger type and enter a CRON expression to define the schedule.

Credit: youtube.com, How to Create Web Jobs in Azure App Service

Here's a summary of the WebJob types and their settings:

Remember to refresh the WebJobs page after creating a new WebJob, as it may take a few moments to appear. You can also run a triggered WebJob manually by right-clicking on it and selecting the Run button.

Job Management

Job Management is a breeze with Azure WebJobs. You can manage the running state of individual WebJobs in the Azure portal by going to Settings > WebJobs, choosing the WebJob, and starting or stopping it.

You can also add an application setting named WEBJOBS_STOPPED with a value of 1 to stop all WebJobs running on your site. This is useful for preventing conflicting WebJobs from running in both staging and production slots.

Common WebJob statuses include running, stopped, and disabled. To disable triggered WebJobs, you can add an application setting named WEBJOBS_DISABLE_SCHEDULE with a value of 1.

Check this out: Azure Webjobs Storage

Job Types

There are two main types of WebJobs: triggered and continuous. Triggered WebJobs start based on a binding event, on a schedule, or when you trigger them manually (on demand). They run on a single instance that the web app runs on.

Credit: youtube.com, Job List Manager - Managing Job Types

Continuous WebJobs start immediately when the WebJob is created and run on all web app scaled instances by default. However, they can be configured to run as a single instance via settings.job.

Here's a summary of the main differences between triggered and continuous WebJobs:

In general, continuous WebJobs are suitable for long-running tasks that need to run continuously, while triggered WebJobs are better suited for tasks that only need to run occasionally or on demand.

Manage

Managing your WebJobs is a crucial part of ensuring they run smoothly and efficiently. You can manage the running state of individual WebJobs in the Azure portal by going to Settings > WebJobs, choosing the WebJob, and starting or stopping it.

You can also add an application setting named WEBJOBS_STOPPED with a value of 1 to stop all WebJobs running on your site, which is especially useful for preventing conflicting WebJobs from running in both staging and production slots.

Credit: youtube.com, Job Management App - Manage Jobs

To stop or restart a continuous WebJob, right-click the WebJob in the list and select the Stop or Run button, then confirm your selection. This is a simple yet effective way to manage your WebJobs.

To view and modify the password of the webhook that runs a WebJob, go to the WebJobs page in the Azure portal and choose the WebJob. You can then make any necessary changes to the password.

The following is a list of common WebJob statuses:

  1. The WebJob is running.
  2. The WebJob is stopped.
  3. The WebJob is disabled.
  4. The WebJob is not deployed.

Singleton Mode.Listener

Singleton Mode.Listener is a feature that ensures only one instance of your function runs at a time, even when there are multiple instances of the host web app. This is achieved through distributed locking.

To implement Singleton Mode.Listener, you need to set specific settings for different trigger types. For QueueTrigger, set JobHostConfiguration.Queues.BatchSize to 1. For ServiceBusTrigger, set ServiceBusConfiguration.MessageOptions.MaxConcurrentCalls to 1. For FileTrigger, set FileProcessor.MaxDegreeOfParallelism to 1.

These settings ensure that your function runs as a singleton on a single instance. However, to ensure that only a single instance of the function is running when the web app scales out to multiple instances, you need to apply a listener-level singleton lock on the function. This is done by adding the Singleton attribute with Mode set to SingletonMode.Listener.

If three scaled-out instances all start at the same time, only one of the instances acquires the lock and only one listener starts. This ensures that your function runs as a singleton across multiple instances of the host web app.

Worth a look: Azure Static Web App

Queue

Credit: youtube.com, Queue and job management system

Queue triggers can persist messages and attempt to re-deliver them until they receive an acknowledgement of success, giving you an 'at least once' guarantee.

This means you can be sure you'll receive a notification, even if it's sent multiple times.

Azure Storage Queue messages can be posted from other applications or Azure services themselves by subscribing a storage queue to an Event Grid topic.

Event Grid does not work with the emulator, so you can only test it 'for real' in Azure.

After a configured number of failures and retries, the message will be delivered to a 'poison' queue, which is your final chance to log it or handle it in some other way.

The poison queue has the same name as your main queue but with "poison" appended to it, and is automatically set up for you.

You just need to handle the messages in the poison queue.

Timeout Attribute

The Timeout attribute is a powerful tool for managing job execution time. It cancels a function if it doesn't finish within a specified amount of time.

Credit: youtube.com, DDM 2019.05 What's new - Workflow Timeout Enhancement

You can apply the Timeout attribute at the class or method level, and it overrides global timeouts. The default value of "throwOnError" is "false".

If you set "throwOnError" to "true", the function invocation is terminated by throwing an exception when the timeout interval is exceeded. This can be useful for handling errors in a more robust way.

The Timeout attribute causes a function to be canceled after the specified time, in this case 15 seconds. This prevents the function from running indefinitely until it returns or throws an exception.

You can also specify a global timeout using JobHostConfiguration.FunctionTimeout. However, class-level or method-level timeouts take priority over global timeouts.

Expand your knowledge: Azure Functions Startup Class

Return

To return to the concept of running jobs, we need to understand how they can be added to our application. A WebJob is similar to a Function App, but it runs in the context of an existing application.

You can add WebJobs to your application by including the WebJobs SDK package in your ASP.NET app. This is a straightforward process that allows you to take advantage of the benefits of WebJobs.

Scheduling a Job

Credit: youtube.com, Azure Web Jobs | Azure Scheduled Web Job Complete Tutorial | Azure Send Email

You can schedule a WebJob to run at a specific time using a CRON expression. A CRON expression is a string that defines the schedule, such as 0 0/20 * * * *.

To schedule a WebJob, you can use the settings.job file, which is a JSON file that contains the scheduling settings. This file is created automatically when you publish a console app to Azure.

The settings.job file can contain settings such as the schedule, which is set using the schedule setting. For example, you can set the schedule to run every hour from 9 AM to 5 PM using the following settings.job file:

```

{

"schedule": "0 0/1 * * * *"

}

```

You can also set the schedule using a CRON expression in the Azure portal.

The default time zone used to run CRON expressions is Coordinated Universal Time (UTC). To have your CRON expression run based on another time zone, create an app setting for your function app named WEBSITE_TIME_ZONE.

Credit: youtube.com, Azure Web Job with Scheduler

Here's a summary of the supported settings in the settings.job file:

By using these settings, you can schedule your WebJob to run at a specific time and configure it to run reliably.

Deploy to App Service

To deploy a .NET Core WebJob to Azure App Service, you can use the same tooling as publishing an ASP.NET Core app. This process involves right-clicking the project in Solution Explorer and selecting Publish.

In the Publish dialog box, select Azure for Target, and then select Next. You'll then select Azure WebJobs for Specific target, and select Next again. Above App Service instances, select the plus (+) button to Create a new Azure WebJob.

To create a new WebJob, you'll need to provide hosting settings. Here are some suggested values for the settings:

Once you've provided the hosting settings, select Create to create a WebJob and related resources in Azure with these settings and deploy your project code. You can then select Finish to return to the Publish page.

Settings and Configuration

Credit: youtube.com, Azure Webjob aborted|Azure Webjobs Issues | Azure Web jobs tutorial | Webjobs in Azure Portal

In Azure WebJobs, you can configure various settings to control how your job runs. The settings.job file is a key part of this process, allowing you to set an execution schedule for your WebJob.

You can also configure settings using the JobHostConfiguration class, specifically through the UseDevelopmentSettings method. This enables development mode, which automatically changes certain settings to make local development more efficient.

Some of the settings that change in development mode include Tracing.ConsoleLevel, which is set to TraceLevel.Verbose to maximize log output, and Queues.MaxPollingInterval, which is set to a low value to ensure queue methods are triggered immediately.

Here are some of the key settings you can configure in Azure WebJobs:

These settings can be configured through the settings.job file or using the JobHostConfiguration class, depending on your specific needs.

Settings Job Reference

In WebJobs, you can configure various settings to control how your job runs. The `is_in_place` setting allows the WebJob to run directly in its working directory without being copied to a temporary folder.

Credit: youtube.com, Betatalks #64 - App Configuration References

The `is_singleton` setting is useful when you want to run a continuous job on a single instance, even when scaled out. This setting prevents multiple instances of the job from running simultaneously.

WebJobs also support a `schedule` setting, which enables you to run the job on a CRON-based schedule. This is particularly useful for tasks that need to run at specific intervals.

The `stopping_wait_time` setting gives you control over the shutdown behavior of your job. This setting is useful for ensuring a smooth transition when the job is stopped.

Here's a summary of the supported settings:

Scope Values

You can specify a scope expression/value on a singleton to serialize function invocations at a specific scope, allowing for some level of parallelism.

This approach can be particularly useful when dealing with multiple messages in the same region, as shown in the example where messages with region East are run serially.

The scope expression binds to the Region value of the incoming message, ensuring that all executions of the function at that specific region will be serialized.

Credit: youtube.com, 21 Configuration sets and Configuration settings

For instance, if a queue contains three messages in regions East, East, and West, the messages with region East will be run one after the other, while the message with region West can run in parallel.

To lock across functions, specify SingletonScope.Host and use a scope ID name that's the same across all functions that you don't want to run simultaneously.

Explore further: Azure East

Host Development Settings

Host development settings can greatly improve your local development experience. By enabling development mode, you can make local development more efficient.

Tracing.ConsoleLevel is automatically set to TraceLevel.Verbose in development mode, which maximizes log output. This is useful for debugging purposes.

Queues.MaxPollingInterval is also set to a low value, ensuring queue methods are triggered immediately. This is especially helpful when working on complex jobs.

Singleton.ListenerLockPeriod is set to 15 seconds in development mode, aiding in rapid iterative development. This setting is crucial for testing and refining your code.

To enable development mode, you can use the JobHostConfiguration class's UseDevelopmentSettings method. Alternatively, you can set a local environment variable named AzureWebJobsEnv with the value Development.

Disable Attribute

Computer server in data center room
Credit: pexels.com, Computer server in data center room

The Disable attribute is a powerful tool for controlling function triggers. It allows you to prevent a function from running based on a specific app setting.

You can declare the Disable attribute at the parameter, method, or class level. This gives you flexibility in how you implement it in your code.

The setting name can also contain binding expressions, which can be useful for more complex scenarios. This adds another layer of customization to the Disable attribute.

If the app setting Disable_TestJob has a value of 1 or True (case insensitive), the function won't run. This is a straightforward way to disable a function based on a setting.

In that case, the runtime creates a log message Function 'Functions.TestJob' is disabled. This provides a clear indication of why the function wasn't executed.

Frequently Asked Questions

What is the difference between WebJobs and Azure Functions?

The main difference between WebJobs and Azure Functions is that Azure Functions doesn't require you to manage infrastructure, making it ideal for small code snippets. This frees you up to focus on writing code, not worrying about the underlying infrastructure.

How to trigger an Azure WebJob?

To trigger an Azure WebJob, navigate to the App Service page in the Azure portal and select Add WebJob from the WebJobs page. This will allow you to create and run a new WebJob.

How do I know if WebJobs are running in Azure?

To check if WebJobs are running in Azure, you can use 3rd party tools like CloudMonix or Azure Monitor's App Insights Web Tests. These tools can provide real-time status updates, including current execution status, with the help of the Kudu Web Jobs API.

What is a WebJob in Azure?

A WebJob in Azure is a feature that enables running background tasks or scripts in your App Service, perfect for scheduled tasks, data processing, and maintenance. It's a convenient way to execute jobs without interrupting your app's main functionality.

Margarita Champlin

Writer

Margarita Champlin is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for simplifying complex topics, she has established herself as a go-to expert in the field of technology. Her writing has been featured in various publications, covering a range of topics, including Azure Monitoring.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.