28
28
29
29
class Shell2HttpAPI (MethodView ):
30
30
"""
31
- ``Flask.MethodView`` that registers ``GET`` and ``POST ``
31
+ ``Flask.MethodView`` that creates ``GET``, ``POST`` and ``DELETE ``
32
32
methods for a given endpoint.
33
33
This is invoked on ``Shell2HTTP.register_command``.
34
34
@@ -37,6 +37,7 @@ class Shell2HttpAPI(MethodView):
37
37
38
38
def get (self ):
39
39
"""
40
+ Get report by job key.
40
41
Args:
41
42
key (str):
42
43
- Future key
@@ -138,6 +139,42 @@ def post(self):
138
139
response_dict ["result_url" ] = self .__build_result_url (key )
139
140
return make_response (jsonify (response_dict ), HTTPStatus .BAD_REQUEST )
140
141
142
+ def delete (self ):
143
+ """
144
+ Cancel (if running) and delete job by job key.
145
+ Args:
146
+ key (str):
147
+ - Future key
148
+ """
149
+ key : str = ""
150
+ try :
151
+ key = request .args .get ("key" )
152
+ logger .info (
153
+ f"Job: '{ key } ' --> deletion requested. "
154
+ f"Requester: '{ request .remote_addr } '."
155
+ )
156
+ if not key :
157
+ raise Exception ("No key provided in arguments." )
158
+
159
+ # get the future object
160
+ future : Future = self .executor .futures ._futures .get (key )
161
+ if not future :
162
+ raise JobNotFoundException (f"No job exists for key: '{ key } '." )
163
+
164
+ # cancel and delete from memory
165
+ future .cancel ()
166
+ self .executor .futures .pop (key )
167
+
168
+ return make_response ({}, HTTPStatus .NO_CONTENT )
169
+
170
+ except JobNotFoundException as e :
171
+ logger .error (e )
172
+ return make_response (jsonify (error = str (e )), HTTPStatus .NOT_FOUND )
173
+
174
+ except Exception as e :
175
+ logger .error (e )
176
+ return make_response (jsonify (error = str (e )), HTTPStatus .BAD_REQUEST )
177
+
141
178
@classmethod
142
179
def __build_result_url (cls , key : str ) -> str :
143
180
return f"{ request .base_url } ?key={ key } &wait=false"
0 commit comments