|
6 | 6 | from meilisearch.config import Config
|
7 | 7 | from meilisearch.errors import MeiliSearchTimeoutError
|
8 | 8 |
|
9 |
| -class Task: |
| 9 | +def get_tasks(config: Config, index_id: Optional[str] = None) -> Dict[str, List[Dict[str, Any]]]: |
| 10 | + """Get all tasks. |
| 11 | +
|
| 12 | + Parameters |
| 13 | + ---------- |
| 14 | + config: |
| 15 | + Config object containing permission and location of MeiliSearch. |
| 16 | + index_id: |
| 17 | + The id of the `Index`. |
| 18 | +
|
| 19 | + Returns |
| 20 | + ------- |
| 21 | + task: |
| 22 | + Dictionary containing a list of all enqueued, processing, succeeded or failed tasks. |
| 23 | +
|
| 24 | + Raises |
| 25 | + ------ |
| 26 | + MeiliSearchApiError |
| 27 | + An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors |
10 | 28 | """
|
11 |
| - Tasks routes wrapper. |
12 |
| -
|
13 |
| - Task class gives access to all Task routes and child routes via an Index (inherited). |
14 |
| - https://docs.meilisearch.com/reference/api/task.html |
15 |
| - """ |
16 |
| - |
17 |
| - def __init__( |
18 |
| - self, |
19 |
| - config: Config, |
20 |
| - uid: Optional[str] = None, |
21 |
| - ) -> None: |
22 |
| - """ |
23 |
| - Parameters |
24 |
| - ---------- |
25 |
| - config: |
26 |
| - Config object containing permission and location of MeiliSearch. |
27 |
| - """ |
28 |
| - self.config = config |
29 |
| - self.http = HttpRequests(config) |
30 |
| - self.uid = uid |
31 |
| - |
32 |
| - def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]: |
33 |
| - """Get all tasks. |
34 |
| -
|
35 |
| - Returns |
36 |
| - ------- |
37 |
| - task: |
38 |
| - Dictionary containing a list of all enqueued, processing, succeeded or failed tasks. |
39 |
| -
|
40 |
| - Raises |
41 |
| - ------ |
42 |
| - MeiliSearchApiError |
43 |
| - An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors |
44 |
| - """ |
45 |
| - if self.uid is None: |
46 |
| - return self.http.get( |
47 |
| - f'{self.config.paths.task}' |
48 |
| - ) |
49 |
| - return self.http.get( |
50 |
| - f'{self.config.paths.index}/{self.uid}/{self.config.paths.task}' |
| 29 | + http = HttpRequests(config) |
| 30 | + if index_id is None: |
| 31 | + return http.get( |
| 32 | + f'{config.paths.task}' |
51 | 33 | )
|
52 |
| - |
53 |
| - |
54 |
| - def get_task(self, uid: int) -> Dict[str, Any]: |
55 |
| - """Get one task. |
56 |
| -
|
57 |
| - Parameters |
58 |
| - ---------- |
59 |
| - uid: |
60 |
| - Identifier of the task. |
61 |
| -
|
62 |
| - Returns |
63 |
| - ------- |
64 |
| - task: |
65 |
| - Dictionary containing information about the processed asynchronous task. |
66 |
| -
|
67 |
| - Raises |
68 |
| - ------ |
69 |
| - MeiliSearchApiError |
70 |
| - An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors |
71 |
| - """ |
72 |
| - if self.uid is None: |
73 |
| - return self.http.get( |
74 |
| - f'{self.config.paths.task}/{uid}' |
75 |
| - ) |
76 |
| - return self.http.get( |
77 |
| - f'{self.config.paths.index}/{self.uid}/{self.config.paths.task}/{uid}' |
| 34 | + return http.get( |
| 35 | + f'{config.paths.index}/{index_id}/{config.paths.task}' |
| 36 | + ) |
| 37 | + |
| 38 | +def get_task(config: Config, uid: int, index_id: Optional[str] = None) -> Dict[str, Any]: |
| 39 | + """Get one task. |
| 40 | +
|
| 41 | + Parameters |
| 42 | + ---------- |
| 43 | + config: |
| 44 | + Config object containing permission and location of MeiliSearch. |
| 45 | + uid: |
| 46 | + Identifier of the task. |
| 47 | + index_id: |
| 48 | + The id of the `Index`. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + task: |
| 53 | + Dictionary containing information about the status of the asynchronous task. |
| 54 | +
|
| 55 | + Raises |
| 56 | + ------ |
| 57 | + MeiliSearchApiError |
| 58 | + An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors |
| 59 | + """ |
| 60 | + http = HttpRequests(config) |
| 61 | + if index_id is None: |
| 62 | + return http.get( |
| 63 | + f'{config.paths.task}/{uid}' |
78 | 64 | )
|
79 |
| - |
80 |
| - |
81 |
| - def wait_for_task( |
82 |
| - self, uid: int, |
83 |
| - timeout_in_ms: int = 5000, |
84 |
| - interval_in_ms: int = 50, |
85 |
| - ) -> Dict[str, Any]: |
86 |
| - """Wait until MeiliSearch processes a task until it fails or succeeds. |
87 |
| -
|
88 |
| - Parameters |
89 |
| - ---------- |
90 |
| - uid: |
91 |
| - Identifier of the task to wait for being processed. |
92 |
| - timeout_in_ms (optional): |
93 |
| - Time the method should wait before raising a MeiliSearchTimeoutError. |
94 |
| - interval_in_ms (optional): |
95 |
| - Time interval the method should wait (sleep) between requests. |
96 |
| -
|
97 |
| - Returns |
98 |
| - ------- |
99 |
| - task: |
100 |
| - Dictionary containing information about the processed asynchronous task. |
101 |
| -
|
102 |
| - Raises |
103 |
| - ------ |
104 |
| - MeiliSearchTimeoutError |
105 |
| - An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors |
106 |
| - """ |
107 |
| - start_time = datetime.now() |
108 |
| - elapsed_time = 0. |
109 |
| - while elapsed_time < timeout_in_ms: |
110 |
| - get_task = self.get_task(uid) |
111 |
| - if get_task['status'] not in ('enqueued', 'processing'): |
112 |
| - return get_task |
113 |
| - sleep(interval_in_ms / 1000) |
114 |
| - time_delta = datetime.now() - start_time |
115 |
| - elapsed_time = time_delta.seconds * 1000 + time_delta.microseconds / 1000 |
116 |
| - raise MeiliSearchTimeoutError(f'timeout of ${timeout_in_ms}ms has exceeded on process ${uid} when waiting for task to be resolve.') |
| 65 | + return http.get( |
| 66 | + f'{config.paths.index}/{index_id}/{config.paths.task}/{uid}' |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def wait_for_task( |
| 71 | + config: Config, |
| 72 | + uid: int, |
| 73 | + timeout_in_ms: int = 5000, |
| 74 | + interval_in_ms: int = 50, |
| 75 | +) -> Dict[str, Any]: |
| 76 | + """Wait until the task fails or succeeds in MeiliSearch. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + uid: |
| 81 | + Identifier of the task to wait for being processed. |
| 82 | + timeout_in_ms (optional): |
| 83 | + Time the method should wait before raising a MeiliSearchTimeoutError. |
| 84 | + interval_in_ms (optional): |
| 85 | + Time interval the method should wait (sleep) between requests. |
| 86 | +
|
| 87 | + Returns |
| 88 | + ------- |
| 89 | + task: |
| 90 | + Dictionary containing information about the processed asynchronous task. |
| 91 | +
|
| 92 | + Raises |
| 93 | + ------ |
| 94 | + MeiliSearchTimeoutError |
| 95 | + An error containing details about why MeiliSearch can't process your request. MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors |
| 96 | + """ |
| 97 | + start_time = datetime.now() |
| 98 | + elapsed_time = 0. |
| 99 | + while elapsed_time < timeout_in_ms: |
| 100 | + task = get_task(config, uid) |
| 101 | + if task['status'] not in ('enqueued', 'processing'): |
| 102 | + return task |
| 103 | + sleep(interval_in_ms / 1000) |
| 104 | + time_delta = datetime.now() - start_time |
| 105 | + elapsed_time = time_delta.seconds * 1000 + time_delta.microseconds / 1000 |
| 106 | + raise MeiliSearchTimeoutError(f'timeout of ${timeout_in_ms}ms has exceeded on process ${uid} when waiting for task to be resolve.') |
0 commit comments