Azure Container App Jobs
Azure Container Apps jobs enable you to run containerized tasks that execute for a finite duration and exit. You can use jobs to perform tasks such as data processing, machine learning, or any scenario where on-demand processing is required. Unlike traditional Azure Container Apps that run continuously, Container App Jobs are designed for batch processing, scheduled tasks, and event-driven workloads.
Aspire provides support for Azure Container App Jobs through the PublishAsAzureContainerAppJob extension method, allowing you to deploy your .NET projects, containers, and executables as jobs in Azure Container Apps.
Prerequisites
- Azure subscription.
- Aspire project.
- Understanding of Azure Container Apps jobs.
Supported resource types
Aspire allows you to publish the following resource types as Azure Container App Jobs:
ContainerResource: Represents a specified container.ExecutableResource: Represents a specified executable process.ProjectResource: Represents a specified .NET project.
Publishing as Azure Container App Jobs
To publish resources as Azure Container App Jobs, use the following APIs:
PublishAsAzureContainerAppJob<T>(IResourceBuilder<T>);PublishAsAzureContainerAppJob<T>(IResourceBuilder<T>, Action<AzureResourceInfrastructure, ContainerAppJob>);PublishAsScheduledAzureContainerAppJob<T>(IResourceBuilder<T>, string, Action<AzureResourceInfrastructure, ContainerAppJob>);
Basic usage
The following example demonstrates how to configure a .NET project as an Azure Container App Job with a scheduled trigger:
var builder = DistributedApplication.CreateBuilder();
builder.AddProject<Projects.DataProcessor>("data-processor")
.PublishAsScheduledAzureContainerAppJob("0 0 * * *"); // Every day at midnight
This code:
- Creates a new distributed application builder.
- Adds a project named
data-processorto the builder. - Calls
PublishAsScheduledAzureContainerAppJobto configure the project as a Container App Job. - Sets the trigger type to
Schedulewith a cron expression to run daily at midnight.
Job trigger types
Azure Container App Jobs support different trigger types:
Manual trigger
Use manual triggers for on-demand job execution:
builder.AddProject<Projects.ManualTask>("manual-task")
.PublishAsAzureContainerAppJob((_, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Manual;
});
Schedule trigger
Use schedule triggers for time-based job execution:
builder.AddProject<Projects.ScheduledTask>("scheduled-task")
.PublishAsAzureContainerAppJob((_, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Schedule;
job.Configuration.ScheduleTriggerConfig.CronExpression = "0 */6 * * *"; // Every 6 hours
job.Configuration.ScheduleTriggerConfig.Parallelism = 1;
job.Configuration.ScheduleTriggerConfig.ReplicaCompletionCount = 1;
});
Event trigger
Use event triggers for reactive job execution based on external events:
builder.AddProject<Projects.EventDrivenTask>("event-task")
.PublishAsAzureContainerAppJob((_, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Event;
job.Configuration.EventTriggerConfig.Scale.MinExecutions = 1;
job.Configuration.EventTriggerConfig.Scale.MaxExecutions = 10;
job.Configuration.EventTriggerConfig.Parallelism = 1;
job.Configuration.EventTriggerConfig.ReplicaCompletionCount = 1;
});
Advanced configuration
Setting resource requirements
Configure CPU and memory resources for your job:
builder.AddProject<Projects.ResourceIntensiveTask>("intensive-task")
.PublishAsAzureContainerAppJob((_, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Manual;
job.Template.Containers[0].Value!.Resources.Cpu = 1.0;
job.Template.Containers[0].Value!.Resources.Memory = "2Gi";
});
Environment variables
Add environment variables to your job:
var connectionString = builder.AddParameter("connectionString");
builder.AddProject<Projects.DatabaseTask>("db-task")
.PublishAsAzureContainerAppJob((infra, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Manual;
job.Template.Containers[0].Value!.Env.Add(new ContainerAppEnvironmentVariable
{
Name = "ConnectionString",
Value = connectionString.AsProvisioningParameter(infra)
});
});
Timeout and retry policy
Configure job execution timeout and retry behavior:
builder.AddProject<Projects.RetryableTask>("retryable-task")
.PublishAsAzureContainerAppJob((_, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Manual;
job.Configuration.ReplicaTimeout = 1800; // 30 minutes
job.Configuration.ReplicaRetryLimit = 3;
});
Container resources
You can also publish container resources as Azure Container App Jobs:
builder.AddContainer("batch-processor", "myregistry.azurecr.io/batch-processor:latest")
.PublishAsAzureContainerAppJob((_, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Schedule;
job.Configuration.ScheduleTriggerConfig.CronExpression = "0 2 * * 0"; // Weekly on Sunday at 2 AM
});
Executable resources
Executable resources can also be published as jobs:
builder.AddExecutable("data-script", "python", ".", "process_data.py")
.PublishAsAzureContainerAppJob((_, job) =>
{
job.Configuration.TriggerType = ContainerAppJobTriggerType.Manual;
});
Experimental feature notice
Important
Azure Container App Jobs support in Aspire is an experimental feature. The APIs are subject to change in future releases. You might encounter the diagnostic ASPIREAZURE002 when using these features. For more information, see ASPIREAZURE002.