airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Seems dependencies accross Dags don't work ?

Open adadle opened this issue 11 years ago • 8 comments

I'm trying to set upstream task in upstream_dag,downstream task in downstream_dag. But it seems it does not work.

  • upstream
from datetime import datetime, timedelta

from airflow.operators import BashOperator, EmailOperator
from airflow.models import DAG

args = {
    'owner': 'tony',
    'start_date': datetime.strptime('2015-09-01 00:00:00', '%Y-%m-%d %H:%M:%S'),
    'email': ['xxx'],
    'email_on_failure': True,
    'email_on_retry': True,
    'retries': 3,
    'retry_interval': timedelta(minutes=5),
    'schedule_interval': timedelta(minutes=5),
}

dag = DAG(dag_id='upstream_dag', default_args=args)

ETL_HOME = '/home/prod/deepdive'
step_one = BashOperator(
    task_id='upstream',
    bash_command=(
        "export ETL_HOME=%(ETL_HOME)s; "
        " echo ${ETL_HOME} "
    ) % {
        'ETL_HOME': ETL_HOME,
    },
dag=dag)
  • downstream
from datetime import datetime, timedelta

from airflow.operators import BashOperator, EmailOperator
from airflow.models import DAG
from upstream import step_one as up_task

args = {
    'owner': 'tony',
    'start_date': datetime.strptime('2015-09-01 00:00:00', '%Y-%m-%d %H:%M:%S'),
    'email': MAIL_TO,
    'email_on_failure': True,
    'email_on_retry': True,
    'retries': 3,
    'retry_interval': timedelta(minutes=5),
    'schedule_interval': timedelta(minutes=5),
}

dag = DAG(dag_id='downstream_dag', default_args=args)

ETL_HOME = '/home/prod/deepdive'
down = BashOperator(
    task_id='donwstream',
    bash_command=(
        "export ETL_HOME=%(ETL_HOME)s; "
        " echo "downstream_dag" "
    ) % {
        'ETL_HOME': ETL_HOME,
    },
dag=dag)

down.set_upstream(up_task)

It will log out : [2015-09-01 15:38:16,983] {models.py:778} WARNING - Dependencies not met yet

adadle avatar Sep 01 '15 07:09 adadle

For cross dag dependencies, use ExternalTaskSensor, it allows you to wait for task completion within a different dag

jomar83 avatar Sep 01 '15 07:09 jomar83

Thanks @jomar83 ,I will have a try.

adadle avatar Sep 01 '15 07:09 adadle

Your welcome. In your case it would look something like this:

et = ExternalTaskSensor(
  external_dag_id='upstream_dag',
  external_task_id='upstream',
  dag=dag, 
  task_id='wait_for_upstream')

down definition here

down.set_upstream(et)

jomar83 avatar Sep 01 '15 08:09 jomar83

Still dont work,Do I need to set execution_date? both the tasks's execution_date I'm running is 2015-08-31 00:00:00

from datetime import datetime, timedelta

from airflow.operators import BashOperator, EmailOperator
from airflow.models import DAG
from airflow.operators import ExternalTaskSensor

MAIL_TO = [
    '[email protected]'
]

args = {
    'owner': 'tony',
    'start_date': datetime.strptime('2015-08-31 12:00:00', '%Y-%m-%d %H:%M:%S'),
    'email': MAIL_TO,
    'email_on_failure': True,
    'email_on_retry': True,
    'retries': 3,
    'retry_interval': timedelta(minutes=5),
    'schedule_interval': timedelta(minutes=5),
}

dag = DAG(dag_id='downstream_dag', default_args=args)

ETL_HOME = '/home/prod/deepdive'
et = ExternalTaskSensor(
  external_dag_id='upstream_dag',
  external_task_id='upstream',
  dag=dag, 
  task_id='wait_for_upstream'
)

down = BashOperator(
    task_id='downstream',
    bash_command=(
        "export ETL_HOME=%(ETL_HOME)s; "
        " echo 'downstream_dag' "
    ) % {
        'ETL_HOME': ETL_HOME,
    },dag=dag)

down.set_upstream(et)

adadle avatar Sep 01 '15 09:09 adadle

can you elaborate on what issue you're facing? As long as your upstream dag is called "upstream_dag" and the task your waiting for in that dag is called "upstream", your down task should start shortly after the upstream task has completed successfully... In the web ui the "et" Sensor should be running while the upstream task hasn't finished.

jomar83 avatar Sep 01 '15 10:09 jomar83

After I have ran the upstream, and the status I saw is success. Then I ran downstream, but it still can not generate a TI,and log out:Dependencies not met yet. BTW, I did not seee any "et" Sensor in web ui. I'm running v1.41. Appriciate for your help.

adadle avatar Sep 02 '15 02:09 adadle

There's a refresh button on the DAGs page (the rightmost icon in the table view), that indicates to the web server to refresh that dag's definition. Also schedule_interval is a DAG parameter, not a task parameter.

Are you trying to run the DAG every 5 minutes?

mistercrunch avatar Sep 02 '15 04:09 mistercrunch

Great, thanks! It works well now. At first, I'm thinking the scheduler will auto generate TIs by the schedule_interval parameter, It's just a demo for upcoming depency accross dags usage,so I need to schedule 5 mins to see whether it's ok. Close it.

adadle avatar Sep 02 '15 06:09 adadle