5
5
from arango .utils import HTTP_OK
6
6
from arango .exceptions import (
7
7
AsyncExecuteError ,
8
- AsyncJobInvalidError ,
9
- AsyncJobNotDoneError ,
10
- AsyncJobNotFoundError ,
11
8
AsyncJobCancelError ,
12
- AsyncJobGetStatusError ,
13
- AsyncJobGetResultError ,
9
+ AsyncJobStatusError ,
10
+ AsyncJobResultError ,
14
11
AsyncJobClearError
15
12
)
16
13
from arango .graph import Graph
@@ -39,14 +36,15 @@ def __init__(self, connection, return_result=True):
39
36
username = connection .username ,
40
37
password = connection .password ,
41
38
http_client = connection .http_client ,
42
- database = connection .database
39
+ database = connection .database ,
40
+ enable_logging = connection .has_logging
43
41
)
44
42
self ._return_result = return_result
45
43
self ._aql = AQL (self )
46
44
self ._type = 'async'
47
45
48
46
def __repr__ (self ):
49
- return '<ArangoDB asynchronous request >'
47
+ return '<ArangoDB asynchronous execution >'
50
48
51
49
def handle_request (self , request , handler ):
52
50
"""Handle the incoming request and response handler.
@@ -57,11 +55,13 @@ def handle_request(self, request, handler):
57
55
:type handler: callable
58
56
:returns: the async job or None
59
57
:rtype: arango.async.AsyncJob
58
+ :raises arango.exceptions.AsyncExecuteError: if the async request
59
+ cannot be executed
60
60
"""
61
61
if self ._return_result :
62
62
request .headers ['x-arango-async' ] = 'store'
63
63
else :
64
- request .headers ['x-arango-async' ] = True
64
+ request .headers ['x-arango-async' ] = 'true'
65
65
66
66
res = getattr (self , request .method )(** request .kwargs )
67
67
if res .status_code not in HTTP_OK :
@@ -145,66 +145,48 @@ def status(self):
145
145
"""Return the status of the async job from the server.
146
146
147
147
: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
149
149
an exception)
150
150
: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
156
152
async job cannot be retrieved from the server
157
153
"""
158
- res = self ._conn .get ('/_api/job/{}' .format (self ._id ))
154
+ res = self ._conn .get ('/_api/job/{}' .format (self .id ))
159
155
if res .status_code == 204 :
160
156
return 'pending'
161
157
elif res .status_code in HTTP_OK :
162
158
return 'done'
163
- elif res .status_code == 400 :
164
- raise AsyncJobInvalidError (res )
165
159
elif res .status_code == 404 :
166
- raise AsyncJobNotFoundError (res )
160
+ raise AsyncJobStatusError (res , 'Job {} missing' . format ( self . id ) )
167
161
else :
168
- raise AsyncJobGetStatusError (res )
162
+ raise AsyncJobStatusError (res )
169
163
170
164
def result (self ):
171
165
"""Return the result of the async job if available.
172
166
173
167
:returns: the result or the exception from the async job
174
168
: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
182
170
async job cannot be retrieved from the server
183
171
184
172
.. note::
185
173
An async job result will automatically be cleared from the server
186
174
once fetched and will *not* be available in subsequent calls.
187
175
"""
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 :
197
178
try :
198
179
result = self ._handler (res )
199
180
except Exception as error :
200
181
return error
201
182
else :
202
183
return result
203
184
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 )
208
190
209
191
def cancel (self , ignore_missing = False ):
210
192
"""Cancel the async job if it is still pending.
@@ -214,55 +196,40 @@ def cancel(self, ignore_missing=False):
214
196
:returns: ``True`` if the job was cancelled successfully, ``False`` if
215
197
the job was not found but **ignore_missing** was set to ``True``
216
198
: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
221
199
:raises arango.exceptions.AsyncJobCancelError: if the async job cannot
222
200
be cancelled
223
201
224
202
.. 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).
226
205
"""
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 ))
229
207
if res .status_code == 200 :
230
208
return True
231
- elif res .status_code == 400 :
232
- raise AsyncJobInvalidError (res , 'Job {} invalid' .format (_id ))
233
209
elif res .status_code == 404 :
234
210
if ignore_missing :
235
211
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 )
238
215
239
216
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.
245
218
246
219
:param ignore_missing: ignore missing async jobs
247
220
:type ignore_missing: bool
248
221
:returns: ``True`` if the result was deleted successfully, ``False``
249
222
if the job was not found but **ignore_missing** was set to ``True``
250
223
: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
257
226
"""
258
- _id = self ._id
259
- res = self ._conn .delete ('/_api/job/{}' .format (_id ))
227
+ res = self ._conn .delete ('/_api/job/{}' .format (self ._id ))
260
228
if res .status_code in HTTP_OK :
261
229
return True
262
- elif res .status_code == 400 :
263
- raise AsyncJobInvalidError (res , 'Job {} invalid' .format (_id ))
264
230
elif res .status_code == 404 :
265
231
if ignore_missing :
266
232
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