也许有人可以告诉我这里我做错了什么。我在Airflow中有一个运行命令的任务,在日志中我收到此错误:
[2018-05-30 11:22:43,814] {models.py:1428} INFO - Executing <Task(PythonOperator): computer_unload_and_load> on 2018-05-30 15:22:41.595535
[2018-05-30 11:22:43,814] {base_task_runner.py:115} INFO - Running: ['bash', '-c', 'airflow run copy_kiosk_status computer_unload_and_load 2018-05-30T15:22:41.595535 --job_id 23 --raw -sd DAGS_FOLDER/copy_poc.py']
[2018-05-30 11:22:44,367] {base_task_runner.py:98} INFO - Subtask: [2018-05-30 11:22:44,367] {__init__.py:45} INFO - Using executor SequentialExecutor
[2018-05-30 11:22:44,412] {base_task_runner.py:98} INFO - Subtask: [2018-05-30 11:22:44,412] {models.py:189} INFO - Filling up the DagBag from /other/airflow/dags/copy_kiosk.py
[2018-05-30 11:22:44,570] {cli.py:374} INFO - Running on host [redacted]
[2018-05-30 11:22:46,967] {base_task_runner.py:98} INFO - Subtask: ERROR: schema "dw2" does not exist
[2018-05-30 11:22:46,969] {base_task_runner.py:98} INFO - Subtask: [2018-05-30 11:22:46,968] {python_operator.py:90} INFO - Done. Returned value was: None
这是任务:
computer_load_task = PythonOperator(
task_id='computer_unload_and_load',
python_callable=unload_and_load.unload_and_load_table,
op_args=(source_host, source_db, "public", "computer", "computer_unload_task")
dag=dag
)
这是它正在调用的函数:
def load_table(host, db, schema, table, unload_task_id=False, file_path=False, **kwargs):
""" load a csv file into a table
if no file_path is given, uses XCOM to get the file_name returned by the unload task"""
try:
if not file_path:
file_path = kwargs['ti'].xcom_pull(task_ids=unload_task_id)
load_cmd = "\copy {}.{} FROM {} WITH (FORMAT CSV, NULL '^', HEADER)".format(schema, table, file_path)
command = [ "psql",
"-U", "root",
"-h", host,
"-d", db,
"-c", load_cmd
]
subprocess.run(command)
except:
raise
显然我知道如何修复错误(我在那里有错误的架构),但我想知道为什么任务在Airflow中成功而不是失败?我一定错过了一些明显的东西。
答案 0 :(得分:1)
你的代码示例看起来很好 - 但有一件事。在此代码示例中,您的任务中缺少provide_context=True
。
除此之外,我认为这与子进程处理错误有关。您可以尝试设置属性check=True
,以便在出现问题时子进程将抛出异常。
或者,您可以检查子进程check_returncode()
,如果它不为零,则抛出您自己的异常。
以下是子流程的Python 3文档:https://docs.python.org/3/library/subprocess.html
要单独尝试异常处理,创建一个任务可能是一个好主意,如果不起作用,则只需raise
异常。