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

Events: Reusable events #310

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
67 changes: 49 additions & 18 deletions ayon_server/events/eventstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async def dispatch(
payload: dict[str, Any] | None = None,
finished: bool = True,
store: bool = True,
reuse: bool = False,
recipients: list[str] | None = None,
) -> str:
"""
Expand All @@ -46,6 +47,9 @@ async def dispatch(
store:
whether to store the event in the database

reuse:
allow to reuse an existing event with the same hash

recipients:
list of user names to notify via websocket (None for all users)
"""
Expand Down Expand Up @@ -79,32 +83,59 @@ async def dispatch(
)

if store:
query = SQLTool.insert(
table="events",
id=event.id,
hash=event.hash,
sender=event.sender,
topic=event.topic,
project_name=event.project,
user_name=event.user,
depends_on=depends_on,
status=status,
description=description,
summary=event.summary,
payload=event.payload,
)
query = """
INSERT INTO
events (
id, hash, sender, topic, project_name, user_name,
depends_on, status, description, summary, payload
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
"""
if reuse:
query += """
ON CONFLICT (hash) DO UPDATE SET
id = EXCLUDED.id,
sender = EXCLUDED.sender,
topic = EXCLUDED.topic,
project_name = EXCLUDED.project_name,
user_name = EXCLUDED.user_name,
depends_on = EXCLUDED.depends_on,
status = EXCLUDED.status,
description = EXCLUDED.description,
summary = EXCLUDED.summary,
payload = EXCLUDED.payload,
updated_at = NOW()
"""

try:
await Postgres.execute(*query)
await Postgres.execute(
query,
event.id,
event.hash,
event.sender,
event.topic,
event.project,
event.user,
event.depends_on,
status,
description,
event.summary,
event.payload,
)
except Postgres.ForeignKeyViolationError as e:
raise ConstraintViolationException(
"Event depends on non-existing event",
) from e

except Postgres.UniqueViolationError as e:
raise ConstraintViolationException(
"Event with same hash already exists",
) from e
if reuse:
raise ConstraintViolationException(
"Unable to reuse the event. Another event depends on it",
) from e
else:
raise ConstraintViolationException(
"Event with same hash already exists",
) from e

depends_on = (
str(event.depends_on).replace("-", "") if event.depends_on else None
Expand Down