55from arango .utils import HTTP_OK
66from arango .exceptions import (
77 AsyncExecuteError ,
8- AsyncJobInvalidError ,
9- AsyncJobNotDoneError ,
10- AsyncJobNotFoundError ,
118 AsyncJobCancelError ,
12- AsyncJobGetStatusError ,
13- AsyncJobGetResultError ,
9+ AsyncJobStatusError ,
10+ AsyncJobResultError ,
1411 AsyncJobClearError
1512)
1613from arango .graph import Graph
@@ -39,14 +36,15 @@ def __init__(self, connection, return_result=True):
3936 username = connection .username ,
4037 password = connection .password ,
4138 http_client = connection .http_client ,
42- database = connection .database
39+ database = connection .database ,
40+ enable_logging = connection .has_logging
4341 )
4442 self ._return_result = return_result
4543 self ._aql = AQL (self )
4644 self ._type = 'async'
4745
4846 def __repr__ (self ):
49- return '<ArangoDB asynchronous request >'
47+ return '<ArangoDB asynchronous execution >'
5048
5149 def handle_request (self , request , handler ):
5250 """Handle the incoming request and response handler.
@@ -57,11 +55,13 @@ def handle_request(self, request, handler):
5755 :type handler: callable
5856 :returns: the async job or None
5957 :rtype: arango.async.AsyncJob
58+ :raises arango.exceptions.AsyncExecuteError: if the async request
59+ cannot be executed
6060 """
6161 if self ._return_result :
6262 request .headers ['x-arango-async' ] = 'store'
6363 else :
64- request .headers ['x-arango-async' ] = True
64+ request .headers ['x-arango-async' ] = 'true'
6565
6666 res = getattr (self , request .method )(** request .kwargs )
6767 if res .status_code not in HTTP_OK :
@@ -145,66 +145,48 @@ def status(self):
145145 """Return the status of the async job from the server.
146146
147147 :returns: the status of the async job, which can be ``"pending"`` (the
148- job is still in the queue), ``"done"`` (the job completed or raised
148+ job is still in the queue), ``"done"`` (the job finished or raised
149149 an exception)
150150 :rtype: str
151- :raises arango.exceptions.AsyncJobInvalidError: if the async job is
152- not valid
153- :raises arango.exceptions.AsyncJobNotFoundError: if the async job
154- cannot be found in the server
155- :raises arango.exceptions.AsyncJobGetStatusError: if the status of the
151+ :raises arango.exceptions.AsyncJobStatusError: if the status of the
156152 async job cannot be retrieved from the server
157153 """
158- res = self ._conn .get ('/_api/job/{}' .format (self ._id ))
154+ res = self ._conn .get ('/_api/job/{}' .format (self .id ))
159155 if res .status_code == 204 :
160156 return 'pending'
161157 elif res .status_code in HTTP_OK :
162158 return 'done'
163- elif res .status_code == 400 :
164- raise AsyncJobInvalidError (res )
165159 elif res .status_code == 404 :
166- raise AsyncJobNotFoundError (res )
160+ raise AsyncJobStatusError (res , 'Job {} missing' . format ( self . id ) )
167161 else :
168- raise AsyncJobGetStatusError (res )
162+ raise AsyncJobStatusError (res )
169163
170164 def result (self ):
171165 """Return the result of the async job if available.
172166
173167 :returns: the result or the exception from the async job
174168 :rtype: object
175- :raises arango.exceptions.AsyncJobInvalidError: if the async job is
176- not valid
177- :raises arango.exceptions.AsyncJobNotFoundError: if the async job
178- cannot be found in the server
179- :raises arango.exceptions.AsyncJobNotDoneError: if the async job is
180- still pending in the queue
181- :raises arango.exceptions.AsyncJobGetResultError: if the result of the
169+ :raises arango.exceptions.AsyncJobResultError: if the result of the
182170 async job cannot be retrieved from the server
183171
184172 .. note::
185173 An async job result will automatically be cleared from the server
186174 once fetched and will *not* be available in subsequent calls.
187175 """
188- _id = self ._id
189- res = self ._conn .put ('/_api/job/{}' .format (_id ))
190- if (
191- res .status_code == 404 and
192- res .error_code == 404 and
193- res .error_message == 'not found'
194- ):
195- raise AsyncJobNotFoundError (res , 'Job {} not found' .format (_id ))
196- elif res .body is not None :
176+ res = self ._conn .put ('/_api/job/{}' .format (self ._id ))
177+ if 'X-Arango-Async-Id' in res .headers :
197178 try :
198179 result = self ._handler (res )
199180 except Exception as error :
200181 return error
201182 else :
202183 return result
203184 elif res .status_code == 204 :
204- raise AsyncJobNotDoneError (res , 'Job {} pending' .format (_id ))
205- elif res .status_code == 400 :
206- raise AsyncJobInvalidError (res , 'Job {} invalid' .format (_id ))
207- raise AsyncJobGetResultError (res , 'Failed to query job {}' .format (_id ))
185+ raise AsyncJobResultError (res , 'Job {} not done' .format (self ._id ))
186+ elif res .status_code == 404 :
187+ raise AsyncJobResultError (res , 'Job {} missing' .format (self ._id ))
188+ else :
189+ raise AsyncJobResultError (res )
208190
209191 def cancel (self , ignore_missing = False ):
210192 """Cancel the async job if it is still pending.
@@ -214,55 +196,40 @@ def cancel(self, ignore_missing=False):
214196 :returns: ``True`` if the job was cancelled successfully, ``False`` if
215197 the job was not found but **ignore_missing** was set to ``True``
216198 :rtype: bool
217- :raises arango.exceptions.AsyncJobInvalidError: if the async job is
218- not valid
219- :raises arango.exceptions.AsyncJobNotFoundError: if the async job
220- cannot be found in the server
221199 :raises arango.exceptions.AsyncJobCancelError: if the async job cannot
222200 be cancelled
223201
224202 .. note::
225- An async job cannot be cancelled once it is taken out of the queue.
203+ An async job cannot be cancelled once it is taken out of the queue
204+ (i.e. started, finished or cancelled).
226205 """
227- _id = self ._id
228- res = self ._conn .put ('/_api/job/{}/cancel' .format (_id ))
206+ res = self ._conn .put ('/_api/job/{}/cancel' .format (self ._id ))
229207 if res .status_code == 200 :
230208 return True
231- elif res .status_code == 400 :
232- raise AsyncJobInvalidError (res , 'Job {} invalid' .format (_id ))
233209 elif res .status_code == 404 :
234210 if ignore_missing :
235211 return False
236- raise AsyncJobNotFoundError (res , 'Job {} not found' .format (_id ))
237- raise AsyncJobCancelError (res , 'Failed to cancel job {}' .format (_id ))
212+ raise AsyncJobCancelError (res , 'Job {} missing' .format (self ._id ))
213+ else :
214+ raise AsyncJobCancelError (res )
238215
239216 def clear (self , ignore_missing = False ):
240- """Clear the result of the job from the server if available.
241-
242- If the result is deleted successfully, boolean True is returned. If
243- the job was not found but ``ignore_missing`` was set, boolean False
244- is returned.
217+ """Delete the result of the job from the server.
245218
246219 :param ignore_missing: ignore missing async jobs
247220 :type ignore_missing: bool
248221 :returns: ``True`` if the result was deleted successfully, ``False``
249222 if the job was not found but **ignore_missing** was set to ``True``
250223 :rtype: bool
251- :raises arango.exceptions.AsyncJobInvalidError: if the async job is
252- not valid
253- :raises arango.exceptions.AsyncJobNotFoundError: if the async job
254- cannot be found in the server
255- :raises arango.exceptions.AsyncJobClearError: if the result of
256- the async job cannot be removed from the server
224+ :raises arango.exceptions.AsyncJobClearError: if the result of the
225+ async job cannot be delete from the server
257226 """
258- _id = self ._id
259- res = self ._conn .delete ('/_api/job/{}' .format (_id ))
227+ res = self ._conn .delete ('/_api/job/{}' .format (self ._id ))
260228 if res .status_code in HTTP_OK :
261229 return True
262- elif res .status_code == 400 :
263- raise AsyncJobInvalidError (res , 'Job {} invalid' .format (_id ))
264230 elif res .status_code == 404 :
265231 if ignore_missing :
266232 return False
267- raise AsyncJobNotFoundError (res , 'Job {} not found' .format (_id ))
268- raise AsyncJobClearError (res , 'Failed to clear job {}' .format (_id ))
233+ raise AsyncJobClearError (res , 'Job {} missing' .format (self ._id ))
234+ else :
235+ raise AsyncJobClearError (res )
0 commit comments