-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamictask2.py
103 lines (85 loc) · 2.66 KB
/
dynamictask2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import airflow
from airflow.operators.python_operator import PythonOperator
from airflow.operators.http_operator import SimpleHttpOperator
import time
import os
BdpHttpConn = 'bdp_conn'
BdpHttpConnTriggerJobEndPoint = 'schedule-control/task/triggerJob'
main_dag_id = 'DynamicTask2'
args = {
'owner': 'robbie',
'start_date': airflow.utils.dates.days_ago(60),
'provide_context': True,
}
dag = airflow.DAG(
main_dag_id,
schedule_interval='40 14 22 * *',
default_args=args,
)
currtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def save_curr_time():
fw = open('/tmp/shapetest.txt', 'w')
fw.write(currtime)
fw.close()
def get_curr_time_from_file():
if False == os.path.exists('/tmp/shapetest.txt'):
save_curr_time()
with open('/tmp/shapetest.txt', 'r') as fr:
currtime_tmp = fr.read()
return currtime_tmp
def doShapeMap(start, end, *args, **kwargs):
print("doShapeMap:", start, " to ", end)
print(currtime)
def doShapeReduce(*args, **kwargs):
print("doShapeReduce")
print(currtime)
save_curr_time()
doShapeReduceTask = PythonOperator(
task_id='shape_reduce',
dag=dag,
provide_context=True,
python_callable=doShapeReduce,
op_args=[],
)
def doTwoThree(start, end, *args, **kwargs):
print("doTwoThree:", start, " to ", end)
print(get_curr_time_from_file())
def doTwoThreeReduce(*args, **kwargs):
print("doTwoThreeReduce:%s" % get_curr_time_from_file())
doTwoThreeReduceTask = PythonOperator(
task_id='two_three_reduce',
dag=dag,
provide_context=True,
python_callable=doTwoThreeReduce,
op_args=[],
)
total = 5
task_trigger_data_prepare_step_one = SimpleHttpOperator(
task_id='task_trigger_data_prepare_step_one',
http_conn_id=BdpHttpConn,
endpoint=BdpHttpConnTriggerJobEndPoint+get_curr_time_from_file(),
method='GET',
headers={'Content-Type': 'application/json'},
data={"curr_time": get_curr_time_from_file()},
dag=dag,
retries=10,
retry_delay=60,
response_check=lambda response: True if 200 == response.status_code else False,
xcom_push=True,
)
for index in range(total):
doDynamicShapeMapTask = PythonOperator(
task_id='shape_map_' + str(index),
dag=dag,
provide_context=True,
python_callable=doShapeMap,
op_args=[index, index],
)
doDynamicTwoThreeTask = PythonOperator(
task_id='two_three_' + str(index),
dag=dag,
provide_context=True,
python_callable=doTwoThree,
op_args=[index, index],
)
doDynamicShapeMapTask >> doShapeReduceTask >> doDynamicTwoThreeTask >> doTwoThreeReduceTask >> task_trigger_data_prepare_step_one