diff --git a/ayon_api/_api.py b/ayon_api/_api.py index dfa178e88..fd4bee8a3 100644 --- a/ayon_api/_api.py +++ b/ayon_api/_api.py @@ -1621,7 +1621,9 @@ def get_rest_entity_by_id( def send_batch_operations( project_name: str, operations: list[dict[str, Any]], + *, can_fail: bool = False, + wait_for_events: bool = False, raise_on_fail: bool = True, ) -> list[dict[str, Any]]: """Post multiple CRUD operations to server. @@ -1636,6 +1638,7 @@ def send_batch_operations( operations (list[dict[str, Any]]): Operations to be processed. can_fail (bool): Server will try to process all operations even if one of them fails. + wait_for_events (bool): Wait for events to be processed on server. raise_on_fail (bool): Raise exception if an operation fails. You can handle failed operations on your own when set to 'False'. @@ -1654,6 +1657,7 @@ def send_batch_operations( project_name=project_name, operations=operations, can_fail=can_fail, + wait_for_events=wait_for_events, raise_on_fail=raise_on_fail, ) @@ -1663,8 +1667,9 @@ def send_background_batch_operations( operations: list[dict[str, Any]], *, can_fail: bool = False, - wait: bool = False, + wait_for_events: bool = False, raise_on_fail: bool = True, + wait: bool = False, ) -> BackgroundOperationTask: """Post multiple CRUD operations to server. @@ -1687,10 +1692,11 @@ def send_background_batch_operations( operations (list[dict[str, Any]]): Operations to be processed. can_fail (bool): Server will try to process all operations even if one of them fails. - wait (bool): Wait for operations to end. + wait_for_events (bool): Wait for events to be processed on server. raise_on_fail (bool): Raise exception if an operation fails. You can handle failed operations on your own when set to 'False'. Used when 'wait' is enabled. + wait (bool): Wait for operations to end. Raises: ValueError: Operations can't be converted to json string. @@ -1706,8 +1712,9 @@ def send_background_batch_operations( project_name=project_name, operations=operations, can_fail=can_fail, - wait=wait, + wait_for_events=wait_for_events, raise_on_fail=raise_on_fail, + wait=wait, ) @@ -2709,7 +2716,9 @@ def set_entity_watchers( def send_activities_batch_operations( project_name: str, operations: list[dict[str, Any]], + *, can_fail: bool = False, + wait_for_events: bool = False, raise_on_fail: bool = True, ) -> list[dict[str, Any]]: """Post multiple CRUD activities operations to server. @@ -2724,6 +2733,7 @@ def send_activities_batch_operations( operations (list[dict[str, Any]]): Operations to be processed. can_fail (Optional[bool]): Server will try to process all operations even if one of them fails. + wait_for_events (bool): Wait for events to be processed on server. raise_on_fail (Optional[bool]): Raise exception if an operation fails. You can handle failed operations on your own when set to 'False'. @@ -2742,6 +2752,7 @@ def send_activities_batch_operations( project_name=project_name, operations=operations, can_fail=can_fail, + wait_for_events=wait_for_events, raise_on_fail=raise_on_fail, ) diff --git a/ayon_api/_api_helpers/activities.py b/ayon_api/_api_helpers/activities.py index 2428e3b54..277edaefa 100644 --- a/ayon_api/_api_helpers/activities.py +++ b/ayon_api/_api_helpers/activities.py @@ -370,8 +370,10 @@ def send_activities_batch_operations( self, project_name: str, operations: list[dict[str, Any]], + *, can_fail: bool = False, - raise_on_fail: bool = True + wait_for_events: bool = False, + raise_on_fail: bool = True, ) -> list[dict[str, Any]]: """Post multiple CRUD activities operations to server. @@ -385,6 +387,7 @@ def send_activities_batch_operations( operations (list[dict[str, Any]]): Operations to be processed. can_fail (Optional[bool]): Server will try to process all operations even if one of them fails. + wait_for_events (bool): Wait for events to be processed on server. raise_on_fail (Optional[bool]): Raise exception if an operation fails. You can handle failed operations on your own when set to 'False'. @@ -401,6 +404,7 @@ def send_activities_batch_operations( return self._send_batch_operations( f"projects/{project_name}/operations/activities", operations, - can_fail, - raise_on_fail, + can_fail=can_fail, + wait_for_events=wait_for_events, + raise_on_fail=raise_on_fail, ) diff --git a/ayon_api/_api_helpers/base.py b/ayon_api/_api_helpers/base.py index d9284207b..d852a95b4 100644 --- a/ayon_api/_api_helpers/base.py +++ b/ayon_api/_api_helpers/base.py @@ -167,6 +167,7 @@ def _send_batch_operations( uri: str, operations: list[dict[str, Any]], can_fail: bool, + wait_for_events: bool, raise_on_fail: bool ) -> list[dict[str, Any]]: raise NotImplementedError() diff --git a/ayon_api/operations.py b/ayon_api/operations.py index e39167a80..0ef2ce705 100644 --- a/ayon_api/operations.py +++ b/ayon_api/operations.py @@ -741,12 +741,21 @@ class OperationsSession(object): Args: con (Optional[ServerAPI]): Connection to server. Global connection is used if not passed. + wait_for_events (bool): Wait for events to be processed on server. + Default value of the argument. Can be changed when 'commit' + is called. """ - def __init__(self, con: Optional[ServerAPI] = None) -> None: + def __init__( + self, + con: Optional[ServerAPI] = None, + *, + wait_for_events: bool = False, + ) -> None: if con is None: con = get_server_api_connection() self._con = con + self._wait_for_events = wait_for_events self._project_cache = {} self._operations = [] self._nested_operations = collections.defaultdict(list) @@ -817,8 +826,15 @@ def to_data(self) -> list[dict[str, Any]]: for operation in self._operations ] - def commit(self) -> None: - """Commit session operations.""" + def commit(self, *, wait_for_events: bool | None = None) -> None: + """Commit session operations. + + Args: + wait_for_events (bool | None): Wait for events to be processed + on server. Use 'None' to use the default value defined on + OperationsSession object. + + """ operations, self._operations = self._operations, [] if not operations: return @@ -827,6 +843,9 @@ def commit(self) -> None: for operation in operations: operations_by_project[operation.project_name].append(operation) + if wait_for_events is None: + wait_for_events = self._wait_for_events + for project_name, operations in operations_by_project.items(): operations_body = [] for operation in operations: @@ -835,7 +854,11 @@ def commit(self) -> None: operations_body.append(body) self._con.send_background_batch_operations( - project_name, operations_body, wait=True, can_fail=False + project_name, + operations_body, + can_fail=False, + wait_for_events=wait_for_events, + wait=True, ) def create_entity( diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index e27bcd496..9981554c9 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -2475,7 +2475,9 @@ def send_batch_operations( self, project_name: str, operations: list[dict[str, Any]], + *, can_fail: bool = False, + wait_for_events: bool = False, raise_on_fail: bool = True, ) -> list[dict[str, Any]]: """Post multiple CRUD operations to server. @@ -2490,6 +2492,7 @@ def send_batch_operations( operations (list[dict[str, Any]]): Operations to be processed. can_fail (bool): Server will try to process all operations even if one of them fails. + wait_for_events (bool): Wait for events to be processed on server. raise_on_fail (bool): Raise exception if an operation fails. You can handle failed operations on your own when set to 'False'. @@ -2506,8 +2509,9 @@ def send_batch_operations( return self._send_batch_operations( f"projects/{project_name}/operations", operations, - can_fail, - raise_on_fail, + can_fail=can_fail, + wait_for_events=wait_for_events, + raise_on_fail=raise_on_fail, ) def send_background_batch_operations( @@ -2516,8 +2520,9 @@ def send_background_batch_operations( operations: list[dict[str, Any]], *, can_fail: bool = False, - wait: bool = False, + wait_for_events: bool = False, raise_on_fail: bool = True, + wait: bool = False, ) -> BackgroundOperationTask: """Post multiple CRUD operations to server. @@ -2540,10 +2545,11 @@ def send_background_batch_operations( operations (list[dict[str, Any]]): Operations to be processed. can_fail (bool): Server will try to process all operations even if one of them fails. - wait (bool): Wait for operations to end. + wait_for_events (bool): Wait for events to be processed on server. raise_on_fail (bool): Raise exception if an operation fails. You can handle failed operations on your own when set to 'False'. Used when 'wait' is enabled. + wait (bool): Wait for operations to end. Raises: ValueError: Operations can't be converted to json string. @@ -2558,7 +2564,8 @@ def send_background_batch_operations( response = self.post( f"projects/{project_name}/operations/background", operations=operations_body, - canFail=can_fail + canFail=can_fail, + waitForEvents=wait_for_events, ) response.raise_for_status() if not wait: @@ -2631,7 +2638,8 @@ def _send_batch_operations( uri: str, operations: list[dict[str, Any]], can_fail: bool, - raise_on_fail: bool + wait_for_events: bool, + raise_on_fail: bool, ) -> list[dict[str, Any]]: if not operations: return [] @@ -2643,7 +2651,8 @@ def _send_batch_operations( response = self.post( uri, operations=operations_body, - canFail=can_fail + canFail=can_fail, + waitForEvents=wait_for_events, ) op_results = response.get("operations")