13
13
from arangoasync .connection import Connection
14
14
from arangoasync .errno import HTTP_FORBIDDEN , HTTP_NOT_FOUND
15
15
from arangoasync .exceptions import (
16
+ AsyncJobClearError ,
17
+ AsyncJobListError ,
16
18
CollectionCreateError ,
17
19
CollectionDeleteError ,
18
20
CollectionListError ,
41
43
UserReplaceError ,
42
44
UserUpdateError ,
43
45
)
44
- from arangoasync .executor import ApiExecutor , DefaultApiExecutor , TransactionApiExecutor
46
+ from arangoasync .executor import (
47
+ ApiExecutor ,
48
+ AsyncApiExecutor ,
49
+ DefaultApiExecutor ,
50
+ TransactionApiExecutor ,
51
+ )
45
52
from arangoasync .request import Method , Request
46
53
from arangoasync .response import Response
54
+ from arangoasync .result import Result
47
55
from arangoasync .serialization import Deserializer , Serializer
48
56
from arangoasync .typings import (
49
57
CollectionInfo ,
53
61
Jsons ,
54
62
KeyOptions ,
55
63
Params ,
56
- Result ,
57
64
ServerStatusInformation ,
58
65
UserInfo ,
59
66
)
@@ -1314,7 +1321,7 @@ def response_handler(resp: Response) -> str:
1314
1321
return cast (str , result ["id" ])
1315
1322
1316
1323
transaction_id = await self ._executor .execute (request , response_handler )
1317
- return TransactionDatabase (self .connection , transaction_id )
1324
+ return TransactionDatabase (self .connection , cast ( str , transaction_id ) )
1318
1325
1319
1326
def fetch_transaction (self , transaction_id : str ) -> "TransactionDatabase" :
1320
1327
"""Fetch an existing transaction.
@@ -1328,6 +1335,86 @@ def fetch_transaction(self, transaction_id: str) -> "TransactionDatabase":
1328
1335
"""
1329
1336
return TransactionDatabase (self .connection , transaction_id )
1330
1337
1338
+ def begin_async_execution (self , return_result : bool = True ) -> "AsyncDatabase" :
1339
+ """Begin async execution.
1340
+
1341
+ Args:
1342
+ return_result (bool): If set to `True`, API executions return instances of
1343
+ `arangoasync.job.AsyncJob`, which you can be used to retrieve
1344
+ results from server once available. Otherwise, API executions
1345
+ return `None` and no results are stored on server.
1346
+
1347
+ Returns:
1348
+ AsyncDatabase: Database API wrapper tailored for async execution.
1349
+ """
1350
+ return AsyncDatabase (self .connection , return_result )
1351
+
1352
+ async def async_jobs (
1353
+ self , status : str , count : Optional [int ] = None
1354
+ ) -> Result [List [str ]]:
1355
+ """Return IDs of async jobs with given status.
1356
+
1357
+ Args:
1358
+ status (str): Job status (e.g. "pending", "done").
1359
+ count (int | None): Max number of job IDs to return.
1360
+
1361
+ Returns:
1362
+ list: List of job IDs.
1363
+
1364
+ Raises:
1365
+ AsyncJobListError: If retrieval fails.
1366
+
1367
+ References:
1368
+ - `list-async-jobs-by-status-or-get-the-status-of-specific-job <https://docs.arangodb.com/stable/develop/http-api/jobs/#list-async-jobs-by-status-or-get-the-status-of-specific-job>`__
1369
+ """ # noqa: E501
1370
+ params : Params = {}
1371
+ if count is not None :
1372
+ params ["count" ] = count
1373
+
1374
+ request = Request (
1375
+ method = Method .GET , endpoint = f"/_api/job/{ status } " , params = params
1376
+ )
1377
+
1378
+ def response_handler (resp : Response ) -> List [str ]:
1379
+ if resp .is_success :
1380
+ return cast (List [str ], self .deserializer .loads (resp .raw_body ))
1381
+ raise AsyncJobListError (resp , request )
1382
+
1383
+ return await self ._executor .execute (request , response_handler )
1384
+
1385
+ async def clear_async_jobs (self , threshold : Optional [float ] = None ) -> None :
1386
+ """Clear async job results from the server.
1387
+
1388
+ Async jobs that are still queued or running are not stopped.
1389
+ Clients can use this method to perform an eventual garbage
1390
+ collection of job results.
1391
+
1392
+ Args:
1393
+ threshold (float | None): If specified, only the job results created
1394
+ prior to the threshold (a Unix timestamp) are deleted. Otherwise,
1395
+ all job results are deleted.
1396
+
1397
+ Raises:
1398
+ AsyncJobClearError: If the operation fails.
1399
+
1400
+ References:
1401
+ - `delete-async-job-results <https://docs.arangodb.com/stable/develop/http-api/jobs/#delete-async-job-results>`__
1402
+ """ # noqa: E501
1403
+ if threshold is None :
1404
+ request = Request (method = Method .DELETE , endpoint = "/_api/job/all" )
1405
+ else :
1406
+ request = Request (
1407
+ method = Method .DELETE ,
1408
+ endpoint = "/_api/job/expired" ,
1409
+ params = {"stamp" : threshold },
1410
+ )
1411
+
1412
+ def response_handler (resp : Response ) -> None :
1413
+ if not resp .is_success :
1414
+ raise AsyncJobClearError (resp , request )
1415
+
1416
+ await self ._executor .execute (request , response_handler )
1417
+
1331
1418
1332
1419
class TransactionDatabase (Database ):
1333
1420
"""Database API tailored specifically for
@@ -1420,3 +1507,26 @@ def response_handler(resp: Response) -> None:
1420
1507
raise TransactionAbortError (resp , request )
1421
1508
1422
1509
await self ._standard_executor .execute (request , response_handler )
1510
+
1511
+
1512
+ class AsyncDatabase (Database ):
1513
+ """Database API wrapper tailored specifically for async execution.
1514
+
1515
+ See :func:`arangoasync.database.StandardDatabase.begin_async_execution`.
1516
+
1517
+ Args:
1518
+ connection (Connection): HTTP connection.
1519
+ return_result (bool): If set to `True`, API executions return instances of
1520
+ :class:`arangoasync.job.AsyncJob`, which you can use to retrieve results
1521
+ from server once available. If set to `False`, API executions return `None`
1522
+ and no results are stored on server.
1523
+
1524
+ References:
1525
+ - `jobs <https://docs.arangodb.com/stable/develop/http-api/jobs/>`__
1526
+ """ # noqa: E501
1527
+
1528
+ def __init__ (self , connection : Connection , return_result : bool ) -> None :
1529
+ super ().__init__ (executor = AsyncApiExecutor (connection , return_result ))
1530
+
1531
+ def __repr__ (self ) -> str :
1532
+ return f"<AsyncDatabase { self .name } >"
0 commit comments