簡體   English   中英

如何獲取氣流 dag 運行的 JobID?

[英]How to get the JobID for the airflow dag runs?

當我們執行 dagrun 時,在 Airflow UI 上的“圖表視圖”中,我們會獲得每個作業運行的詳細信息。

JobID 類似於 "scheduled__2017-04-11T10:47:00"

我需要這個 JobID 來跟蹤和創建日志,我在其中維護每個任務/dagrun 花費的時間。

所以我的問題是如何在正在運行的同一個 dag 中獲取 JobID

謝謝,車坦

該值實際上稱為run_id ,可以通過上下文或宏訪問。

在 python 運算符中,這是通過上下文訪問的,而在 bash 運算符中,這是通過bash_command字段上的 jinja 模板訪問的。

有關宏中可用內容的更多信息:

https://airflow.apache.org/docs/stable/macros.html

更多關於 jinja 的信息:

https://airflow.apache.org/docs/stable/concepts.html#jinja-templating

from airflow.models import DAG
from datetime import datetime
from airflow.operators.bash_operator import BashOperator
from airflow.operators.python_operator import PythonOperator


dag = DAG(
    dag_id='run_id',
    schedule_interval=None,
    start_date=datetime(2017, 2, 26)
)

def my_func(**kwargs):
    context = kwargs
    print(context['dag_run'].run_id)

t1 = PythonOperator(
    task_id='python_run_id',
    python_callable=my_func,
    provide_context=True,
    dag=dag
    )

t2 = BashOperator(
    task_id='bash_run_id',
    bash_command='echo {{run_id}}',
    dag=dag)

t1.set_downstream(t2)

以此 dag 為例,檢查每個操作員的日志,您應該看到日志中打印了run_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM