/
bash_operator_test.py
66 lines (47 loc) · 1.66 KB
/
bash_operator_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from builtins import range
from datetime import timedelta
import airflow
from airflow.models import DAG , Variable
from airflow.operators.bash_operator import BashOperator
from airflow.operators.python_operator import PythonOperator
from airflow.operators.dummy_operator import DummyOperator
import pendulum
args = {
'owner': 'Airflow',
'start_date': airflow.utils.dates.days_ago(2),
}
dag = DAG(
dag_id='example_bash_operator',
default_args=args,
schedule_interval='0 0 * * *',
dagrun_timeout=timedelta(minutes=60),
)
task = BashOperator(
task_id='task1',
bash_command='echo "{{ task_instance_key_str }} Now {{ execution_date }} Prev {{ var.value.last_execution_date_succes }} " && sleep 1',
dag=dag,
)
task2 = BashOperator(
task_id='task2',
bash_command='echo "{{ task_instance_key_str }} Now {{ execution_date }} Prec {{ var.value.last_execution_date_succes }} " && sleep 1',
dag=dag,
)
task5 = BashOperator(
task_id='task5',
bash_command='echo "{{ task_instance_key_str }} Now {{ execution_date }} Prec {{ var.value.last_execution_date_succes }} " && sleep 1',
dag=dag,
)
task4 = BashOperator(
task_id='task4',
bash_command='echo "{{ task_instance_key_str }} Now {{ execution_date }} Prec {{ var.value.last_execution_date_succes }} " && sleep 1',
dag=dag,
)
def setEndTime(**kwargs):
Variable.set('last_execution_date_succes', kwargs['execution_date'])
task3 = PythonOperator(task_id='setTime',
provide_context=True,
python_callable=setEndTime,
dag=dag)
task >> task2 >> [task5,task4] >> task3
if __name__ == "__main__":
dag.cli()