2
2
import json
3
3
import jwt
4
4
import logging
5
- import requests
6
5
7
6
from airflow .models import Variable
8
7
from airflow .utils .state import State
11
10
12
11
CONN_ID = "process_report"
13
12
ROUTES = {
14
- "progress" : "progress" ,
15
- "results" : "results" ,
16
- "status" : "status"
13
+ "progress" : "airflow/ progress" ,
14
+ "results" : "airflow/ results" ,
15
+ "status" : "airflow/ status"
17
16
}
18
17
PRIVATE_KEY = "process_report_private_key"
19
18
ALGORITHM = "process_report_algorithm"
20
19
20
+ http_hook = HttpHook (method = "POST" , http_conn_id = CONN_ID ) # won't fail even if CONN_ID doesn't exist
21
21
22
- def prepare_connection (conn_id , route ):
23
- http_hook = HttpHook (http_conn_id = conn_id )
24
- session = http_hook .get_conn ()
25
- url = "/" .join (
26
- [
27
- u .strip ("/" ) for u in [http_hook .base_url , session .headers ["endpoint" ], route ]
28
- ]
29
- )
30
- return http_hook , session , url
31
22
32
-
33
- def sign_with_jwt (data , private_key = None , algorithm = None ):
23
+ def sign_with_jwt (data ):
34
24
try :
35
25
data = jwt .encode (
36
26
payload = data ,
37
- key = private_key or Variable .get (PRIVATE_KEY ),
38
- algorithm = algorithm or Variable .get (ALGORITHM )
27
+ key = Variable .get (PRIVATE_KEY ),
28
+ algorithm = Variable .get (ALGORITHM )
39
29
).decode ("utf-8" )
40
30
except Exception as err :
41
31
logging .debug (f"Failed to sign data with JWT key. \n { err } " )
@@ -45,27 +35,19 @@ def sign_with_jwt(data, private_key=None, algorithm=None):
45
35
def post_progress (context , from_task = None ):
46
36
from_task = False if from_task is None else from_task
47
37
try :
48
- http_hook , session , url = prepare_connection (CONN_ID , ROUTES ["progress" ])
49
38
dag_run = context ["dag_run" ]
50
39
len_tis = len (dag_run .get_task_instances ())
51
40
len_tis_success = len (dag_run .get_task_instances (state = State .SUCCESS )) + int (from_task )
52
41
data = sign_with_jwt (
53
- data = {
54
- "state" : dag_run .state ,
55
- "dag_id" : dag_run .dag_id ,
56
- "run_id" : dag_run .run_id ,
42
+ {
43
+ "state" : dag_run .state ,
44
+ "dag_id" : dag_run .dag_id ,
45
+ "run_id" : dag_run .run_id ,
57
46
"progress" : int (len_tis_success / len_tis * 100 ),
58
- "error" : context ["reason" ] if dag_run .state == State .FAILED else ""
47
+ "error" : context ["reason" ] if dag_run .state == State .FAILED else ""
59
48
}
60
49
)
61
- prepped_request = session .prepare_request (
62
- requests .Request (
63
- "POST" ,
64
- url ,
65
- json = {"payload" : data }
66
- )
67
- )
68
- http_hook .run_and_check (session , prepped_request , {})
50
+ http_hook .run (endpoint = ROUTES ["progress" ], json = {"payload" : data })
69
51
except Exception as err :
70
52
logging .debug (f"Failed to POST progress updates. \n { err } " )
71
53
@@ -76,11 +58,10 @@ def post_results(context):
76
58
isinstance(task, CWLJobGatherer) to find the proper task because of the
77
59
endless import loop (file where we define CWLJobGatherer class import this
78
60
file). If CWLDAG is contsructed with custom gatherer node, posting results
79
- might not work.
61
+ might not work. We need to except missing results file as the same callback
62
+ is used for clean_dag_run DAG
80
63
"""
81
-
82
64
try :
83
- http_hook , session , url = prepare_connection (CONN_ID , ROUTES ["results" ])
84
65
dag_run = context ["dag_run" ]
85
66
results = {}
86
67
try :
@@ -89,47 +70,31 @@ def post_results(context):
89
70
results = json .load (input_stream )
90
71
except Exception as err :
91
72
logging .debug (f"Failed to read results. \n { err } " )
92
-
93
73
data = sign_with_jwt (
94
- data = {
95
- "dag_id" : dag_run .dag_id ,
96
- "run_id" : dag_run .run_id ,
74
+ {
75
+ "dag_id" : dag_run .dag_id ,
76
+ "run_id" : dag_run .run_id ,
97
77
"results" : results
98
78
}
99
79
)
100
- prepped_request = session .prepare_request (
101
- requests .Request (
102
- "POST" ,
103
- url ,
104
- json = {"payload" : data }
105
- )
106
- )
107
- http_hook .run_and_check (session , prepped_request , {})
80
+ http_hook .run (endpoint = ROUTES ["results" ], json = {"payload" : data })
108
81
except Exception as err :
109
82
logging .debug (f"Failed to POST results. \n { err } " )
110
83
111
84
112
85
def post_status (context ):
113
86
try :
114
- http_hook , session , url = prepare_connection (CONN_ID , ROUTES ["status" ])
115
87
dag_run = context ["dag_run" ]
116
88
ti = context ["ti" ]
117
89
data = sign_with_jwt (
118
- data = {
119
- "state" : ti .state ,
120
- "dag_id" : dag_run .dag_id ,
121
- "run_id" : dag_run .run_id ,
122
- "task_id" : ti .task_id
90
+ {
91
+ "state" : ti .state ,
92
+ "dag_id" : dag_run .dag_id ,
93
+ "run_id" : dag_run .run_id ,
94
+ "task_id" : ti .task_id
123
95
}
124
96
)
125
- prepped_request = session .prepare_request (
126
- requests .Request (
127
- "POST" ,
128
- url ,
129
- json = {"payload" : data }
130
- )
131
- )
132
- http_hook .run_and_check (session , prepped_request , {})
97
+ http_hook .run (endpoint = ROUTES ["status" ], json = {"payload" : data })
133
98
except Exception as err :
134
99
logging .debug (f"Failed to POST status updates. \n { err } " )
135
100
0 commit comments