簡體   English   中英

GCP 中的 Composer (Airflow) DAG RunID 沖突

[英]Composer (Airflow) DAG RunID conflict in GCP

我們有一個基於雲存儲的雲 function。 一旦文件加載到存儲桶中,此雲 function 將觸發。 加載文件時,function 將調用/觸發 airflow DAG。 此 DAG 將處理該文件。

問題是,當多個文件在一秒鍾內同時放置時,function 調用失敗並出現以下錯誤,

b'{"error":"運行 id manual__2020-07-31T17:48:15+00:00 已存在 dag id pl_imaoc_trigger_dag"}\n'

為了解決這個問題,我們將 run_id 作為“run_id”傳遞:“IMAOC_31072020201842766625”,以毫秒為單位的日期。

代碼:

dag_name = environ_vars['imaoc_meta_dag']
    webserver_url = (
        webserver_id
        + '/api/experimental/dags/'
        + dag_name
        + '/dag_runs'
    )

    print('webserver_url: {}'.format(webserver_url))
    data['run_id'] = _datetime.datetime.now().strftime(**"IMAOC_%d%m%Y%H%M%S%f"**)
    resp = map_iap_request(webserver_url,client_id,method = 'POST',json = data)
    print('response text:{}'.format(resp))

但仍然沒有解決,AIRFLOW_CTX_DAG_RUN_ID 以“manual__2020-07-31T20:18:43+00:00”格式出現......

如果文件在同一秒出現,不知道該怎么做才能消除此沖突並觸發 DAG。

請使用下面的代碼它工作

client_id = os.getenv("CLIENT_ID")
# This should be part of your webserver's URL:
# {tenant-project-id}.appspot.com
webserver_id = os.getenv("TENANT_PROJECT")
# The name of the DAG you wish to trigger
dag_name = os.getenv("DAG_NAME")
webserver_url = (
    'https://'
    + webserver_id
    + '.appspot.com/api/experimental/dags/'
    + dag_name
    + '/dag_runs'
)
# Make a POST request to IAP which then Triggers the DAG
run_id = datetime.utcnow().strftime('alpaca_%Y-%m-%dT%H:%M:%S.%f')

conf = {"conf": data}
print(f"JSON body = {conf}")

make_iap_request(
    webserver_url, client_id, method='POST', json={"conf": data, "run_id": run_id, "replace_microseconds": False})

通過在 conf 文件中添加"replace_microseconds": False ,上述答案對我有用,如下所示

run_id = 'trig__'+datetime.datetime.utcnow().isoformat()
conf['replace_microseconds'] = False
response = requests.post(URL, headers=Header, json={"conf": conf, "run_id": run_id})

暫無
暫無

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

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