20
20
# lib imports
21
21
from .classes import RunnerParser
22
22
from .helpers import get_logger
23
-
23
+ from . exceptions import JobNotFoundException , JobStillRunningException
24
24
25
25
logger = get_logger ()
26
26
runner_parser = RunnerParser ()
@@ -34,8 +34,9 @@ class Shell2HttpAPI(MethodView):
34
34
"""
35
35
36
36
def get (self ):
37
+ key : str = ""
37
38
try :
38
- key : str = request .args .get ("key" )
39
+ key = request .args .get ("key" )
39
40
logger .info (
40
41
f"Job: '{ key } ' --> Report requested. "
41
42
f"Requester: '{ request .remote_addr } '."
@@ -46,29 +47,44 @@ def get(self):
46
47
# get the future object
47
48
future : Future = self .executor .futures ._futures .get (key )
48
49
if not future :
49
- raise Exception (f"No report exists for key: '{ key } '." )
50
+ raise JobNotFoundException (f"No report exists for key: '{ key } '." )
50
51
51
52
# check if job has been finished
52
53
if not future .done ():
53
- logger .debug (f"Job: '{ key } ' --> still running." )
54
- return make_response (jsonify (status = "running" , key = key ), 200 )
54
+ raise JobStillRunningException ()
55
55
56
56
# pop future object since it has been finished
57
57
self .executor .futures .pop (key )
58
58
59
59
# if yes, get result from store
60
60
report : Dict = future .result ()
61
61
if not report :
62
- raise Exception (f"Job: '{ key } ' --> No report exists." )
62
+ raise JobNotFoundException (f"Job: '{ key } ' --> No report exists." )
63
63
64
64
logger .debug (f"Job: '{ key } ' --> Requested report: { report } " )
65
65
return jsonify (report )
66
66
67
- except Exception as e :
67
+ except JobNotFoundException as e :
68
68
logger .error (e )
69
69
return make_response (jsonify (error = str (e )), HTTPStatus .NOT_FOUND )
70
70
71
+ except JobStillRunningException :
72
+ logger .debug (f"Job: '{ key } ' --> still running." )
73
+ return make_response (
74
+ jsonify (
75
+ status = "running" ,
76
+ key = key ,
77
+ result_url = self .__build_result_url (key ),
78
+ ),
79
+ HTTPStatus .OK ,
80
+ )
81
+
82
+ except Exception as e :
83
+ logger .error (e )
84
+ return make_response (jsonify (error = str (e )), HTTPStatus .BAD_REQUEST )
85
+
71
86
def post (self ):
87
+ key : str = ""
72
88
try :
73
89
logger .info (
74
90
f"Received request for endpoint: '{ request .url_rule } '. "
@@ -96,15 +112,23 @@ def post(self):
96
112
)
97
113
98
114
logger .info (f"Job: '{ key } ' --> added to queue for command: { cmd } " )
99
- result_url = f" { request . base_url } ? key= { key } "
115
+ result_url = self . __build_result_url ( key )
100
116
return make_response (
101
117
jsonify (status = "running" , key = key , result_url = result_url ),
102
118
HTTPStatus .ACCEPTED ,
103
119
)
104
120
105
121
except Exception as e :
106
122
logger .error (e )
107
- return make_response (jsonify (error = str (e )), HTTPStatus .BAD_REQUEST )
123
+ response_dict = {"error" : str (e )}
124
+ if key :
125
+ response_dict ["key" ] = key
126
+ response_dict ["result_url" ] = self .__build_result_url (key )
127
+ return make_response (jsonify (response_dict ), HTTPStatus .BAD_REQUEST )
128
+
129
+ @classmethod
130
+ def __build_result_url (cls , key : str ) -> str :
131
+ return f"{ request .base_url } ?key={ key } "
108
132
109
133
def __init__ (
110
134
self ,
0 commit comments