Airflow Deferrable Job Sensor DAG

Prerequisites

  • apache-airflow-providers-imednet installed in an Apache Airflow 3.2+ environment with a running triggerer service

Environment variables

Configure the imednet_default connection in Apache Airflow (or set AIRFLOW_CONN_IMEDNET_DEFAULT):

export AIRFLOW_CONN_IMEDNET_DEFAULT="https://<api-key>:<security-key>@your.imednet.com"

Description

Monitor an asynchronous iMednet Job in Apache Airflow using ImednetJobSensor with deferrable=True, releasing the Airflow slot to ImednetJobTrigger until the Job reaches a terminal state.

See Airflow Integration for deferrable sensor configuration.

"""Example DAG demonstrating :class:`ImednetJobSensor` to monitor an iMednet job.

This sensor polls iMednet until the specified job reaches a terminal state.
When ``deferrable=True``, the task yields its worker slot to the Airflow
triggerer (:class:`ImednetJobTrigger`) and polls asynchronously via
:class:`~imednet.utils.job_poller.AsyncJobPoller`.

Configuration is similar to ``ImednetExportOperator``:
- Credentials are read from the Airflow connection ``imednet_default`` (or a
  custom ``imednet_conn_id``) using the login/password or ``extra`` fields.
  Environment variables ``IMEDNET_API_KEY`` and ``IMEDNET_SECURITY_KEY`` may be
  used as fallbacks.
- ``base_url`` can be provided in ``extra`` or via ``IMEDNET_BASE_URL`` when
  targeting a non-default environment.

Update ``STUDY_KEY`` and ``BATCH_ID`` with real values before running.
"""

from datetime import datetime

from airflow.sdk import DAG

from apache_airflow_providers_imednet import ImednetJobSensor

default_args = {"start_date": datetime(2024, 1, 1)}

with DAG(
    dag_id="imednet_job_sensor_example",
    schedule=None,
    default_args=default_args,
    catchup=False,
) as dag:
    wait_for_job = ImednetJobSensor(
        task_id="wait_for_job",
        study_key="STUDY_KEY",
        batch_id="BATCH_ID",
        poke_interval=60,
        deferrable=True,
    )