Skip to content

Latest commit

 

History

History
162 lines (116 loc) · 6 KB

jobs-get-started-cli.md

File metadata and controls

162 lines (116 loc) · 6 KB
title description services author ms.service ms.custom ms.topic ms.date ms.author zone_pivot_groups
Create a job with Azure Container Apps
Learn to create an on-demand or scheduled job in Azure Container Apps
container-apps
craigshoemaker
container-apps
build-2023, devx-track-azurecli
quickstart
08/17/2023
cshoe
container-apps-job-types

Create a job with Azure Container Apps

Azure Container Apps jobs allow you to run containerized tasks that execute for a finite duration and exit. You can trigger a job manually, schedule their execution, or trigger their execution based on events.

Jobs are best suited to for tasks such as data processing, machine learning, resource cleanup, or any scenario that requires on-demand processing.

In this quickstart, you create a manual or scheduled job. To learn how to create an event-driven job, see Deploy an event-driven job with Azure Container Apps.

[!INCLUDE container-apps-create-cli-steps-jobs.md]

::: zone pivot="container-apps-job-manual"

Create and run a manual job

To use manual jobs, you first create a job with trigger type Manual and then start an execution. You can start multiple executions of the same job and multiple job executions can run concurrently.

  1. Create a job in the Container Apps environment using the following command.

    az containerapp job create \
        --name "$JOB_NAME" --resource-group "$RESOURCE_GROUP"  --environment "$ENVIRONMENT" \
        --trigger-type "Manual" \
        --replica-timeout 1800 \
        --image "mcr.microsoft.com/k8se/quickstart-jobs:latest" \
        --cpu "0.25" --memory "0.5Gi"
    

    Manual jobs don't execute automatically. You must start an execution of the job.

  2. Start an execution of the job using the following command.

    az containerapp job start \
        --name "$JOB_NAME" \
        --resource-group "$RESOURCE_GROUP"
    

    The command returns details of the job execution, including its name.

::: zone-end

::: zone pivot="container-apps-job-scheduled"

Create and run a scheduled job

To use scheduled jobs, you create a job with trigger type Schedule and a cron expression that defines the schedule.

Create a job in the Container Apps environment that starts every minute using the following command.

az containerapp job create \
    --name "$JOB_NAME" --resource-group "$RESOURCE_GROUP"  --environment "$ENVIRONMENT" \
    --trigger-type "Schedule" \
    --replica-timeout 1800 \
    --image "mcr.microsoft.com/k8se/quickstart-jobs:latest" \
    --cpu "0.25" --memory "0.5Gi" \
    --cron-expression "*/1 * * * *"

Job executions start automatically based on the schedule.

Container Apps jobs use cron expressions to define schedules. It supports the standard cron expression format with five fields for minute, hour, day of month, month, and day of week.

::: zone-end

List recent job execution history

Container Apps jobs maintain a history of recent executions. You can list the executions of a job.

az containerapp job execution list \
    --name "$JOB_NAME" \
    --resource-group "$RESOURCE_GROUP" \
    --output table \
    --query '[].{Status: properties.status, Name: name, StartTime: properties.startTime}'

Executions of scheduled jobs appear in the list as they run.

Status     Name            StartTime
---------  --------------  -------------------------
Succeeded  my-job-jvsgub6  2023-05-08T21:21:45+00:00

Query job execution logs

Job executions output logs to the logging provider that you configured for the Container Apps environment. By default, logs are stored in Azure Log Analytics.

  1. Save the Log Analytics workspace ID for the Container Apps environment to a variable.

    LOG_ANALYTICS_WORKSPACE_ID=`az containerapp env show \
        --name "$ENVIRONMENT" \
        --resource-group "$RESOURCE_GROUP" \
        --query "properties.appLogsConfiguration.logAnalyticsConfiguration.customerId" \
        --output tsv`
    
  2. Save the name of the most recent job execution to a variable.

    JOB_EXECUTION_NAME=`az containerapp job execution list \
        --name "$JOB_NAME" \
        --resource-group "$RESOURCE_GROUP" \
        --query "[0].name" \
        --output tsv`
    
  3. Run a query against Log Analytics for the job execution using the following command.

    az monitor log-analytics query \
        --workspace "$LOG_ANALYTICS_WORKSPACE_ID" \
        --analytics-query "ContainerAppConsoleLogs_CL | where ContainerGroupName_s startswith '$JOB_EXECUTION_NAME' | order by _timestamp_d asc" \
        --query "[].Log_s"
    

    [!NOTE] Until the ContainerAppConsoleLogs_CL table is ready, the command returns no results or with an error: BadArgumentError: The request had some invalid properties. Wait a few minutes and run the command again.

    The following output is an example of the logs printed by the job execution.

    [
        "2023/04/24 18:38:28 This is a sample application that demonstrates how to use Azure Container Apps jobs",
        "2023/04/24 18:38:28 Starting processing...",
        "2023/04/24 18:38:33 Finished processing. Shutting down!"
    ]

Clean up resources

If you're not going to continue to use this application, run the following command to delete the resource group along with all the resources created in this quickstart.

Caution

The following command deletes the specified resource group and all resources contained within it. If resources outside the scope of this quickstart exist in the specified resource group, they will also be deleted.

az group delete --name "$RESOURCE_GROUP"

Tip

Having issues? Let us know on GitHub by opening an issue in the Azure Container Apps repo.

Next steps

[!div class="nextstepaction"] Container Apps jobs