Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ayon_api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Comment on lines 1623 to 1627
) -> list[dict[str, Any]]:
"""Post multiple CRUD operations to server.
Expand All @@ -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
Comment on lines 1639 to 1642

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Wait for events to be processed on server" does sound clearer?

fails. You can handle failed operations on your own
when set to 'False'.
Expand All @@ -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,
)

Expand All @@ -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.

Expand All @@ -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.
Expand All @@ -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,
)


Expand Down Expand Up @@ -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.
Expand All @@ -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'.
Expand All @@ -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,
)

Expand Down
10 changes: 7 additions & 3 deletions ayon_api/_api_helpers/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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'.
Expand All @@ -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,
)
1 change: 1 addition & 0 deletions ayon_api/_api_helpers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
31 changes: 27 additions & 4 deletions ayon_api/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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(
Expand Down
23 changes: 16 additions & 7 deletions ayon_api/server_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'.
Expand All @@ -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(
Expand All @@ -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.

Expand All @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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 []
Expand All @@ -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")
Expand Down
Loading