Skip to content

Commit

Permalink
Pass resource config inside the event context (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tankilevitch authored Aug 6, 2023
1 parent 7e76883 commit b6ee4a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/PORT-4398.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pass resource config inside event context
1 change: 1 addition & 0 deletions changelog/PORT-4411.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix not supporting multiple relations
14 changes: 13 additions & 1 deletion port_ocean/context/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from port_ocean.utils import get_time

if TYPE_CHECKING:
from port_ocean.core.handlers.port_app_config.models import PortAppConfig
from port_ocean.core.handlers.port_app_config.models import (
PortAppConfig,
ResourceConfig,
)

TriggerType = Literal["manual", "machine", "request"]

Expand All @@ -25,6 +28,7 @@ class EventType:
class EventContext:
event_type: str
trigger_type: TriggerType = "machine"
resource_config: Optional["ResourceConfig"] = None
attributes: dict[str, Any] = field(default_factory=dict)
_port_app_config: Optional["PortAppConfig"] = None
_parent_event: Optional["EventContext"] = None
Expand Down Expand Up @@ -76,6 +80,7 @@ def _get_event_context() -> EventContext:
async def event_context(
event_type: str,
trigger_type: TriggerType = "manual",
resource_config: Optional["ResourceConfig"] = None,
attributes: dict[str, Any] | None = None,
) -> AsyncIterator[EventContext]:
attributes = attributes or {}
Expand All @@ -86,8 +91,11 @@ async def event_context(
EventContext(
event_type,
trigger_type=trigger_type,
resource_config=resource_config,
attributes=attributes,
_parent_event=parent,
# inherit port app config from parent event so it can be used in nested events
_port_app_config=parent.port_app_config if parent else None,
)
)

Expand All @@ -96,6 +104,10 @@ async def event_context(
event_trigger_type=event.trigger_type,
event_kind=event.event_type,
event_id=event.id,
event_parent_id=event.parent_id,
event_resource_kind=event.resource_config.kind
if event.resource_config
else None,
):
logger.info("Event started")
try:
Expand Down
16 changes: 12 additions & 4 deletions port_ocean/core/integrations/mixins/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,19 @@ async def sync_raw_all(

creation_results: list[tuple[list[Entity], list[Exception]]] = []
for resource in app_config.resources:
creation_results.append(
await self._register_in_batches(
resource, user_agent_type, ocean.config.batch_work_size
# create event context per resource kind, so resync method could have access to the resource config
# as we might have multiple resources with the same kind name
async with event_context(
EventType.RESYNC,
trigger_type=trigger_type,
resource_config=resource,
attributes={"resource_kind": resource.kind},
):
creation_results.append(
await self._register_in_batches(
resource, user_agent_type, ocean.config.batch_work_size
)
)
)
flat_created_entities, errors = zip_and_sum(creation_results) or [[], []]

if errors:
Expand Down

0 comments on commit b6ee4a2

Please sign in to comment.