|
2 | 2 |
|
3 | 3 | import time
|
4 | 4 | import warnings
|
5 |
| -from typing import TYPE_CHECKING, Any, NamedTuple |
| 5 | +from typing import TYPE_CHECKING, Any, Callable, NamedTuple |
6 | 6 |
|
| 7 | +from .._utils import batched |
7 | 8 | from ..core import BoundModelBase, ClientEntityBase, Meta
|
8 | 9 | from .domain import Action, ActionFailedException, ActionTimeoutException
|
9 | 10 |
|
@@ -129,6 +130,100 @@ class ActionsClient(ResourceActionsClient):
|
129 | 130 | def __init__(self, client: Client):
|
130 | 131 | super().__init__(client, None)
|
131 | 132 |
|
| 133 | + def _get_list_by_ids(self, ids: list[int]) -> list[BoundAction]: |
| 134 | + """ |
| 135 | + Get a list of Actions by their IDs. |
| 136 | +
|
| 137 | + :param ids: List of Action IDs to get. |
| 138 | + :raises ValueError: Raise when Action IDs were not found. |
| 139 | + :return: List of Actions. |
| 140 | + """ |
| 141 | + actions: list[BoundAction] = [] |
| 142 | + |
| 143 | + for ids_batch in batched(ids, 25): |
| 144 | + params: dict[str, Any] = { |
| 145 | + "id": ids_batch, |
| 146 | + } |
| 147 | + |
| 148 | + response = self._client.request( |
| 149 | + method="GET", |
| 150 | + url="/actions", |
| 151 | + params=params, |
| 152 | + ) |
| 153 | + |
| 154 | + actions.extend( |
| 155 | + BoundAction(self._client.actions, action_data) |
| 156 | + for action_data in response["actions"] |
| 157 | + ) |
| 158 | + |
| 159 | + if len(ids) != len(actions): |
| 160 | + found_ids = [a.id for a in actions] |
| 161 | + not_found_ids = list(set(ids) - set(found_ids)) |
| 162 | + |
| 163 | + raise ValueError(f"actions not found: {', '.join(not_found_ids)}") |
| 164 | + |
| 165 | + return actions |
| 166 | + |
| 167 | + def wait_for_function( |
| 168 | + self, |
| 169 | + handle_update: Callable[[BoundAction], None], |
| 170 | + actions: list[Action | BoundAction], |
| 171 | + ) -> list[BoundAction]: |
| 172 | + """ |
| 173 | + Waits until all Actions succeed by polling the API at the interval defined by |
| 174 | + the client's poll interval and function. An Action is considered as complete |
| 175 | + when its status is either "success" or "error". |
| 176 | +
|
| 177 | + The handle_update callback is called every time an Action is updated. |
| 178 | +
|
| 179 | + :param handle_update: Function called every time an Action is updated. |
| 180 | + :param actions: List of Actions to wait for. |
| 181 | + :raises: ActionFailedException when an Action failed. |
| 182 | + :return: List of succeeded Actions. |
| 183 | + """ |
| 184 | + running_ids = [a.id for a in actions] |
| 185 | + |
| 186 | + completed: list[BoundAction] = [] |
| 187 | + |
| 188 | + retries = 0 |
| 189 | + while len(running_ids): |
| 190 | + # pylint: disable=protected-access |
| 191 | + time.sleep(self._client._poll_interval_func(retries)) |
| 192 | + retries += 1 |
| 193 | + |
| 194 | + updates = self._get_list_by_ids(running_ids) |
| 195 | + |
| 196 | + for update in updates: |
| 197 | + if update.status != Action.STATUS_RUNNING: |
| 198 | + running_ids.remove(update.id) |
| 199 | + completed.append(update) |
| 200 | + |
| 201 | + handle_update(update) |
| 202 | + |
| 203 | + return completed |
| 204 | + |
| 205 | + def wait_for( |
| 206 | + self, |
| 207 | + actions: list[Action | BoundAction], |
| 208 | + ) -> list[BoundAction]: |
| 209 | + """ |
| 210 | + Waits until all Actions succeed by polling the API at the interval defined by |
| 211 | + the client's poll interval and function. An Action is considered as complete |
| 212 | + when its status is either "success" or "error". |
| 213 | +
|
| 214 | + If a single Action fails, the function will stop waiting and raise ActionFailedException. |
| 215 | +
|
| 216 | + :param actions: List of Actions to wait for. |
| 217 | + :raises: ActionFailedException when an Action failed. |
| 218 | + :return: List of succeeded Actions. |
| 219 | + """ |
| 220 | + |
| 221 | + def handle_update(update: BoundAction): |
| 222 | + if update.status == Action.STATUS_ERROR: |
| 223 | + raise ActionFailedException(action=update) |
| 224 | + |
| 225 | + return self.wait_for_function(handle_update, actions) |
| 226 | + |
132 | 227 | def get_list(
|
133 | 228 | self,
|
134 | 229 | status: list[str] | None = None,
|
|
0 commit comments