|
40 | 40 | PermissionUpdateError,
|
41 | 41 | ServerStatusError,
|
42 | 42 | ServerVersionError,
|
| 43 | + TaskCreateError, |
| 44 | + TaskDeleteError, |
| 45 | + TaskGetError, |
| 46 | + TaskListError, |
43 | 47 | TransactionAbortError,
|
44 | 48 | TransactionCommitError,
|
45 | 49 | TransactionExecuteError,
|
@@ -2193,6 +2197,148 @@ def response_handler(resp: Response) -> Json:
|
2193 | 2197 |
|
2194 | 2198 | return await self._executor.execute(request, response_handler)
|
2195 | 2199 |
|
| 2200 | + async def tasks(self) -> Result[Jsons]: |
| 2201 | + """Fetches all existing tasks from the server. |
| 2202 | +
|
| 2203 | + Returns: |
| 2204 | + list: List of currently active server tasks. |
| 2205 | +
|
| 2206 | + Raises: |
| 2207 | + TaskListError: If the list cannot be retrieved. |
| 2208 | +
|
| 2209 | + References: |
| 2210 | + - `list-all-tasks <https://docs.arangodb.com/stable/develop/http-api/tasks/#list-all-tasks>`__ |
| 2211 | + """ # noqa: E501 |
| 2212 | + request = Request(method=Method.GET, endpoint="/_api/tasks") |
| 2213 | + |
| 2214 | + def response_handler(resp: Response) -> Jsons: |
| 2215 | + if not resp.is_success: |
| 2216 | + raise TaskListError(resp, request) |
| 2217 | + result: Jsons = self.deserializer.loads_many(resp.raw_body) |
| 2218 | + return result |
| 2219 | + |
| 2220 | + return await self._executor.execute(request, response_handler) |
| 2221 | + |
| 2222 | + async def task(self, task_id: str) -> Result[Json]: |
| 2223 | + """Return the details of an active server task. |
| 2224 | +
|
| 2225 | + Args: |
| 2226 | + task_id (str) -> Server task ID. |
| 2227 | +
|
| 2228 | + Returns: |
| 2229 | + dict: Details of the server task. |
| 2230 | +
|
| 2231 | + Raises: |
| 2232 | + TaskGetError: If the task details cannot be retrieved. |
| 2233 | +
|
| 2234 | + References: |
| 2235 | + - `get-a-task <https://docs.arangodb.com/stable/develop/http-api/tasks/#get-a-task>`__ |
| 2236 | + """ # noqa: E501 |
| 2237 | + request = Request(method=Method.GET, endpoint=f"/_api/tasks/{task_id}") |
| 2238 | + |
| 2239 | + def response_handler(resp: Response) -> Json: |
| 2240 | + if not resp.is_success: |
| 2241 | + raise TaskGetError(resp, request) |
| 2242 | + result: Json = self.deserializer.loads(resp.raw_body) |
| 2243 | + return result |
| 2244 | + |
| 2245 | + return await self._executor.execute(request, response_handler) |
| 2246 | + |
| 2247 | + async def create_task( |
| 2248 | + self, |
| 2249 | + command: str, |
| 2250 | + task_id: Optional[str] = None, |
| 2251 | + name: Optional[str] = None, |
| 2252 | + offset: Optional[int] = None, |
| 2253 | + params: Optional[Json] = None, |
| 2254 | + period: Optional[int] = None, |
| 2255 | + ) -> Result[Json]: |
| 2256 | + """Create a new task. |
| 2257 | +
|
| 2258 | + Args: |
| 2259 | + command (str): The JavaScript code to be executed. |
| 2260 | + task_id (str | None): Optional task ID. If not provided, the server will |
| 2261 | + generate a unique ID. |
| 2262 | + name (str | None): The name of the task. |
| 2263 | + offset (int | None): The offset in seconds after which the task should |
| 2264 | + start executing. |
| 2265 | + params (dict | None): Parameters to be passed to the command. |
| 2266 | + period (int | None): The number of seconds between the executions. |
| 2267 | +
|
| 2268 | + Returns: |
| 2269 | + dict: Details of the created task. |
| 2270 | +
|
| 2271 | + Raises: |
| 2272 | + TaskCreateError: If the task cannot be created. |
| 2273 | +
|
| 2274 | + References: |
| 2275 | + - `create-a-task <https://docs.arangodb.com/stable/develop/http-api/tasks/#create-a-task>`__ |
| 2276 | + - `create-a-task-with-id <https://docs.arangodb.com/stable/develop/http-api/tasks/#create-a-task-with-id>`__ |
| 2277 | + """ # noqa: E501 |
| 2278 | + data: Json = {"command": command} |
| 2279 | + if name is not None: |
| 2280 | + data["name"] = name |
| 2281 | + if offset is not None: |
| 2282 | + data["offset"] = offset |
| 2283 | + if params is not None: |
| 2284 | + data["params"] = params |
| 2285 | + if period is not None: |
| 2286 | + data["period"] = period |
| 2287 | + |
| 2288 | + if task_id is None: |
| 2289 | + request = Request( |
| 2290 | + method=Method.POST, |
| 2291 | + endpoint="/_api/tasks", |
| 2292 | + data=self.serializer.dumps(data), |
| 2293 | + ) |
| 2294 | + else: |
| 2295 | + request = Request( |
| 2296 | + method=Method.PUT, |
| 2297 | + endpoint=f"/_api/tasks/{task_id}", |
| 2298 | + data=self.serializer.dumps(data), |
| 2299 | + ) |
| 2300 | + |
| 2301 | + def response_handler(resp: Response) -> Json: |
| 2302 | + if not resp.is_success: |
| 2303 | + raise TaskCreateError(resp, request) |
| 2304 | + result: Json = self.deserializer.loads(resp.raw_body) |
| 2305 | + return result |
| 2306 | + |
| 2307 | + return await self._executor.execute(request, response_handler) |
| 2308 | + |
| 2309 | + async def delete_task( |
| 2310 | + self, |
| 2311 | + task_id: str, |
| 2312 | + ignore_missing: bool = False, |
| 2313 | + ) -> Result[bool]: |
| 2314 | + """Delete a server task. |
| 2315 | +
|
| 2316 | + Args: |
| 2317 | + task_id (str): Task ID. |
| 2318 | + ignore_missing (bool): If `True`, do not raise an exception if the |
| 2319 | + task does not exist. |
| 2320 | +
|
| 2321 | + Returns: |
| 2322 | + bool: `True` if the task was deleted successfully, `False` if the |
| 2323 | + task was not found and **ignore_missing** was set to `True`. |
| 2324 | +
|
| 2325 | + Raises: |
| 2326 | + TaskDeleteError: If the operation fails. |
| 2327 | +
|
| 2328 | + References: |
| 2329 | + - `delete-a-task <https://docs.arangodb.com/stable/develop/http-api/tasks/#delete-a-task>`__ |
| 2330 | + """ # noqa: E501 |
| 2331 | + request = Request(method=Method.DELETE, endpoint=f"/_api/tasks/{task_id}") |
| 2332 | + |
| 2333 | + def response_handler(resp: Response) -> bool: |
| 2334 | + if resp.is_success: |
| 2335 | + return True |
| 2336 | + if resp.status_code == HTTP_NOT_FOUND and ignore_missing: |
| 2337 | + return False |
| 2338 | + raise TaskDeleteError(resp, request) |
| 2339 | + |
| 2340 | + return await self._executor.execute(request, response_handler) |
| 2341 | + |
2196 | 2342 |
|
2197 | 2343 | class StandardDatabase(Database):
|
2198 | 2344 | """Standard database API wrapper.
|
|
0 commit comments