Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move emit events to profile and delay sending until after commit #2760

Merged
merged 2 commits into from
Feb 7, 2024
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
34 changes: 34 additions & 0 deletions aries_cloudagent/core/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def __init__(
self._entered = 0
self._context = (context or profile.context).start_scope("session", settings)
self._profile = profile
self._events = []

async def _setup(self):
"""Create the underlying session or transaction."""
Expand Down Expand Up @@ -239,6 +240,12 @@ async def commit(self):
if not self._active:
raise ProfileSessionInactiveError()
await self._teardown(commit=True)

# emit any pending events
for event in self._events:
await self.emit_event(event["topic"], event["payload"], force_emit=True)
self._events = []

self._active = False

async def rollback(self):
Expand All @@ -249,8 +256,35 @@ async def rollback(self):
if not self._active:
raise ProfileSessionInactiveError()
await self._teardown(commit=False)

# clear any pending events
self._events = []

self._active = False

async def emit_event(self, topic: str, payload: Any, force_emit: bool = False):
"""Emit an event.

If we are in an active transaction, just queue the event, otherwise emit it.

Args:
session: The profile session to use
payload: The event payload
"""

# TODO check transaction, either queue or emit event
if force_emit or (not self.is_transaction):
Copy link
Contributor

@jamshale jamshale Feb 7, 2024

Choose a reason for hiding this comment

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

Only problem I could see is events getting stuck in an is_transaction state. I'm not aware if that's possible or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should get cleared when the transaction is either committed or rolled back. (At least that's my expectation, I haven't traced every single path of code ...)

# just emit directly
await self.profile.notify(topic, payload)
else:
# add to queue
self._events.append(
{
"topic": topic,
"payload": payload,
}
)

def inject(
self,
base_cls: Type[InjectType],
Expand Down
2 changes: 1 addition & 1 deletion aries_cloudagent/messaging/models/base_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ async def emit_event(self, session: ProfileSession, payload: Any = None):
if not payload:
payload = self.serialize()

await session.profile.notify(topic, payload)
await session.emit_event(topic, payload)

@classmethod
def log_state(
Expand Down
14 changes: 11 additions & 3 deletions demo/features/0453-issue-credential.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ Feature: RFC 0453 Aries agent issue credential
When "Acme" offers a credential with data <Credential_data>
Then "Bob" has the credential issued

@GHA @WalletType_Askar
@GHA @WalletType_Askar @BasicTest
Examples:
| Acme_capabilities | Bob_capabilities | Schema_name | Credential_data |
| --public-did | | driverslicense | Data_DL_NormalizedValues |
| --public-did --did-exchange | --did-exchange | driverslicense | Data_DL_NormalizedValues |

@GHA @WalletType_Askar @AltTests
Examples:
| Acme_capabilities | Bob_capabilities | Schema_name | Credential_data |
| --public-did | | driverslicense | Data_DL_NormalizedValues |
| --public-did --mediation | --mediation | driverslicense | Data_DL_NormalizedValues |
| --public-did --multitenant | --multitenant --log-file | driverslicense | Data_DL_NormalizedValues |

@GHA @WalletType_Askar_AnonCreds
@GHA @WalletType_Askar_AnonCreds @BasicTest
Examples:
| Acme_capabilities | Bob_capabilities | Schema_name | Credential_data |
| --public-did --wallet-type askar-anoncreds | --wallet-type askar-anoncreds | driverslicense | Data_DL_NormalizedValues |

@GHA @WalletType_Askar_AnonCreds @AltTests
Examples:
| Acme_capabilities | Bob_capabilities | Schema_name | Credential_data |
| --public-did --wallet-type askar-anoncreds | | driverslicense | Data_DL_NormalizedValues |
| --public-did | --wallet-type askar-anoncreds | driverslicense | Data_DL_NormalizedValues |

Expand Down
Loading