1

我是 Airflow 的新手,并创建了我的第一个 DAG。这是我的 DAG 代码。我希望 DAG 现在开始,然后每天运行一次。

from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime, timedelta

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime.now(),
    'email': ['aaaa@gmail.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
}

dag = DAG(
    'alamode', default_args=default_args, schedule_interval=timedelta(1))

create_command = "/home/ubuntu/scripts/makedir.sh "

# t1 is the task which will invoke the directory creation shell script
t1 = BashOperator(
    task_id='create_directory',
    bash_command=create_command,
    dag=dag)

run_spiders = "/home/ubuntu/scripts/crawl_spiders.sh "
# t2 is the task which will invoke the spiders
t2 = BashOperator(
    task_id='web_scrawl',
    bash_command=run_spiders,
    dag=dag)

# To set dependency between tasks. 't1' should run before t2
t2.set_upstream(t1)

DAG 没有被 Airflow 选中。我检查了日志,这就是它所说的。

[2017-09-12 18:08:20,220] {jobs.py:343} DagFileProcessor398 INFO - Started process (PID=7001) to work on /home/ubuntu/airflow/dags/alamode.py
[2017-09-12 18:08:20,223] {jobs.py:1521} DagFileProcessor398 INFO - Processing file /home/ubuntu/airflow/dags/alamode.py for tasks to queue
[2017-09-12 18:08:20,223] {models.py:167} DagFileProcessor398 INFO - Filling up the DagBag from /home/ubuntu/airflow/dags/alamode.py
[2017-09-12 18:08:20,262] {jobs.py:1535} DagFileProcessor398 INFO - DAG(s) ['alamode'] retrieved from /home/ubuntu/airflow/dags/alamode.py
[2017-09-12 18:08:20,291] {jobs.py:1169} DagFileProcessor398 INFO - Processing alamode
/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/default_comparator.py:161: SAWarning: The IN-predicate on "dag_run.dag_id" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.  Consider alternative strategies for improved performance.
  'strategies for improved performance.' % expr)
[2017-09-12 18:08:20,317] {models.py:322} DagFileProcessor398 INFO - Finding 'running' jobs without a recent heartbeat
[2017-09-12 18:08:20,318] {models.py:328} DagFileProcessor398 INFO - Failing jobs without heartbeat after 2017-09-12 18:03:20.318105
[2017-09-12 18:08:20,320] {jobs.py:351} DagFileProcessor398 INFO - Processing /home/ubuntu/airflow/dags/alamode.py took 0.100 seconds

我到底做错了什么?我尝试将 schedule_interval 更改为 schedule_interval=timedelta(minutes=1) 以查看它是否立即启动,但仍然没有用。我可以在 Airflow UI 中按预期看到 DAG 下的任务,但计划状态为“无状态”。请在这里帮助我。

4

1 回答 1

2

此问题已通过以下步骤解决:

1) 我为 start_date 和 schedule_interval=timedelta(minutes=10) 使用了更早的日期。此外,使用真实日期而不是 datetime.now()。
2) 在 DAG 参数中添加了 catchup = True。
3) 设置环境变量为 export AIRFLOW_HOME= pwd/airflow_home。
4)删除airflow.db
5)将新代码移动到DAGS文件夹
6)运行命令'airflow initdb'再次创建数据库。
7)通过用户界面打开我的 DAG 的“ON”开关
8)运行命令“气流调度程序”

这是现在可以使用的代码:

from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime, timedelta

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2017, 9, 12),
    'email': ['anjana@gapro.tech'],
    'retries': 0,
    'retry_delay': timedelta(minutes=15)
}

dag = DAG(
    'alamode', catchup=False, default_args=default_args, schedule_interval="@daily")

# t1 is the task which will invoke the directory creation shell script
t1 = BashOperator(
    task_id='create_directory',
    bash_command='/home/ubuntu/scripts/makedir.sh ',
    dag=dag)


# t2 is the task which will invoke the spiders
t2 = BashOperator(
    task_id= 'web_crawl',
    bash_command='/home/ubuntu/scripts/crawl_spiders.sh ',
    dag=dag)

# To set dependency between tasks. 't1' should run before t2
t2.set_upstream(t1)
于 2017-09-13T16:31:04.490 回答