Skip to main content

prefect_dbt.cloud.jobs

Module containing tasks and flows for interacting with dbt Cloud jobs

Functions

get_dbt_cloud_job_info

A task to retrieve information about a dbt Cloud job. Args:
  • dbt_cloud_credentials: Credentials for authenticating with dbt Cloud.
  • job_id: The ID of the job to get.
Returns:
  • The job data returned by the dbt Cloud administrative API.

create_dbt_cloud_job

A task to create a new dbt Cloud job. Args:
  • dbt_cloud_credentials: Credentials for authenticating with dbt Cloud.
  • project_id: The ID of the project to create the job in.
  • environment_id: The ID of the environment for the job.
  • name: The name of the job.
  • execute_steps: List of dbt commands to execute (e.g. [“dbt run”, “dbt test”]). Defaults to [“dbt build”].
  • **kwargs: Additional job configuration options.
Returns:
  • The job data returned by the dbt Cloud administrative API.

delete_dbt_cloud_job

A task to delete a dbt Cloud job. Args:
  • dbt_cloud_credentials: Credentials for authenticating with dbt Cloud.
  • job_id: The ID of the job to delete.

trigger_dbt_cloud_job_run

A task to trigger a dbt Cloud job run. Args:
  • dbt_cloud_credentials: Credentials for authenticating with dbt Cloud.
  • job_id: The ID of the job to trigger.
  • options: An optional TriggerJobRunOptions instance to specify overrides for the triggered job run.
Returns:
  • The run data returned from the dbt Cloud administrative API.
Examples: Trigger a dbt Cloud job run:
Trigger a dbt Cloud job run with overrides:

get_run_id

Task that extracts the run ID from a trigger job run API response, This task is mainly used to maintain dependency tracking between the trigger_dbt_cloud_job_run task and downstream tasks/flows that use the run ID. Args:
  • obj: The JSON body from the trigger job run response.

trigger_dbt_cloud_job_run_and_wait_for_completion

Flow that triggers a job run and waits for the triggered run to complete. Args:
  • dbt_cloud_credentials: Credentials for authenticating with dbt Cloud.
  • job_id: The ID of the job to trigger.
  • trigger_job_run_options: An optional TriggerJobRunOptions instance to specify overrides for the triggered job run.
  • max_wait_seconds: Maximum number of seconds to wait for job to complete
  • poll_frequency_seconds: Number of seconds to wait in between checks for run completion.
  • retry_filtered_models_attempts: Number of times to retry models selected by retry_status_filters.
Raises:
  • DbtCloudJobRunCancelled: The triggered dbt Cloud job run was cancelled.
  • DbtCloudJobRunFailed: The triggered dbt Cloud job run failed.
  • RuntimeError: The triggered dbt Cloud job run ended in an unexpected state.
Returns:
  • The run data returned by the dbt Cloud administrative API.
Examples: Trigger a dbt Cloud job and wait for completion as a stand alone flow:
Trigger a dbt Cloud job and wait for completion as a sub-flow:
Trigger a dbt Cloud job with overrides:

retry_dbt_cloud_job_run_subset_and_wait_for_completion

Flow that retrys a subset of dbt Cloud job run, filtered by select statuses, and waits for the triggered retry to complete. Args:
  • dbt_cloud_credentials: Credentials for authenticating with dbt Cloud.
  • trigger_job_run_options: An optional TriggerJobRunOptions instance to specify overrides for the triggered job run.
  • max_wait_seconds: Maximum number of seconds to wait for job to complete
  • poll_frequency_seconds: Number of seconds to wait in between checks for run completion.
  • run_id: The ID of the job run to retry.
Raises:
  • ValueError: If trigger_job_run_options.steps_override is set by the user.
Returns:
  • The run data returned by the dbt Cloud administrative API.
Examples: Retry a subset of models in a dbt Cloud job run and wait for completion:

run_dbt_cloud_job

Flow that triggers and waits for a dbt Cloud job run, retrying a subset of failed nodes if necessary. Args:
  • dbt_cloud_job: Block that holds the information and methods to interact with a dbt Cloud job.
  • targeted_retries: The number of times to retry failed steps.
  • create_assets: Whether to create Prefect asset materializations for successfully executed dbt models, seeds, and snapshots.
Examples:

Classes

DbtCloudJobRun

Class that holds the information and methods to interact with the resulting run of a dbt Cloud job. Methods:

dbt_cloud_credentials

fetch_result

Gets the results from the job run. Since the results may not be ready, use wait_for_completion before calling this method. Args:
  • step: The index of the step in the run to query for artifacts. The first step in the run has the index 1. If the step parameter is omitted, then this method will return the artifacts compiled for the last step in the run.

get_run

Makes a request to the dbt Cloud API to get the run data. Returns:
  • The run data.

get_run_artifacts

Get an artifact generated for a completed run. Args:
  • path: The relative path to the run artifact.
  • step: The index of the step in the run to query for artifacts. The first step in the run has the index 1. If the step parameter is omitted, then this method will return the artifacts compiled for the last step in the run.
Returns:
  • The contents of the requested manifest. Returns a Dict if the requested artifact is a JSON file and a str otherwise.

get_status_code

Makes a request to the dbt Cloud API to get the run status. Returns:
  • The run status code.

retry_failed_steps

Retries steps that did not complete successfully in a run. Returns:
  • A representation of the dbt Cloud job run.

wait_for_completion

Waits for the job run to reach a terminal state.

DbtCloudJob

Block that holds the information and methods to interact with a dbt Cloud job. Attributes:
  • dbt_cloud_credentials: The credentials to use to authenticate with dbt Cloud.
  • job_id: The id of the dbt Cloud job.
  • timeout_seconds: The number of seconds to wait for the job to complete.
  • interval_seconds: The number of seconds to wait between polling for job completion.
  • trigger_job_run_options: The options to use when triggering a job run.
Examples: Load a configured dbt Cloud job block.
Triggers a dbt Cloud job, waits for completion, and fetches the results.
Methods:

get_job

Retrieve information about a dbt Cloud job. Args:
  • order_by: The field to order the results by.
Returns:
  • The job data.

trigger

Triggers a dbt Cloud job. Returns:
  • A representation of the dbt Cloud job run.