当任务名称和任务ID不匹配时,为什么气流抛出异常?

时间:2019-11-27 18:01:06

标签: python airflow

从气流文档和Internet上的不同站点中可以很明显地看出,使用操作员创建任务时,任务名称和task_id不需要匹配。例如,我正在使用以下代码:

from airflow import DAG
from airflow.utils import timezone
from airflow.operators.python_operator import PythonOperator
from pprint import pprint

default_args = {
    'owner':'me',
    'start_date': timezone.datetime(2019,1,1), 
    'provide_context': True
 }

dag = DAG(
    'etl', # dag_id
    default_args=default_args,
    schedule_interval=None,
    max_active_runs=2,
    catchup=False
)

def some_function(**context):
    start_time = default_args['start_date']
    print(start_time )

def another_function(**context):
    start_time = default_args['start_date']
    print(start_time )

with dag:
        some_task_name = PythonOperator(
            task_id='some_task_name_id',
            python_callable=some_function,
        )

        another_task_name = PythonOperator(
            task_id='another_task_name_id',
            python_callable=another_function,
        )

        some_task_name >> another_task_name

我正在使用以下命令进行测试:

气流测试etl_script some_task_name'args'

并出现以下错误:

Traceback (most recent call last):
  File "~/airflow/.venv/bin/airflow", line 32, in <module>
    args.func(args)
  File "~/airflow/.venv/lib/python3.6/site-packages/airflow/utils/cli.py", line 74, in wrapper
    return f(*args, **kwargs)
  File "~/airflow/.venv/lib/python3.6/site-packages/airflow/bin/cli.py", line 648, in test
    task = dag.get_task(task_id=args.task_id)
  File "~/airflow/.venv/lib/python3.6/site-packages/airflow/models/dag.py", line 1070, in get_task
    raise AirflowException("Task {task_id} not found".format(task_id=task_id))
airflow.exceptions.AirflowException: Task some_task_name not found

但是如果我将任务名称和ID设置为相同,它将起作用。为什么会这样呢?我觉得自己缺少了一些东西,但无法解决。谁能点燃一些灯光?

环境: Airflow版本:1.10.5,Ubuntu 18.04,Python 3.6

1 个答案:

答案 0 :(得分:0)

发生此错误并不是因为task_id和名称不同,而是因为我正在通过 test 命令使用任务名称而不是task_id。从服务器命令行测试dag任务时,命令布局为:

气流测试dag_id task_id日期

我想最好将任务名称和ID保持相同。