How to wait for an asynchronous event in a task of a DAG in a workflow implemented using Airflow?

岁酱吖の 提交于 2019-12-05 02:47:11

One possible solution is using a sensor that waits forever until something external manually sets its state to success.

So you'd have some sort of dummy sensor:

class DummySensor(BaseSensorOperator):

    def poke(self, context):
        return False

Initialized like this:

task_c = DummySensor(
    dag=dag,
    task_id='task_c',
    interval=600,  # something fairly high since we're not polling for anything, just to check when to timeout
    timeout=3600,  # if nothing externally sets state to success in 1 hour, task will fail so task D does not run
)

When Task C starts, it will just sit in RUNNING state. Then you can use the REST API Plugin to set Task C's state to SUCCESS when the condition is met. Task D and other downstream tasks will then get kicked off.

The downside to this is the dummy sensor still holds onto a worker slot while it waits doing nothing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!