作为守护进程发布 运行 airflow scheduler

Issues running airflow scheduler as a daemon process

我有一个 EC2 实例 运行ning airflow 1.8.0 使用 LocalExecutor。根据文档,我预计以下两个命令之一会在守护进程模式下启动调度程序:

airflow scheduler --daemon --num_runs=20

airflow scheduler --daemon=True --num_runs=5

但事实并非如此。第一个命令似乎会起作用,但它只是 returns 在返回终端之前输出以下内容而不产生任何后台任务:

[2017-09-28 18:15:02,794] {__init__.py:57} INFO - Using executor LocalExecutor
[2017-09-28 18:15:03,064] {driver.py:120} INFO - Generating grammar tables from /usr/lib/python3.5/lib2to3/Grammar.txt
[2017-09-28 18:15:03,203] {driver.py:120} INFO - Generating grammar tables from /usr/lib/python3.5/lib2to3/PatternGrammar.txt

第二个命令产生错误:

airflow scheduler: error: argument -D/--daemon: ignored explicit argument 'True'

这很奇怪,因为根据 docs --daemon=True 应该是 airflow scheduler 调用的有效参数。

再深入一点,我发现了 this Whosebug post, where one of the responses recommends an implementation of systemd for handling the airflow scheduler as a background process according to the code available as this repo

我对脚本的轻微编辑改编作为以下要点发布。我正在使用带有 Ubuntu 16.04.3 的香草 m4.xlarge EC2 实例:

我从那里打电话:

sudo systemctl enable airflow-scheduler
sudo systemctl start airflow-scheduler

什么也没发生。虽然我在这个实例上有更复杂的 DAG 运行,但 I am using this dummy case 可以创建一个简单的测试,该测试还可以作为监听器让我知道调度程序何时按计划运行。

我一直在使用 journalctl -f 进行调试。以下是调度程序进程的几行输出。没有明显的问题,但我的任务没有执行,也没有为测试 DAG 生成任何日志来帮助我放大错误。问题出在这里吗?

Sep 28 18:39:30 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:30,965] {dag_processing.py:627} INFO - Started a process (PID: 21822) to generate tasks for /home/ubuntu/airflow/dags/scheduler_test_dag.py - logging into /home/ubuntu/airflow/logs/scheduler/2017-09-28/scheduler_test_dag.py.log
Sep 28 18:39:31 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:31,016] {jobs.py:1002} INFO - No tasks to send to the executor
Sep 28 18:39:31 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:31,020] {jobs.py:1440} INFO - Heartbeating the executor
Sep 28 18:39:32 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:32,022] {jobs.py:1404} INFO - Heartbeating the process manager
Sep 28 18:39:32 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:32,023] {jobs.py:1440} INFO - Heartbeating the executor
Sep 28 18:39:33 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:33,024] {jobs.py:1404} INFO - Heartbeating the process manager
Sep 28 18:39:33 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:33,025] {dag_processing.py:559} INFO - Processor for /home/ubuntu/airflow/dags/capone_dash_dag.py finished
Sep 28 18:39:33 ip-172-31-15-209 airflow[20603]: [2017-09-28 18:39:33,026] {dag_processing.py:559} INFO - Processor for /home/ubuntu/airflow/dags/scheduler_test_dag.py finished

当我手动 运行 airflow scheduler 时,一切正常。由于我的测试 DAG 的开始日期是 9 月 9 日,从那以后它每分钟都在回填,生成 运行ning 时间行情。但是,当我使用 systemd 到 运行 调度程序作为守护进程时,它完全安静,没有明显的错误源。

有什么想法吗?

文档可能已过时?

我通常按如下方式启动 Airflow

airflow kerberos -D
airflow scheduler -D
airflow webserver -D

这里是 airflow webeserver --help 输出(来自版本 1.8):

-D, --daemon Daemonize instead of running in the foreground

注意那里不可能有布尔标志。必须修复文档。

万一 airflow scheduler -D 失败的快速说明:

这在评论中有提及,但在这里似乎值得一提。当您 运行 您的气流调度程序时,它将创建文件 $AIRFLOW_HOME/airflow-scheduler.pid。如果您尝试重新 运行 气流调度程序守护进程,这几乎肯定会生成文件 $AIRFLOW_HOME/airflow-scheduler.err,它会告诉您 lockfile.AlreadyLocked: /home/ubuntu/airflow/airflow-scheduler.pid is already locked。如果您的调度程序守护程序确实无法使用并且您发现自己需要重新启动,请执行以下命令:

sudo rm $AIRFLOW_HOME airflow-scheduler.err  airflow-scheduler.pid
airflow scheduler -D 

这让我的日程安排回到正轨。

关于通过 systemd 启动任务:

当 运行 这种方式最初为空时,我遇到了 PATH 变量问题。也就是说,当你写入文件时 /etc/sysconfig/airflow:

PATH=/home/ubuntu/bin:/home/ubuntu/.local/bin:$PATH

你字面上写:

PATH=/home/ubuntu/bin:/home/ubuntu/.local/bin

因此,变量 PATH 不包含 /bin 这是 LocalExecutor 用于 运行 任务的 bash 实用程序。

所以我不明白为什么你没有在这个文件中指定 AIRFLOW_HOME。也就是Airflow查找配置文件的目录。