|
27 | 27 | import base64 |
28 | 28 | import random |
29 | 29 | import sys |
| 30 | +import time |
30 | 31 | from platform import python_version |
31 | 32 |
|
32 | 33 | try: |
@@ -257,6 +258,31 @@ def batch(self, requests, request_options=None): |
257 | 258 | path = '/1/indexes/*/batch' |
258 | 259 | return self._req(False, path, 'POST', request_options, data=requests) |
259 | 260 |
|
| 261 | + def wait_task(self, index_name, task_id, time_before_retry=100, request_options=None): |
| 262 | + """ |
| 263 | + Wait the publication of a task on the server. |
| 264 | + All server task are asynchronous and you can check with this method |
| 265 | + that the task is published. |
| 266 | +
|
| 267 | + @param index_name the index name associated with the taskID |
| 268 | + @param task_id the id of the task returned by server |
| 269 | + @param time_before_retry the time in milliseconds before retry (default = 100ms) |
| 270 | + @param request_options |
| 271 | + """ |
| 272 | + while True: |
| 273 | + task = self.get_task(index_name, task_id, request_options) |
| 274 | + if task['status'] == 'published': |
| 275 | + return task |
| 276 | + time.sleep(time_before_retry / 1000.0) |
| 277 | + |
| 278 | + def get_task(self, index_name, task_id, request_options=None): |
| 279 | + path = '/1/indexes/%s/task/%d' % (safe(index_name), task_id) |
| 280 | + return self._req(True, path, 'GET', request_options) |
| 281 | + |
| 282 | + def is_task_published(self, index_name, task_id, request_options=None): |
| 283 | + task = self.get_task(index_name, task_id, request_options) |
| 284 | + return task['status'] == 'published' |
| 285 | + |
260 | 286 | @deprecated |
261 | 287 | def listIndexes(self): |
262 | 288 | return self.list_indexes() |
|
0 commit comments