-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add service for event and integration with Prefect event #4493
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .models import EventMeta, InfrahubEvent | ||
from .node_action import NodeMutatedEvent | ||
|
||
__all__ = ["EventMeta", "InfrahubEvent", "NodeMutatedEvent"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
EVENT_NAMESPACE = "infrahub" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from typing import Any | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from infrahub.message_bus import InfrahubMessage, Meta | ||
|
||
from .constants import EVENT_NAMESPACE | ||
|
||
|
||
class EventMeta(BaseModel): | ||
request_id: str = "" | ||
account_id: str = "" | ||
initiator_id: str | None = Field( | ||
default=None, description="The worker identity of the initial sender of this message" | ||
) | ||
|
||
|
||
class InfrahubEvent(BaseModel): | ||
meta: EventMeta | None = None | ||
|
||
def get_event_namespace(self) -> str: | ||
return EVENT_NAMESPACE | ||
|
||
def get_name(self) -> str: | ||
return f"{self.get_event_namespace()}.unknown" | ||
|
||
def get_resource(self) -> dict[str, str]: | ||
raise NotImplementedError | ||
|
||
def get_message(self) -> InfrahubMessage: | ||
raise NotImplementedError | ||
|
||
def get_related(self) -> list[dict[str, str]]: | ||
related: list[dict[str, str]] = [] | ||
|
||
if not self.meta: | ||
return related | ||
|
||
if self.meta.account_id: | ||
related.append( | ||
{ | ||
"prefect.resource.id": f"infrahub.account.{self.meta.account_id}", | ||
"prefect.resource.role": "account", | ||
} | ||
) | ||
|
||
if self.meta.request_id: | ||
related.append( | ||
{ | ||
"prefect.resource.id": f"infrahub.request.{self.meta.request_id}", | ||
"prefect.resource.role": "request", | ||
} | ||
) | ||
|
||
if self.meta.initiator_id: | ||
related.append( | ||
{ | ||
"prefect.resource.id": f"infrahub.source.{self.meta.initiator_id}", | ||
"prefect.resource.role": "event_source", | ||
} | ||
) | ||
|
||
return related | ||
|
||
def get_payload(self) -> dict[str, Any]: | ||
return {} | ||
|
||
def get_message_meta(self) -> Meta: | ||
meta = Meta() | ||
if not self.meta: | ||
return meta | ||
|
||
if self.meta.initiator_id: | ||
meta.initiator_id = self.meta.initiator_id | ||
if self.meta.request_id: | ||
meta.initiator_id = self.meta.request_id | ||
|
||
return meta | ||
|
||
|
||
class InfrahubBranchEvent(InfrahubEvent): # pylint: disable=abstract-method | ||
branch: str = Field(..., description="The branch on which the event happend") | ||
|
||
def get_related(self) -> list[dict[str, str]]: | ||
related = super().get_related() | ||
related.append( | ||
{ | ||
"prefect.resource.id": "infrahub.branch", | ||
"prefect.resource.name": self.branch, | ||
"prefect.resource.role": "branch", | ||
} | ||
) | ||
return related |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Any | ||
|
||
from pydantic import Field | ||
|
||
from infrahub.core.constants import MutationAction | ||
from infrahub.message_bus.messages.event_node_mutated import EventNodeMutated | ||
|
||
from .models import InfrahubBranchEvent | ||
|
||
|
||
class NodeMutatedEvent(InfrahubBranchEvent): | ||
"""Event generated when a node has been mutated""" | ||
|
||
kind: str = Field(..., description="The type of object modified") | ||
node_id: str = Field(..., description="The ID of the mutated node") | ||
action: MutationAction = Field(..., description="The action taken on the node") | ||
data: dict[str, Any] = Field(..., description="Data on modified object") | ||
|
||
def get_name(self) -> str: | ||
return f"{self.get_event_namespace()}.node.{self.action.value}" | ||
|
||
def get_resource(self) -> dict[str, str]: | ||
return { | ||
"prefect.resource.id": f"infrahub.node.{self.node_id}", | ||
"infrahub.node.kind": self.kind, | ||
"infrahub.node.action": self.action.value, | ||
} | ||
|
||
def get_payload(self) -> dict[str, Any]: | ||
return self.data | ||
|
||
def get_message(self) -> EventNodeMutated: | ||
return EventNodeMutated( | ||
branch=self.branch, | ||
kind=self.kind, | ||
node_id=self.node_id, | ||
action=self.action.value, | ||
data=self.data, | ||
meta=self.get_message_meta(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from typing import TYPE_CHECKING | ||
|
||
from prefect.events import emit_event | ||
|
||
from infrahub.exceptions import InitializationError | ||
|
||
if TYPE_CHECKING: | ||
from infrahub.events import InfrahubEvent | ||
from infrahub.services import InfrahubServices | ||
|
||
|
||
class InfrahubEventService: | ||
"""Base class for infrahub event service""" | ||
|
||
def __init__(self) -> None: | ||
self._service: InfrahubServices | None = None | ||
|
||
@property | ||
def service(self) -> InfrahubServices: | ||
if not self._service: | ||
raise InitializationError("Event is not initialized with a service") | ||
|
||
return self._service | ||
|
||
async def initialize(self, service: InfrahubServices) -> None: | ||
"""Initialize the event service""" | ||
self._service = service | ||
|
||
async def send(self, event: InfrahubEvent) -> None: | ||
tasks = [self._send_bus(event=event), self._send_prefect(event=event)] | ||
await asyncio.gather(*tasks) | ||
|
||
async def _send_bus(self, event: InfrahubEvent) -> None: | ||
message = event.get_message() | ||
await self.service.send(message=message) | ||
|
||
async def _send_prefect(self, event: InfrahubEvent) -> None: | ||
emit_event( | ||
event=event.get_name(), | ||
resource=event.get_resource(), | ||
related=event.get_related(), | ||
payload=event.get_payload(), | ||
) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks a bit problematic, is there an async version of emit_event?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I could find quickly but as far as I understand they are sending the event in a non blocking way. I'm planning to dive deeper into it soon.
We may have to create our own function to send the event but for now since we are sending the events in a background task I don't think it will make a big different
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right the background tasks are run in a separate thread regardless. But we'll have to keep it in mind if used elsewhere, though if Prefects emit_event does the same it should be fine.